Sfoglia il codice sorgente

Make plugincollection.get() to throw error instead of logging warning.

Maciej Gołaszewski 7 anni fa
parent
commit
cad318923e

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

@@ -90,7 +90,7 @@ export default class PluginCollection {
 			 * @error plugincollection-plugin-not-loaded
 			 * @param {String} plugin The name of the plugin which is not loaded.
 			 */
-			const warnMessage = 'plugincollection-plugin-not-loaded: The requested plugin is not loaded.';
+			const errorMsg = 'plugincollection-plugin-not-loaded: The requested plugin is not loaded.';
 
 			let pluginName = key;
 
@@ -98,7 +98,7 @@ export default class PluginCollection {
 				pluginName = key.pluginName || key.name;
 			}
 
-			log.warn( warnMessage, { plugin: pluginName } );
+			throw new CKEditorError( errorMsg, { plugin: pluginName } );
 		}
 
 		return plugin;

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

@@ -449,56 +449,38 @@ describe( 'PluginCollection', () => {
 				} );
 		} );
 
-		it( 'warns if plugin cannot be retrieved by name', () => {
-			const logSpy = testUtils.sinon.stub( log, 'warn' );
-
+		it( 'throws if plugin cannot be retrieved by name', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
 			return plugins.load( [] ).then( () => {
-				const plugin = plugins.get( 'foo' );
-
-				expect( plugin ).to.be.undefined;
-
-				expect( logSpy.calledOnce ).to.equal( true );
-				expect( logSpy.firstCall.args[ 0 ] ).to.match( /^plugincollection-plugin-not-loaded:/ );
-				expect( logSpy.firstCall.args[ 1 ] ).to.deep.equal( { plugin: 'foo' } );
+				expect( () => plugins.get( 'foo' ) )
+					.to.throw( CKEditorError, /^plugincollection-plugin-not-loaded:/ )
+					.with.deep.property( 'data', { plugin: 'foo' } );
 			} );
 		} );
 
-		it( 'warns if plugin cannot be retrieved by class', () => {
+		it( 'throws if plugin cannot be retrieved by class', () => {
 			class SomePlugin extends Plugin {}
 			SomePlugin.pluginName = 'foo';
 
-			const logSpy = testUtils.sinon.stub( log, 'warn' );
-
 			const plugins = new PluginCollection( editor, availablePlugins );
 
 			return plugins.load( [] ).then( () => {
-				const plugin = plugins.get( SomePlugin );
-
-				expect( plugin ).to.be.undefined;
-
-				expect( logSpy.calledOnce ).to.equal( true );
-				expect( logSpy.firstCall.args[ 0 ] ).to.match( /^plugincollection-plugin-not-loaded:/ );
-				expect( logSpy.firstCall.args[ 1 ] ).to.deep.equal( { plugin: 'foo' } );
+				expect( () => plugins.get( SomePlugin ) )
+					.to.throw( CKEditorError, /^plugincollection-plugin-not-loaded:/ )
+					.with.deep.property( 'data', { plugin: 'foo' } );
 			} );
 		} );
 
-		it( 'logs function (class) name if plugin cannot be retrieved by class', () => {
+		it( 'throws if plugin cannot be retrieved by class (class name in error)', () => {
 			class SomePlugin extends Plugin {}
 
-			const logSpy = testUtils.sinon.stub( log, 'warn' );
-
 			const plugins = new PluginCollection( editor, availablePlugins );
 
 			return plugins.load( [] ).then( () => {
-				const plugin = plugins.get( SomePlugin );
-
-				expect( plugin ).to.be.undefined;
-
-				expect( logSpy.calledOnce ).to.equal( true );
-				expect( logSpy.firstCall.args[ 0 ] ).to.match( /^plugincollection-plugin-not-loaded:/ );
-				expect( logSpy.firstCall.args[ 1 ] ).to.deep.equal( { plugin: 'SomePlugin' } );
+				expect( () => plugins.get( SomePlugin ) )
+					.to.throw( CKEditorError, /^plugincollection-plugin-not-loaded:/ )
+					.with.deep.property( 'data', { plugin: 'SomePlugin' } );
 			} );
 		} );
 	} );