浏览代码

Added context to thrown `CKEditorError`s.

Maciej Bukowski 6 年之前
父节点
当前提交
d8e642e7de

+ 1 - 1
packages/ckeditor5-core/src/commandcollection.js

@@ -62,7 +62,7 @@ export default class CommandCollection {
 			 * @error commandcollection-command-not-found
 			 * @param {String} commandName Name of the command.
 			 */
-			throw new CKEditorError( 'commandcollection-command-not-found: Command does not exist.', { commandName } );
+			throw new CKEditorError( 'commandcollection-command-not-found: Command does not exist.', this, { commandName } );
 		}
 
 		command.execute( ...args );

+ 4 - 1
packages/ckeditor5-core/src/editor/utils/attachtoform.js

@@ -26,7 +26,10 @@ export default function attachToForm( editor ) {
 		 *
 		 * @error attachtoform-missing-elementapi-interface
 		 */
-		throw new CKEditorError( 'attachtoform-missing-elementapi-interface: Editor passed to attachToForm() must implement ElementApi.' );
+		throw new CKEditorError(
+			'attachtoform-missing-elementapi-interface: Editor passed to attachToForm() must implement ElementApi.',
+			editor
+		);
 	}
 
 	const sourceElement = editor.sourceElement;

+ 1 - 1
packages/ckeditor5-core/src/editor/utils/elementapimixin.js

@@ -30,7 +30,7 @@ const ElementApiMixin = {
 			 *
 			 * @error editor-missing-sourceelement
 			 */
-			throw new CKEditorError( 'editor-missing-sourceelement: Cannot update the source element of a detached editor.' );
+			throw new CKEditorError( 'editor-missing-sourceelement: Cannot update the source element of a detached editor.', null );
 		}
 
 		setDataInElement( this.sourceElement, this.data.get() );

+ 1 - 1
packages/ckeditor5-core/src/pendingactions.js

@@ -99,7 +99,7 @@ export default class PendingActions extends Plugin {
 			 *
 			 * @error pendingactions-add-invalid-message
 			 */
-			throw new CKEditorError( 'pendingactions-add-invalid-message: The message must be a string.' );
+			throw new CKEditorError( 'pendingactions-add-invalid-message: The message must be a string.', this );
 		}
 
 		const action = Object.create( ObservableMixin );

+ 2 - 1
packages/ckeditor5-core/src/plugincollection.js

@@ -119,7 +119,7 @@ export default class PluginCollection {
 				pluginName = key.pluginName || key.name;
 			}
 
-			throw new CKEditorError( errorMsg, { plugin: pluginName } );
+			throw new CKEditorError( errorMsg, this._editor, { plugin: pluginName } );
 		}
 
 		return plugin;
@@ -266,6 +266,7 @@ export default class PluginCollection {
 							throw new CKEditorError(
 								'plugincollection-required: Cannot load a plugin because one of its dependencies is listed in' +
 								'the `removePlugins` option.',
+								editor,
 								{ plugin: RequiredPluginConstructor, requiredBy: PluginConstructor }
 							);
 						}

+ 6 - 3
packages/ckeditor5-core/tests/commandcollection.js

@@ -4,9 +4,9 @@
  */
 
 import CommandCollection from '../src/commandcollection';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import Command from '../src/command';
 import ModelTestEditor from './_utils/modeltesteditor';
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 class SomeCommand extends Command {
 	execute() {}
@@ -56,9 +56,12 @@ describe( 'CommandCollection', () => {
 		} );
 
 		it( 'throws an error if command does not exist', () => {
-			expect( () => {
+			const command = new SomeCommand( editor );
+			collection.add( 'bar', command );
+
+			expectToThrowCKEditorError( () => {
 				collection.execute( 'foo' );
-			} ).to.throw( CKEditorError, /^commandcollection-command-not-found:/ );
+			}, /^commandcollection-command-not-found:/, editor );
 		} );
 	} );
 

+ 3 - 3
packages/ckeditor5-core/tests/editor/editor.js

@@ -11,10 +11,10 @@ import Config from '@ckeditor/ckeditor5-utils/src/config';
 import EditingController from '@ckeditor/ckeditor5-engine/src/controller/editingcontroller';
 import PluginCollection from '../../src/plugincollection';
 import CommandCollection from '../../src/commandcollection';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import Locale from '@ckeditor/ckeditor5-utils/src/locale';
 import Command from '../../src/command';
 import EditingKeystrokeHandler from '../../src/editingkeystrokehandler';
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 class TestEditor extends Editor {
 	static create( config ) {
@@ -368,9 +368,9 @@ describe( 'Editor', () => {
 		it( 'should throw an error if specified command has not been added', () => {
 			const editor = new TestEditor();
 
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				editor.execute( 'command' );
-			} ).to.throw( CKEditorError, /^commandcollection-command-not-found:/ );
+			}, /^commandcollection-command-not-found:/, editor );
 		} );
 	} );
 

+ 6 - 4
packages/ckeditor5-core/tests/editor/utils/attachtoform.js

@@ -7,8 +7,8 @@ import attachToForm from '../../../src/editor/utils/attachtoform';
 import ElementApiMixin from '../../../src/editor/utils/elementapimixin';
 import Editor from '../../../src/editor/editor';
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 /* global document, Event */
 
@@ -47,9 +47,11 @@ describe( 'attachToForm()', () => {
 	} );
 
 	it( 'should throw an error when is used with editor without `ElementApiMixin`', () => {
-		expect( () => {
-			attachToForm( new Editor() );
-		} ).to.throw( CKEditorError, /^attachtoform-missing-elementapi-interface/ );
+		const editor = new Editor();
+
+		expectToThrowCKEditorError( () => {
+			attachToForm( editor );
+		}, /^attachtoform-missing-elementapi-interface/, editor );
 	} );
 
 	it( 'should update editor#element after the "submit" event', () => {

+ 6 - 2
packages/ckeditor5-core/tests/editor/utils/elementapimixin.js

@@ -9,7 +9,7 @@ import ElementApiMixin from '../../../src/editor/utils/elementapimixin';
 import Editor from '../../../src/editor/editor';
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 describe( 'ElementApiMixin', () => {
 	let editor;
@@ -46,7 +46,11 @@ describe( 'ElementApiMixin', () => {
 		} );
 
 		it( 'throws an error if "sourceElement" has not been set', () => {
-			expect( () => editor.updateSourceElement() ).to.throw( CKEditorError, /editor-missing-sourceelement/ );
+			expectToThrowCKEditorError(
+				() => editor.updateSourceElement(),
+				/editor-missing-sourceelement/,
+				editor
+			);
 		} );
 	} );
 } );

+ 3 - 3
packages/ckeditor5-core/tests/pendingactions.js

@@ -5,7 +5,7 @@
 
 import VirtualTestEditor from './_utils/virtualtesteditor';
 import PendingActions from '../src/pendingactions';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 let editor, pendingActions;
 
@@ -69,9 +69,9 @@ describe( 'PendingActions', () => {
 		} );
 
 		it( 'should throw an error when invalid message is given', () => {
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				pendingActions.add( {} );
-			} ).to.throw( CKEditorError, /^pendingactions-add-invalid-message/ );
+			}, /^pendingactions-add-invalid-message/, editor );
 		} );
 
 		it( 'should fire add event with added item', () => {

+ 12 - 12
packages/ckeditor5-core/tests/plugincollection.js

@@ -11,6 +11,7 @@ import PluginCollection from '../src/plugincollection';
 import Plugin from '../src/plugin';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import log from '@ckeditor/ckeditor5-utils/src/log';
+import { expectToThrowCKEditorError, assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 let editor, availablePlugins;
 let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF, PluginG, PluginH, PluginI, PluginJ, PluginK, PluginX, PluginFoo, AnotherPluginFoo;
@@ -344,13 +345,12 @@ describe( 'PluginCollection', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
 			return plugins.init( [ PluginA, PluginB, PluginC, PluginD ], [ PluginA, PluginB ] )
-				// Throw here, so if by any chance plugins.init() was resolved correctly catch() will be stil executed.
+				// Throw here, so if by any chance plugins.init() was resolved correctly catch() will be still executed.
 				.then( () => {
 					throw new Error( 'Test error: this promise should not be resolved successfully' );
 				} )
 				.catch( err => {
-					expect( err ).to.be.an.instanceof( CKEditorError );
-					expect( err.message ).to.match( /^plugincollection-required/ );
+					assertCKEditorError( err, /^plugincollection-required/, editor );
 
 					expect( logSpy.calledTwice ).to.equal( true );
 				} );
@@ -453,9 +453,9 @@ describe( 'PluginCollection', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
 			return plugins.init( [] ).then( () => {
-				expect( () => plugins.get( 'foo' ) )
-					.to.throw( CKEditorError, /^plugincollection-plugin-not-loaded:/ )
-					.with.deep.property( 'data', { plugin: 'foo' } );
+				expectToThrowCKEditorError( () => plugins.get( 'foo' ),
+					/^plugincollection-plugin-not-loaded:/, editor, { plugin: 'foo' }
+				);
 			} );
 		} );
 
@@ -466,9 +466,8 @@ describe( 'PluginCollection', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
 			return plugins.init( [] ).then( () => {
-				expect( () => plugins.get( SomePlugin ) )
-					.to.throw( CKEditorError, /^plugincollection-plugin-not-loaded:/ )
-					.with.deep.property( 'data', { plugin: 'foo' } );
+				expectToThrowCKEditorError( () => plugins.get( SomePlugin ),
+					/^plugincollection-plugin-not-loaded:/, editor, { plugin: 'foo' } );
 			} );
 		} );
 
@@ -478,9 +477,10 @@ describe( 'PluginCollection', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
 			return plugins.init( [] ).then( () => {
-				expect( () => plugins.get( SomePlugin ) )
-					.to.throw( CKEditorError, /^plugincollection-plugin-not-loaded:/ )
-					.with.deep.property( 'data', { plugin: 'SomePlugin' } );
+				expectToThrowCKEditorError( () => plugins.get( SomePlugin ),
+					/^plugincollection-plugin-not-loaded:/,
+					editor, { plugin: 'SomePlugin' }
+				);
 			} );
 		} );
 	} );