Procházet zdrojové kódy

Other: Throw an error when attempting to create a component not registered in ComponentFactory. Closes #174.

Aleksander Nowodzinski před 8 roky
rodič
revize
0d914fe012

+ 9 - 1
packages/ckeditor5-ui/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 );
 	}
 }

+ 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 ) {