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

Commit

Permalink
Moved common error handling part to the CKEditorError class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2ciek committed Oct 7, 2019
1 parent 9a563c5 commit ae2d706
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 62 deletions.
36 changes: 2 additions & 34 deletions src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,7 @@ export default class Model {
return callback( this._currentWriter );
}
} catch ( err ) {
if ( err.is && err.is( 'CKEditorError' ) ) {
throw err;
}

/**
* An unexpected error occurred inside the `model.change()` block. The `error.data.originalError` property
* shows the original error properties.
*
* @error model-change-unexpected-error
*/
throw new CKEditorError( 'model-change-unexpected-error', this, {
originalError: {
message: err.message,
stack: err.stack,
name: err.name
}
} );
CKEditorError.rethrowUnexpectedError( err, this );
}
}

Expand Down Expand Up @@ -233,23 +217,7 @@ export default class Model {
this._runPendingChanges();
}
} catch ( err ) {
if ( err.is && err.is( 'CKEditorError' ) ) {
throw err;
}

/**
* An unexpected error occurred inside the `model.enqueueChange()` block. The `error.data.originalError` property
* shows the original error properties.
*
* @error model-enqueueChange-unexpected-error
*/
throw new CKEditorError( 'model-enqueueChange-unexpected-error', this, {
originalError: {
message: err.message,
stack: err.stack,
name: err.name
}
} );
CKEditorError.rethrowUnexpectedError( err, this );
}
}

Expand Down
19 changes: 1 addition & 18 deletions src/view/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ export default class View {
);
}

// Use try-catch to catch the
try {
// Recursive call to view.change() method - execute listener immediately.
if ( this._ongoingChange ) {
Expand All @@ -483,23 +482,7 @@ export default class View {

return callbackResult;
} catch ( err ) {
if ( err.is && err.is( 'CKEditorError' ) ) {
throw err;
}

/**
* An unexpected error occurred inside the `view.change()` block. The `error.data.originalError` property
* shows the original error properties.
*
* @error view-change-unexpected-error
*/
throw new CKEditorError( 'view-change-unexpected-error', this, {
originalError: {
message: err.message,
stack: err.stack,
name: err.name
}
} );
CKEditorError.rethrowUnexpectedError( err, this );
}
}

Expand Down
8 changes: 3 additions & 5 deletions tests/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ describe( 'Model', () => {
model.change( () => {
throw error;
} );
}, /model-change-unexpected-error/, model, {
}, /unexpected-error/, model, {
originalError: {
message: 'foo',
stack: 'bar',
Expand All @@ -342,11 +342,9 @@ describe( 'Model', () => {
} );

it( 'should throw the original CKEditorError error if it was thrown inside the `change()` block', () => {
const err = new CKEditorError( 'foo', null, { foo: 1 } );

expectToThrowCKEditorError( () => {
model.change( () => {
throw err;
throw new CKEditorError( 'foo', null, { foo: 1 } );
} );
}, /foo/, null, { foo: 1 } );
} );
Expand All @@ -359,7 +357,7 @@ describe( 'Model', () => {
model.enqueueChange( () => {
throw error;
} );
}, /model-enqueueChange-unexpected-error/, model, {
}, /unexpected-error/, model, {
originalError: {
message: 'foo',
stack: 'bar',
Expand Down
7 changes: 2 additions & 5 deletions tests/view/view/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ describe( 'view', () => {
view.change( () => {
throw error;
} );
}, /view-change-unexpected-error/, view, {
}, /unexpected-error/, view, {
originalError: {
message: 'foo',
stack: 'bar',
Expand All @@ -821,10 +821,7 @@ describe( 'view', () => {
} );
} );

it( 'should re-throw custom CKEditorError errors', () => {
const error = new TypeError( 'foo' );
error.stack = 'bar';

it( 'should rethrow custom CKEditorError errors', () => {
expectToThrowCKEditorError( () => {
view.change( () => {
throw new CKEditorError( 'foo', view );
Expand Down

0 comments on commit ae2d706

Please sign in to comment.