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 #313 from ckeditor/i/5595
Browse files Browse the repository at this point in the history
Fix: Improved error rethrowing by replacing the error stack. Closes ckeditor/ckeditor5#5595.
  • Loading branch information
Reinmar authored Nov 25, 2019
2 parents 5ec2518 + 7f3110c commit 7685c0d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
24 changes: 12 additions & 12 deletions src/ckeditorerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/emittermixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ const EmitterMixin = {

return eventInfo.return;
} catch ( err ) {
// @if CK_DEBUG // throw err;
/* istanbul ignore next */
CKEditorError.rethrowUnexpectedError( err, this );
}
},
Expand Down
8 changes: 1 addition & 7 deletions tests/ckeditorerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,7 @@ describe( 'CKEditorError', () => {

expectToThrowCKEditorError( () => {
CKEditorError.rethrowUnexpectedError( error, context );
}, /unexpected-error/, context, {
originalError: {
message: 'foo',
stack: 'bar',
name: 'Error'
}
} );
}, /foo/, context );
} );
} );
} );
15 changes: 4 additions & 11 deletions tests/emittermixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 7685c0d

Please sign in to comment.