8
0
Просмотр исходного кода

Added error handling to the `editor.execute()` method.

Maciej Bukowski 6 лет назад
Родитель
Сommit
d7dba0f30b

+ 6 - 1
packages/ckeditor5-core/src/editor/editor.js

@@ -19,6 +19,7 @@ import EditingKeystrokeHandler from '../editingkeystrokehandler';
 
 import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 /**
  * Class representing a basic, generic editor.
@@ -270,7 +271,11 @@ export default class Editor {
 	 * @param {*} [...commandParams] Command parameters.
 	 */
 	execute( ...args ) {
-		this.commands.execute( ...args );
+		try {
+			this.commands.execute( ...args );
+		} catch ( err ) {
+			CKEditorError.rethrowUnexpectedError( err, this );
+		}
 	}
 
 	/**

+ 51 - 0
packages/ckeditor5-core/tests/editor/editor.js

@@ -15,6 +15,7 @@ import Locale from '@ckeditor/ckeditor5-utils/src/locale';
 import Command from '../../src/command';
 import EditingKeystrokeHandler from '../../src/editingkeystrokehandler';
 import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 class TestEditor extends Editor {
 	static create( config ) {
@@ -114,6 +115,8 @@ describe( 'Editor', () => {
 	afterEach( () => {
 		delete TestEditor.builtinPlugins;
 		delete TestEditor.defaultConfig;
+
+		sinon.restore();
 	} );
 
 	it( 'imports the version helper', () => {
@@ -387,6 +390,54 @@ describe( 'Editor', () => {
 				editor.execute( 'command' );
 			}, /^commandcollection-command-not-found:/, editor );
 		} );
+
+		it( 'should catch native errors and wrap them into the CKEditorError errors', () => {
+			const editor = new TestEditor();
+			const error = new TypeError( 'foo' );
+			error.stack = 'bar';
+
+			class SomeCommand extends Command {
+				constructor( editor ) {
+					super( editor );
+					this.isEnabled = true;
+				}
+				execute() {
+					throw error;
+				}
+			}
+
+			editor.commands.add( 'someCommand', new SomeCommand( editor ) );
+
+			expectToThrowCKEditorError( () => {
+				editor.execute( 'someCommand' );
+			}, /unexpected-error/, editor, {
+				originalError: {
+					message: 'foo',
+					stack: 'bar',
+					name: 'TypeError'
+				}
+			} );
+		} );
+
+		it( 'should rethrow custom CKEditorError errors', () => {
+			const editor = new TestEditor();
+
+			class SomeCommand extends Command {
+				constructor( editor ) {
+					super( editor );
+					this.isEnabled = true;
+				}
+				execute() {
+					throw new CKEditorError( 'foo', editor );
+				}
+			}
+
+			editor.commands.add( 'someCommand', new SomeCommand( editor ) );
+
+			expectToThrowCKEditorError( () => {
+				editor.execute( 'someCommand' );
+			}, /foo/, editor );
+		} );
 	} );
 
 	describe( 'create()', () => {