Browse Source

Merge pull request #175 from ckeditor/t/174

Other: `ComponentFactory` will throw an error when attempting to create a non-existent component. Closes #174.
Piotrek Koszuliński 8 years ago
parent
commit
7b7f7d50b1

+ 21 - 1
packages/ckeditor5-ui/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 }
 			);
@@ -68,6 +74,20 @@ 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 ) {
+			/**
+			 * 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 UI component in the factory.', { name }
+			);
+		}
+
+		return component( this.editor.locale );
 	}
 }

+ 6 - 0
packages/ckeditor5-ui/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 ) {