Ver código fonte

Added context to `CKEditorError`, changed `isCKEditorError`()` method to `is()`.

Maciej Bukowski 6 anos atrás
pai
commit
6ba104e8e9

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

@@ -32,11 +32,13 @@ export default class CKEditorError extends Error {
 	 * @param {String} message The error message in an `error-name: Error message.` format.
 	 * During the minification process the "Error message" part will be removed to limit the code size
 	 * and a link to this error documentation will be added to the `message`.
+	 * @param {Object} context A context of the error by which the {@link module:watchdog/watchdog~Watchdog watchdog}
+	 * is able to determine which editor crashed. It should be an editor instance or a property connected to it.
 	 * @param {Object} [data] Additional data describing the error. A stringified version of this object
 	 * will be appended to the error message, so the data are quickly visible in the console. The original
 	 * data object will also be later available under the {@link #data} property.
 	 */
-	constructor( message, data ) {
+	constructor( message, context, data ) {
 		message = attachLinkToDocumentation( message );
 
 		if ( data ) {
@@ -46,26 +48,30 @@ export default class CKEditorError extends Error {
 		super( message );
 
 		/**
-		 * @member {String}
+		 * @type {String}
 		 */
 		this.name = 'CKEditorError';
 
 		/**
+		 * A context of the error by which the Watchdog is able to determine which editor crashed.
+		 *
+		 * @type {Object}
+		 */
+		this.context = context;
+
+		/**
 		 * The additional error data passed to the constructor. Undefined if none was passed.
 		 *
-		 * @member {Object|undefined}
+		 * @type {Object|undefined}
 		 */
 		this.data = data;
 	}
 
 	/**
-	 * Checks if error is an instance of CKEditorError class.
-	 *
-	 * @param {Object} error Object to check.
-	 * @returns {Boolean}
+	 * Checks if the error is of the `CKEditorError` type.
 	 */
-	static isCKEditorError( error ) {
-		return error instanceof CKEditorError;
+	is( type ) {
+		return type === 'CKEditorError';
 	}
 }
 

+ 19 - 11
packages/ckeditor5-utils/tests/ckeditorerror.js

@@ -7,20 +7,20 @@ import { default as CKEditorError, DOCUMENTATION_URL } from '../src/ckeditorerro
 
 describe( 'CKEditorError', () => {
 	it( 'inherits from Error', () => {
-		const error = new CKEditorError( 'foo' );
+		const error = new CKEditorError( 'foo', {} );
 
 		expect( error ).to.be.an.instanceOf( Error );
 		expect( error ).to.be.an.instanceOf( CKEditorError );
 	} );
 
 	it( 'sets the name', () => {
-		const error = new CKEditorError( 'foo' );
+		const error = new CKEditorError( 'foo', {} );
 
 		expect( error ).to.have.property( 'name', 'CKEditorError' );
 	} );
 
 	it( 'sets the message', () => {
-		const error = new CKEditorError( 'foo' );
+		const error = new CKEditorError( 'foo', {} );
 
 		expect( error ).to.have.property( 'message', 'foo' );
 		expect( error.data ).to.be.undefined;
@@ -28,12 +28,20 @@ describe( 'CKEditorError', () => {
 
 	it( 'sets the message and data', () => {
 		const data = { bar: 1 };
-		const error = new CKEditorError( 'foo', data );
+		const error = new CKEditorError( 'foo', {}, data );
 
 		expect( error ).to.have.property( 'message', 'foo {"bar":1}' );
 		expect( error ).to.have.property( 'data', data );
 	} );
 
+	it( 'sets the context of the error', () => {
+		const data = { bar: 1 };
+		const editor = {};
+		const error = new CKEditorError( 'foo', editor, data );
+
+		expect( error.context ).to.equal( editor );
+	} );
+
 	it( 'appends stringified data to the message', () => {
 		class Foo {
 			constructor() {
@@ -46,14 +54,14 @@ describe( 'CKEditorError', () => {
 			bom: new Foo(),
 			bim: 10
 		};
-		const error = new CKEditorError( 'foo', data );
+		const error = new CKEditorError( 'foo', {}, data );
 
 		expect( error ).to.have.property( 'message', 'foo {"bar":"a","bom":{"x":1},"bim":10}' );
 		expect( error ).to.have.property( 'data', data );
 	} );
 
 	it( 'contains a link which leads to the documentation', () => {
-		const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.' );
+		const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.', {} );
 
 		const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
 			`Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n`;
@@ -62,7 +70,7 @@ describe( 'CKEditorError', () => {
 	} );
 
 	it( 'link to documentation is added before the additional data message', () => {
-		const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.', { foo: 1, bar: 2 } );
+		const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.', {}, { foo: 1, bar: 2 } );
 
 		const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
 			`Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n ` +
@@ -71,13 +79,13 @@ describe( 'CKEditorError', () => {
 		expect( error ).to.have.property( 'message', errorMessage );
 	} );
 
-	describe( 'isCKEditorError', () => {
+	describe( 'is()', () => {
 		it( 'checks if error is an instance of CKEditorError', () => {
-			const ckeditorError = new CKEditorError( 'foo' );
+			const ckeditorError = new CKEditorError( 'foo', {} );
 			const regularError = new Error( 'foo' );
 
-			expect( CKEditorError.isCKEditorError( ckeditorError ) ).to.be.true;
-			expect( CKEditorError.isCKEditorError( regularError ) ).to.be.false;
+			expect( ( !!ckeditorError.is && ckeditorError.is( 'CKEditorError' ) ) ).to.be.true;
+			expect( ( !!regularError.is && regularError.is( 'CKEditorError' ) ) ).to.be.false;
 		} );
 	} );
 } );