8
0
Эх сурвалжийг харах

Merge pull request #41 from cksource/t/40

Made PluginCollection forward the original error.
Piotrek Koszuliński 10 жил өмнө
parent
commit
a4c7853841

+ 3 - 3
packages/ckeditor5-engine/src/plugincollection.js

@@ -12,7 +12,7 @@
  * @extends Collection
  */
 
-CKEDITOR.define( [ 'collection', 'promise', 'ckeditorerror' ], function( Collection, Promise, CKEditorError ) {
+CKEDITOR.define( [ 'collection', 'promise', 'log' ], function( Collection, Promise, log ) {
 	class PluginCollection extends Collection {
 		/**
 		 * Creates an instance of the PluginCollection class, initializing it with a set of plugins.
@@ -76,14 +76,14 @@ CKEDITOR.define( [ 'collection', 'promise', 'ckeditorerror' ], function( Collect
 							);
 						},
 						// Error callback.
-						function() {
+						function( err ) {
 							/**
 							 * It was not possible to load the plugin.
 							 *
 							 * @error plugincollection-load
 							 * @param {String} plugin The name of the plugin that could not be loaded.
 							 */
-							var err = new CKEditorError( 'plugincollection-load: It was not possible to load the plugin.', { plugin: plugin } );
+							log.error( 'plugincollection-load: It was not possible to load the plugin.', { plugin: plugin } );
 							reject( err );
 						}
 					);

+ 42 - 5
packages/ckeditor5-engine/tests/plugincollection/plugincollection.js

@@ -7,9 +7,13 @@
 
 'use strict';
 
-var modules = bender.amd.require( 'plugincollection', 'plugin', 'editor', 'ckeditorerror' );
+var modules = bender.amd.require( 'plugincollection', 'plugin', 'editor', 'log' );
 var editor;
 var PluginA, PluginB;
+class TestError extends Error {}
+
+var sandbox = sinon.sandbox.create();
+afterEach( sandbox.restore.bind( sandbox ) );
 
 before( function() {
 	var Editor = modules.editor;
@@ -47,6 +51,10 @@ CKEDITOR.define( 'plugin!F', [ 'plugin', 'plugin!E' ], function( Plugin ) {
 	return class extends Plugin {};
 } );
 
+CKEDITOR.define( 'plugin!G', function() {
+	throw new TestError( 'Some error inside a plugin' );
+} );
+
 /////////////
 
 describe( 'load', function() {
@@ -167,19 +175,48 @@ describe( 'load', function() {
 			} );
 	} );
 
-	it( 'should throw an error for invalid plugins', function() {
+	it( 'should reject on invalid plugin names (forward require.js loading error)', function() {
 		var PluginCollection = modules.plugincollection;
-		var CKEditorError = modules.ckeditorerror;
+		var log = modules.log;
+
+		var logSpy = sandbox.stub( log, 'error' );
 
 		var plugins = new PluginCollection( editor );
 
 		return plugins.load( 'A,BAD,B' )
+			// Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
+			.then( function() {
+				throw new Error( 'Test error: this promise should not be resolved successfully' );
+			} )
+			.catch( function( err ) {
+				expect( err ).to.be.an.instanceof( Error );
+				// Make sure it's the Require.JS error, not the one thrown above.
+				expect( err.message ).to.match( /^Script error for:/ );
+
+				sinon.assert.calledOnce( logSpy );
+				expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
+			} );
+	} );
+
+	it( 'should reject on broken plugins (forward the error thrown in a plugin)', function() {
+		var PluginCollection = modules.plugincollection;
+		var log = modules.log;
+
+		var logSpy = sandbox.stub( log, 'error' );
+
+		var plugins = new PluginCollection( editor );
+
+		return plugins.load( 'A,G,B' )
+			// Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
 			.then( function() {
 				throw new Error( 'Test error: this promise should not be resolved successfully' );
 			} )
 			.catch( function( err ) {
-				expect( err ).to.be.an.instanceof( CKEditorError );
-				expect( err.data ).to.have.property( 'plugin', 'BAD' );
+				expect( err ).to.be.an.instanceof( TestError );
+				expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
+
+				sinon.assert.calledOnce( logSpy );
+				expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
 			} );
 	} );
 } );