浏览代码

Moved common error handling part to the `CKEditorError` class.

Maciej Bukowski 6 年之前
父节点
当前提交
8025d041c9

+ 2 - 34
packages/ckeditor5-engine/src/model/model.js

@@ -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 );
 		}
 	}
 
@@ -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 );
 		}
 	}
 

+ 1 - 18
packages/ckeditor5-engine/src/view/view.js

@@ -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 ) {
@@ -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 );
 		}
 	}
 

+ 3 - 5
packages/ckeditor5-engine/tests/model/model.js

@@ -332,7 +332,7 @@ describe( 'Model', () => {
 				model.change( () => {
 					throw error;
 				} );
-			}, /model-change-unexpected-error/, model, {
+			}, /unexpected-error/, model, {
 				originalError: {
 					message: 'foo',
 					stack: 'bar',
@@ -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 } );
 		} );
@@ -359,7 +357,7 @@ describe( 'Model', () => {
 				model.enqueueChange( () => {
 					throw error;
 				} );
-			}, /model-enqueueChange-unexpected-error/, model, {
+			}, /unexpected-error/, model, {
 				originalError: {
 					message: 'foo',
 					stack: 'bar',

+ 2 - 5
packages/ckeditor5-engine/tests/view/view/view.js

@@ -812,7 +812,7 @@ describe( 'view', () => {
 				view.change( () => {
 					throw error;
 				} );
-			}, /view-change-unexpected-error/, view, {
+			}, /unexpected-error/, view, {
 				originalError: {
 					message: 'foo',
 					stack: 'bar',
@@ -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 );