8
0
فهرست منبع

Made the `CKEditor.rethrowUnexpectedError()` stack trace more readable.

Maciej Bukowski 6 سال پیش
والد
کامیت
31bc4a1769
2فایلهای تغییر یافته به همراه27 افزوده شده و 28 حذف شده
  1. 15 10
      packages/ckeditor5-utils/src/ckeditorerror.js
  2. 12 18
      packages/ckeditor5-utils/tests/emittermixin.js

+ 15 - 10
packages/ckeditor5-utils/src/ckeditorerror.js

@@ -1,3 +1,5 @@
+/* globals console */
+
 /**
  * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
@@ -94,11 +96,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 ) {
@@ -111,17 +113,20 @@ export default class CKEditorError extends Error {
 		 * shows the original error properties.
 		 *
 		 * 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.
+		error.stack = err.stack;
+
+		console.log( err.stack );
+
+		console.log( err );
+
+		throw error;
 	}
 }
 

+ 12 - 18
packages/ckeditor5-utils/tests/emittermixin.js

@@ -167,7 +167,7 @@ describe( 'EmitterMixin', () => {
 		} );
 
 		it( 'should wrap an error into the CKEditorError if a native error was thrown', () => {
-			const error = new Error( 'foo' );
+			const error = new TypeError( 'foo' );
 			error.stack = 'bar';
 
 			emitter.on( 'test', () => {
@@ -176,13 +176,7 @@ describe( 'EmitterMixin', () => {
 
 			expectToThrowCKEditorError( () => {
 				emitter.fire( 'test' );
-			}, /unexpected-error/, emitter, {
-				originalError: {
-					message: 'foo',
-					stack: 'bar',
-					name: 'Error'
-				}
-			} );
+			}, /bar/ );
 		} );
 
 		describe( 'return value', () => {
@@ -191,7 +185,7 @@ describe( 'EmitterMixin', () => {
 			} );
 
 			it( 'is undefined if none of the listeners modified EventInfo#return', () => {
-				emitter.on( 'foo', () => {} );
+				emitter.on( 'foo', () => { } );
 
 				expect( emitter.fire( 'foo' ) ).to.be.undefined;
 			} );
@@ -422,8 +416,8 @@ describe( 'EmitterMixin', () => {
 		} );
 
 		it( 'should not fail with unknown events', () => {
-			emitter.off( 'foo', () => {} );
-			emitter.off( 'foo:bar', () => {} );
+			emitter.off( 'foo', () => { } );
+			emitter.off( 'foo:bar', () => { } );
 
 			emitter.off( 'foo' );
 			emitter.off( 'foo:bar' );
@@ -707,17 +701,17 @@ describe( 'EmitterMixin', () => {
 		} );
 
 		it( 'should not fail with unknown events', () => {
-			listener.stopListening( emitter, 'foo', () => {} );
-			listener.stopListening( emitter, 'foo:bar', () => {} );
+			listener.stopListening( emitter, 'foo', () => { } );
+			listener.stopListening( emitter, 'foo:bar', () => { } );
 			listener.stopListening( emitter, 'foo' );
 			listener.stopListening( emitter, 'foo:bar' );
 		} );
 
 		it( 'should not fail with unknown emitter', () => {
-			listener.listenTo( emitter, 'foo:bar', () => {} );
+			listener.listenTo( emitter, 'foo:bar', () => { } );
 
-			listener.stopListening( {}, 'foo', () => {} );
-			listener.stopListening( {}, 'foo:bar', () => {} );
+			listener.stopListening( {}, 'foo', () => { } );
+			listener.stopListening( {}, 'foo:bar', () => { } );
 			listener.stopListening( {}, 'foo' );
 			listener.stopListening( {}, 'foo:bar' );
 			listener.stopListening( {} );
@@ -727,7 +721,7 @@ describe( 'EmitterMixin', () => {
 			const spy = sinon.spy();
 
 			listener.listenTo( emitter, 'foo', spy );
-			listener.stopListening( emitter, 'foo', () => {} );
+			listener.stopListening( emitter, 'foo', () => { } );
 
 			emitter.fire( 'foo' );
 
@@ -1347,7 +1341,7 @@ describe( '_getEmitterListenedTo', () => {
 	} );
 
 	it( 'should return emitter with given id', () => {
-		listener.listenTo( emitter, 'eventName', () => {} );
+		listener.listenTo( emitter, 'eventName', () => { } );
 		const emitterId = _getEmitterId( emitter );
 
 		expect( _getEmitterListenedTo( listener, emitterId ) ).to.equal( emitter );