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

Merge branch t/ckeditor5-watchdog/1

Internal: Add context to CKEditorError exceptions. Required by ckeditor/ckeditor5-watchdog#2.
Piotr Jasiun 6 лет назад
Родитель
Сommit
9f09f903ad

+ 6 - 2
packages/ckeditor5-ui/src/componentfactory.js

@@ -85,7 +85,9 @@ export default class ComponentFactory {
 			 * @param {String} name The name of the component.
 			 */
 			throw new CKEditorError(
-				'componentfactory-item-exists: The item already exists in the component factory.', { name }
+				'componentfactory-item-exists: The item already exists in the component factory.',
+				this,
+				{ name }
 			);
 		}
 
@@ -113,7 +115,9 @@ export default class ComponentFactory {
 			 * @param {String} name The name of the missing component.
 			 */
 			throw new CKEditorError(
-				'componentfactory-item-missing: The required component is not registered in the factory.', { name }
+				'componentfactory-item-missing: The required component is not registered in the factory.',
+				this,
+				{ name }
 			);
 		}
 

+ 12 - 3
packages/ckeditor5-ui/src/panel/balloon/contextualballoon.js

@@ -199,7 +199,10 @@ export default class ContextualBalloon extends Plugin {
 			 *
 			 * @error contextualballoon-add-view-exist
 			 */
-			throw new CKEditorError( 'contextualballoon-add-view-exist: Cannot add configuration of the same view twice.' );
+			throw new CKEditorError(
+				'contextualballoon-add-view-exist: Cannot add configuration of the same view twice.',
+				[ this, data ]
+			);
 		}
 
 		const stackId = data.stackId || 'main';
@@ -248,7 +251,10 @@ export default class ContextualBalloon extends Plugin {
 			 *
 			 * @error contextualballoon-remove-view-not-exist
 			 */
-			throw new CKEditorError( 'contextualballoon-remove-view-not-exist: Cannot remove the configuration of a non-existent view.' );
+			throw new CKEditorError(
+				'contextualballoon-remove-view-not-exist: Cannot remove the configuration of a non-existent view.',
+				[ this, view ]
+			);
 		}
 
 		const stack = this._viewToStack.get( view );
@@ -313,7 +319,10 @@ export default class ContextualBalloon extends Plugin {
 			 *
 			 * @error contextualballoon-showstack-stack-not-exist
 			 */
-			throw new CKEditorError( 'contextualballoon-showstack-stack-not-exist: Cannot show a stack that does not exist.' );
+			throw new CKEditorError(
+				'contextualballoon-showstack-stack-not-exist: Cannot show a stack that does not exist.',
+				this
+			);
 		}
 
 		if ( this._visibleStack === stack ) {

+ 9 - 3
packages/ckeditor5-ui/src/template.js

@@ -208,7 +208,10 @@ export default class Template {
 			 *
 			 * @error ui-template-revert-not-applied
 			 */
-			throw new CKEditorError( 'ui-template-revert-not-applied: Attempting to revert a template which has not been applied yet.' );
+			throw new CKEditorError(
+				'ui-template-revert-not-applied: Attempting to revert a template which has not been applied yet.',
+				[ this, node ]
+			);
 		}
 
 		this._revertTemplateFromNode( node, this._revertData );
@@ -409,7 +412,8 @@ export default class Template {
 			 * @error ui-template-wrong-syntax
 			 */
 			throw new CKEditorError(
-				'ui-template-wrong-syntax: Node definition must have either "tag" or "text" when rendering a new Node.'
+				'ui-template-wrong-syntax: Node definition must have either "tag" or "text" when rendering a new Node.',
+				this
 			);
 		}
 
@@ -1356,6 +1360,7 @@ function extendObjectValueArray( obj, ext ) {
 //
 // @param {module:ui/template~Template} def A template instance to be extended.
 // @param {module:ui/template~TemplateDefinition} def A definition which is to extend the template instance.
+// @param {Object} Error context.
 function extendTemplate( template, def ) {
 	if ( def.attributes ) {
 		if ( !template.attributes ) {
@@ -1385,7 +1390,8 @@ function extendTemplate( template, def ) {
 			 * @error ui-template-extend-children-mismatch
 			 */
 			throw new CKEditorError(
-				'ui-template-extend-children-mismatch: The number of children in extended definition does not match.'
+				'ui-template-extend-children-mismatch: The number of children in extended definition does not match.',
+				template
 			);
 		}
 

+ 4 - 1
packages/ckeditor5-ui/src/view.js

@@ -467,7 +467,10 @@ export default class View {
 			 *
 			 * @error ui-view-render-rendered
 			 */
-			throw new CKEditorError( 'ui-view-render-already-rendered: This View has already been rendered.' );
+			throw new CKEditorError(
+				'ui-view-render-already-rendered: This View has already been rendered.',
+				this
+			);
 		}
 
 		// Render #element of the view.

+ 4 - 1
packages/ckeditor5-ui/src/viewcollection.js

@@ -154,7 +154,10 @@ export default class ViewCollection extends Collection {
 			 *
 			 * @error ui-viewcollection-delegate-wrong-events
 			 */
-			throw new CKEditorError( 'ui-viewcollection-delegate-wrong-events: All event names must be strings.' );
+			throw new CKEditorError(
+				'ui-viewcollection-delegate-wrong-events: All event names must be strings.',
+				this
+			);
 		}
 
 		return {

+ 8 - 7
packages/ckeditor5-ui/tests/componentfactory.js

@@ -5,7 +5,8 @@
 
 import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
 import ComponentFactory from '../src/componentfactory';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 describe( 'ComponentFactory', () => {
 	let editor, factory;
@@ -41,17 +42,17 @@ describe( 'ComponentFactory', () => {
 		it( 'throws when trying to override already registered component', () => {
 			factory.add( 'foo', () => {} );
 
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				factory.add( 'foo', () => {} );
-			} ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
+			}, /^componentfactory-item-exists/, editor );
 		} );
 
 		it( 'throws when trying to override already registered component added with different case', () => {
 			factory.add( 'Foo', () => {} );
 
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				factory.add( 'foo', () => {} );
-			} ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
+			}, /^componentfactory-item-exists/, editor );
 		} );
 
 		it( 'does not normalize component names', () => {
@@ -63,9 +64,9 @@ describe( 'ComponentFactory', () => {
 
 	describe( 'create()', () => {
 		it( 'throws when trying to create a component which has not been registered', () => {
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				factory.create( 'foo' );
-			} ).to.throw( CKEditorError, /^componentfactory-item-missing/ );
+			}, /^componentfactory-item-missing/, editor );
 		} );
 
 		it( 'creates an instance', () => {

+ 1 - 3
packages/ckeditor5-ui/tests/dropdown/utils.js

@@ -5,7 +5,7 @@
 
 /* globals document Event */
 
-import utilsTestUtils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import { assertBinding } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import Collection from '@ckeditor/ckeditor5-utils/src/collection';
 
@@ -23,8 +23,6 @@ import ListItemView from '../../src/list/listitemview';
 import ListSeparatorView from '../../src/list/listseparatorview';
 import ListView from '../../src/list/listview';
 
-const assertBinding = utilsTestUtils.assertBinding;
-
 describe( 'utils', () => {
 	let locale, dropdownView;
 

+ 17 - 16
packages/ckeditor5-ui/tests/panel/balloon/contextualballoon.js

@@ -7,12 +7,13 @@ import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictest
 import ContextualBalloon from '../../../src/panel/balloon/contextualballoon';
 import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
 import View from '../../../src/view';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 /* global document, Event */
 
@@ -238,7 +239,7 @@ describe( 'ContextualBalloon', () => {
 		} );
 
 		it( 'should throw an error when try to add the same view more than once', () => {
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				balloon.add( {
 					view: viewA,
 					position: {
@@ -246,7 +247,7 @@ describe( 'ContextualBalloon', () => {
 						limiter: balloon.positionLimiter
 					}
 				} );
-			} ).to.throw( CKEditorError, /^contextualballoon-add-view-exist/ );
+			}, /^contextualballoon-add-view-exist/, editor );
 		} );
 
 		it( 'should use a provided limiter instead of #positionLimiter', () => {
@@ -459,9 +460,9 @@ describe( 'ContextualBalloon', () => {
 		} );
 
 		it( 'should throw an error when there is no stack of given id', () => {
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				balloon.showStack( 'second' );
-			} ).to.throw( CKEditorError, /^contextualballoon-showstack-stack-not-exist/ );
+			}, /^contextualballoon-showstack-stack-not-exist/, editor );
 		} );
 	} );
 
@@ -503,9 +504,9 @@ describe( 'ContextualBalloon', () => {
 			balloon.remove( viewB );
 
 			expect( balloon.visibleView ).to.equal( viewA );
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				balloon.showStack( 'second' );
-			} ).to.throw();
+			}, /^contextualballoon-showstack-stack-not-exist/, editor );
 		} );
 
 		it( 'should switch stack to the next one when removed view was the last one in the visible stack', () => {
@@ -517,9 +518,9 @@ describe( 'ContextualBalloon', () => {
 			balloon.remove( viewA );
 
 			expect( balloon.visibleView ).to.equal( viewB );
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				balloon.showStack( 'main' );
-			} ).to.throw();
+			}, /^contextualballoon-showstack-stack-not-exist/, editor );
 		} );
 
 		it( 'should remove given view and set preceding in the stack as visible when removed view was visible', () => {
@@ -592,15 +593,15 @@ describe( 'ContextualBalloon', () => {
 			expect( balloon.hasView( viewB ) ).to.false;
 
 			// Does throw, so the stack is not there.
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				balloon.showStack( 'second' );
-			} ).to.throw();
+			}, /^contextualballoon-showstack-stack-not-exist/, editor );
 		} );
 
 		it( 'should throw an error when there is no given view in the stack', () => {
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				balloon.remove( viewB );
-			} ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
+			}, /^contextualballoon-remove-view-not-exist/, editor );
 		} );
 
 		it( 'should set additional css class of visible view to BalloonPanelView', () => {
@@ -707,9 +708,9 @@ describe( 'ContextualBalloon', () => {
 		} );
 
 		it( 'should throw an error when there is no given view in the stack', () => {
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				balloon.remove( viewB );
-			} ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
+			}, /^contextualballoon-remove-view-not-exist/, editor );
 		} );
 	} );
 
@@ -718,7 +719,7 @@ describe( 'ContextualBalloon', () => {
 			expect( () => {
 				balloon.destroy();
 				balloon.destroy();
-			} ).to.not.throw();
+			} );
 		} );
 
 		it( 'should not touch the DOM', () => {

+ 19 - 17
packages/ckeditor5-ui/tests/template.js

@@ -10,12 +10,14 @@ import { default as Template, TemplateToBinding, TemplateIfBinding } from '../sr
 import View from '../src/view';
 import ViewCollection from '../src/viewcollection';
 import Model from '../src/model';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+
 import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
 import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
 import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
 import log from '@ckeditor/ckeditor5-utils/src/log';
 
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+
 let el, text;
 const injectedElements = [];
 
@@ -142,16 +144,16 @@ describe( 'Template', () => {
 
 	describe( 'render()', () => {
 		it( 'throws when the template definition is wrong', () => {
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				new Template( {} ).render();
-			} ).to.throw( CKEditorError, /ui-template-wrong-syntax/ );
+			}, /ui-template-wrong-syntax/ );
 
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				new Template( {
 					tag: 'p',
 					text: 'foo'
 				} ).render();
-			} ).to.throw( CKEditorError, /ui-template-wrong-syntax/ );
+			}, /ui-template-wrong-syntax/ );
 		} );
 
 		it( 'sets #_isRendered true', () => {
@@ -727,12 +729,12 @@ describe( 'Template', () => {
 		} );
 
 		it( 'throws when wrong template definition', () => {
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				new Template( {
 					tag: 'p',
 					text: 'foo'
 				} ).apply( el );
-			} ).to.throw( CKEditorError, /ui-template-wrong-syntax/ );
+			}, /ui-template-wrong-syntax/ );
 		} );
 
 		it( 'accepts empty template definition', () => {
@@ -1119,15 +1121,15 @@ describe( 'Template', () => {
 				tag: 'div'
 			} );
 
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				tpl.revert( el );
-			} ).to.throw( CKEditorError, /ui-template-revert-not-applied/ );
+			}, /ui-template-revert-not-applied/ );
 
 			tpl.render();
 
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				tpl.revert( el );
-			} ).to.throw( CKEditorError, /ui-template-revert-not-applied/ );
+			}, /ui-template-revert-not-applied/ );
 		} );
 
 		describe( 'text', () => {
@@ -2691,7 +2693,7 @@ describe( 'Template', () => {
 
 		describe( 'children', () => {
 			it( 'should throw when the number of children does not correspond', () => {
-				expect( () => {
+				expectToThrowCKEditorError( () => {
 					extensionTest(
 						{
 							tag: 'p',
@@ -2707,11 +2709,11 @@ describe( 'Template', () => {
 						},
 						'it should fail'
 					);
-				} ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
+				}, /ui-template-extend-children-mismatch/ );
 			} );
 
 			it( 'should throw when no children in target but extending one', () => {
-				expect( () => {
+				expectToThrowCKEditorError( () => {
 					extensionTest(
 						{
 							tag: 'p',
@@ -2725,11 +2727,11 @@ describe( 'Template', () => {
 						},
 						'it should fail'
 					);
-				} ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
+				}, /ui-template-extend-children-mismatch/ );
 			} );
 
 			it( 'should throw when the number of children does not correspond on some deeper level', () => {
-				expect( () => {
+				expectToThrowCKEditorError( () => {
 					extensionTest(
 						{
 							tag: 'p',
@@ -2768,7 +2770,7 @@ describe( 'Template', () => {
 						},
 						'it should fail'
 					);
-				} ).to.throw( CKEditorError, /ui-template-extend-children-mismatch/ );
+				}, /ui-template-extend-children-mismatch/ );
 			} );
 
 			it( 'extends existing - simple', () => {

+ 4 - 3
packages/ckeditor5-ui/tests/view.js

@@ -8,10 +8,11 @@
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import View from '../src/view';
 import Template from '../src/template';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+
 import Collection from '@ckeditor/ckeditor5-utils/src/collection';
 import ViewCollection from '../src/viewcollection';
 import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
+import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 let TestView, view, childA, childB;
 
@@ -215,8 +216,8 @@ describe( 'View', () => {
 				view.render();
 				throw new Error( 'This should not be executed.' );
 			} catch ( err ) {
-				expect( err ).to.be.instanceof( CKEditorError );
-				expect( err.message ).to.match( /^ui-view-render-already-rendered:/ );
+				// TODO
+				assertCKEditorError( err, /^ui-view-render-already-rendered:/, view );
 			}
 		} );
 

+ 7 - 7
packages/ckeditor5-ui/tests/viewcollection.js

@@ -5,11 +5,11 @@
 
 /* global document */
 
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import View from '../src/view';
 import ViewCollection from '../src/viewcollection';
 import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 let collection;
 
@@ -169,17 +169,17 @@ describe( 'ViewCollection', () => {
 
 	describe( 'delegate()', () => {
 		it( 'should throw when event names are not strings', () => {
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				collection.delegate();
-			} ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
+			}, /ui-viewcollection-delegate-wrong-events/ );
 
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				collection.delegate( new Date() );
-			} ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
+			}, /ui-viewcollection-delegate-wrong-events/ );
 
-			expect( () => {
+			expectToThrowCKEditorError( () => {
 				collection.delegate( 'color', new Date() );
-			} ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
+			}, /ui-viewcollection-delegate-wrong-events/ );
 		} );
 
 		it( 'returns object', () => {