Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #29 from ckeditor/i/5678
Browse files Browse the repository at this point in the history
Fix: Only instances of the `Error` class will be handled by`Watchdog`. Closes ckeditor/ckeditor5#5678.
  • Loading branch information
Reinmar committed Nov 5, 2019
2 parents 17d2213 + 7f9693e commit 3f24a2e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 6 additions & 8 deletions src/watchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
};

Expand Down Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion tests/watchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} );
} );
Expand Down

0 comments on commit 3f24a2e

Please sign in to comment.