Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #199 from ckeditor/t/ckeditor5/1304
Browse files Browse the repository at this point in the history
Other: Added custom error handling to the `editor.execute()` method. Part of ckeditor/ckeditor5#1304.
  • Loading branch information
Piotr Jasiun authored Oct 9, 2019
2 parents 9dc2c2a + 5dce70b commit c1babca
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
}
}

/**
Expand Down
51 changes: 51 additions & 0 deletions tests/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -114,6 +115,8 @@ describe( 'Editor', () => {
afterEach( () => {
delete TestEditor.builtinPlugins;
delete TestEditor.defaultConfig;

sinon.restore();
} );

it( 'imports the version helper', () => {
Expand Down Expand Up @@ -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()', () => {
Expand Down

0 comments on commit c1babca

Please sign in to comment.