Browse Source

Merge branch 'master' into i/5696

Aleksander Nowodzinski 6 years ago
parent
commit
b8f7e78149

+ 6 - 8
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 );
 			}
 		};
 
@@ -339,7 +337,7 @@ export default class Watchdog {
 	 * Checks if the error comes from the editor that is handled by the watchdog (by checking the error context) and
 	 * restarts the editor. It reacts to {@link module:utils/ckeditorerror~CKEditorError `CKEditorError` errors} only.
 	 *
-	 * @private
+	 * @protected
 	 * @fires error
 	 * @param {Error} error Error.
 	 * @param {ErrorEvent|PromiseRejectionEvent} evt Error event.

+ 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 );
 					} );
 				} );