From 4b0b394c16fbea0b4b7f4d3427f569e3a7e652ed Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Mon, 27 Mar 2017 10:32:59 +0200 Subject: [PATCH 1/2] Other: Throw an error when attempting to create a component not registered in ComponentFactory. Closes #174. --- src/componentfactory.js | 10 +++++++++- tests/componentfactory.js | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/componentfactory.js b/src/componentfactory.js index e93c590b..de50dc1d 100644 --- a/src/componentfactory.js +++ b/src/componentfactory.js @@ -68,6 +68,14 @@ export default class ComponentFactory { * @returns {module:ui/view~View} The instantiated component view. */ create( name ) { - return this._components.get( name )( this.editor.locale ); + const component = this._components.get( name ); + + if ( !component ) { + throw new CKEditorError( + 'componentfactory-item-missing: There is no such component in the factory.', { name } + ); + } + + return component( this.editor.locale ); } } diff --git a/tests/componentfactory.js b/tests/componentfactory.js index 550d8a70..eec57b26 100644 --- a/tests/componentfactory.js +++ b/tests/componentfactory.js @@ -32,6 +32,12 @@ describe( 'ComponentFactory', () => { } ); describe( 'create', () => { + it( 'throws when trying to create a component which has not been registered', () => { + expect( () => { + factory.create( 'foo' ); + } ).to.throw( CKEditorError, /^componentfactory-item-missing/ ); + } ); + it( 'creates an instance', () => { class View { constructor( locale ) { From 28ee3dfdaf7cc7580f7bfe4d09bedddd268abad1 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Mon, 27 Mar 2017 10:57:31 +0200 Subject: [PATCH 2/2] Docs: Improved error docs in the ComponentFactory class. --- src/componentfactory.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/componentfactory.js b/src/componentfactory.js index de50dc1d..05e2b9b5 100644 --- a/src/componentfactory.js +++ b/src/componentfactory.js @@ -53,6 +53,12 @@ export default class ComponentFactory { */ add( name, callback ) { if ( this._components.get( name ) ) { + /** + * The item already exists in the component factory. + * + * @error componentfactory-item-exists + * @param {String} name The name of the component. + */ throw new CKEditorError( 'componentfactory-item-exists: The item already exists in the component factory.', { name } ); @@ -71,8 +77,14 @@ export default class ComponentFactory { const component = this._components.get( name ); if ( !component ) { + /** + * There is no such UI component in the factory. + * + * @error componentfactory-item-missing + * @param {String} name The name of the missing component. + */ throw new CKEditorError( - 'componentfactory-item-missing: There is no such component in the factory.', { name } + 'componentfactory-item-missing: There is no such UI component in the factory.', { name } ); }