8
0
Pārlūkot izejas kodu

Only `Error` instances should be checked deeper.

Maciej Bukowski 6 gadi atpakaļ
vecāks
revīzija
8e1be31c10

+ 5 - 7
packages/ckeditor5-watchdog/src/watchdog.js

@@ -89,14 +89,12 @@ export default class Watchdog {
 		 */
 		this._boundErrorHandler = evt => {
 			// `evt.error` is exposed by EventError while `evt.reason` is available in PromiseRejectionEvent.
+			const error = evt.error || evt.reason;
 
-			if ( evt.reason ) {
-				// Note that evt.reason might be everything that is in the promise rejection.
-				if ( evt.reason instanceof Error ) {
-					this._handleError( evt.reason, evt );
-				}
-			} else {
-				this._handleError( evt.error, evt );
+			// Note that `evt.reason` might be everything that is in the promise rejection.
+			// Similarly everything that is thrown lands in `evt.error`.
+			if ( error instanceof Error ) {
+				this._handleError( error, evt );
 			}
 		};
 

+ 14 - 1
packages/ckeditor5-watchdog/tests/watchdog.js

@@ -244,24 +244,37 @@ describe( 'Watchdog', () => {
 			const editorErrorSpy = sinon.spy();
 			watchdog.on( 'error', editorErrorSpy );
 
+			const watchdogErrorHandlerSpy = sinon.spy( watchdog, '_handleError' );
+
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
 			return watchdog.create( element ).then( () => {
+				const error = new Error( 'foo' );
+
 				setTimeout( () => {
-					throw new Error( 'foo' );
+					throw error;
 				} );
+
 				setTimeout( () => {
 					throw 'bar';
 				} );
 
+				setTimeout( () => {
+					throw null;
+				} );
+
 				return new Promise( res => {
 					setTimeout( () => {
 						window.onerror = originalErrorHandler;
 
 						sinon.assert.notCalled( editorErrorSpy );
 
+						// Assert that only instances of the `Error` class will be checked deeper.
+						sinon.assert.calledOnce( watchdogErrorHandlerSpy );
+						expect( watchdogErrorHandlerSpy.getCall( 0 ).args[ 0 ] ).to.equal( error );
+
 						watchdog.destroy().then( res );
 					} );
 				} );