Преглед на файлове

Merge pull request #313 from ckeditor/i/5595

Fix: Improved error rethrowing by replacing the error stack. Closes ckeditor/ckeditor5#5595.
Piotrek Koszuliński преди 6 години
родител
ревизия
3ef96e5809

+ 12 - 12
packages/ckeditor5-utils/src/ckeditorerror.js

@@ -94,11 +94,11 @@ export default class CKEditorError extends Error {
 
 	/**
 	 * A utility that ensures the the thrown error is a {@link module:utils/ckeditorerror~CKEditorError} one.
-	 * It is uesful when combined with the {@link module:watchdog/watchdog~Watchdog} feature, which can restart the editor in case
+	 * It is useful when combined with the {@link module:watchdog/watchdog~Watchdog} feature, which can restart the editor in case
 	 * of a {@link module:utils/ckeditorerror~CKEditorError} error.
 	 *
 	 * @param {Error} err An error.
-	 * @param {Object} context An object conected through properties with the editor instance. This context will be used
+	 * @param {Object} context An object connected through properties with the editor instance. This context will be used
 	 * by the watchdog to verify which editor should be restarted.
 	 */
 	static rethrowUnexpectedError( err, context ) {
@@ -107,21 +107,21 @@ export default class CKEditorError extends Error {
 		}
 
 		/**
-		 * An unexpected error occurred inside the CKEditor 5 codebase. The `error.data.originalError` property
-		 * shows the original error properties.
+		 * An unexpected error occurred inside the CKEditor 5 codebase. This error will look like the original one
+		 * to make the debugging easier.
 		 *
 		 * This error is only useful when the editor is initialized using the {@link module:watchdog/watchdog~Watchdog} feature.
-		 * In case of such error (or any {@link module:utils/ckeditorerror~CKEditorError} error) the wathcdog should restart the editor.
+		 * In case of such error (or any {@link module:utils/ckeditorerror~CKEditorError} error) the watchdog should restart the editor.
 		 *
 		 * @error unexpected-error
 		 */
-		throw new CKEditorError( 'unexpected-error', context, {
-			originalError: {
-				message: err.message,
-				stack: err.stack,
-				name: err.name
-			}
-		} );
+		const error = new CKEditorError( err.message, context );
+
+		// Restore the original stack trace to make the error look like the original one.
+		// See https://github.com/ckeditor/ckeditor5/issues/5595 for more details.
+		error.stack = err.stack;
+
+		throw error;
 	}
 }
 

+ 2 - 0
packages/ckeditor5-utils/src/emittermixin.js

@@ -239,6 +239,8 @@ const EmitterMixin = {
 
 			return eventInfo.return;
 		} catch ( err ) {
+			// @if CK_DEBUG // throw err;
+			/* istanbul ignore next */
 			CKEditorError.rethrowUnexpectedError( err, this );
 		}
 	},

+ 1 - 7
packages/ckeditor5-utils/tests/ckeditorerror.js

@@ -106,13 +106,7 @@ describe( 'CKEditorError', () => {
 
 			expectToThrowCKEditorError( () => {
 				CKEditorError.rethrowUnexpectedError( error, context );
-			}, /unexpected-error/, context, {
-				originalError: {
-					message: 'foo',
-					stack: 'bar',
-					name: 'Error'
-				}
-			} );
+			}, /foo/, context );
 		} );
 	} );
 } );

+ 4 - 11
packages/ckeditor5-utils/tests/emittermixin.js

@@ -166,23 +166,16 @@ describe( 'EmitterMixin', () => {
 			}, /Foo/, null );
 		} );
 
-		it( 'should wrap an error into the CKEditorError if a native error was thrown', () => {
-			const error = new Error( 'foo' );
-			error.stack = 'bar';
+		it( 'should rethrow the native errors as they are in the dubug=true mode', () => {
+			const error = new TypeError( 'foo' );
 
 			emitter.on( 'test', () => {
 				throw error;
 			} );
 
-			expectToThrowCKEditorError( () => {
+			expect( () => {
 				emitter.fire( 'test' );
-			}, /unexpected-error/, emitter, {
-				originalError: {
-					message: 'foo',
-					stack: 'bar',
-					name: 'Error'
-				}
-			} );
+			} ).to.throw( TypeError, /foo/ );
 		} );
 
 		describe( 'return value', () => {