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

Added support for `null` context. Improved API docs.

Maciej Bukowski 6 лет назад
Родитель
Сommit
0fd24eef56
2 измененных файлов с 47 добавлено и 1 удалено
  1. 19 1
      packages/ckeditor5-watchdog/src/watchdog.js
  2. 28 0
      packages/ckeditor5-watchdog/tests/watchdog.js

+ 19 - 1
packages/ckeditor5-watchdog/src/watchdog.js

@@ -36,6 +36,18 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  *		watchdog.setDestructor( editor => editor.destroy() );
  *		watchdog.setDestructor( editor => editor.destroy() );
  *
  *
  *		watchdog.create( elementOrData, editorConfig ).then( editor => {} );
  *		watchdog.create( elementOrData, editorConfig ).then( editor => {} );
+ *
+ * Other important APIs:
+ *
+ *		watchdog.on( 'crash', () => { console.log( 'Editor crashed.' ) } );
+ *		watchdog.on( 'restart', () => { console.log( 'Editor was restarted.' ) } );
+ *
+ *		watchdog.restart(); // Restarts the editor.
+ *		watchdog.destroy(); // Destroys the editor and global error event listeners.
+ *
+ * 		watchdog.editor; // Current editor instance.
+ *
+ * 		watchdog.crashes.forEach( crashInfo => console.log( crashInfo ) );
  */
  */
 export default class Watchdog {
 export default class Watchdog {
 	/**
 	/**
@@ -308,12 +320,17 @@ export default class Watchdog {
 			return;
 			return;
 		}
 		}
 
 
-		if ( !event.error.context ) {
+		if ( event.error.context === undefined ) {
 			console.error( 'The error is missing its context and Watchdog cannot restart the proper editor.' );
 			console.error( 'The error is missing its context and Watchdog cannot restart the proper editor.' );
 
 
 			return;
 			return;
 		}
 		}
 
 
+		// In some cases the editor should not be restarted - e.g. in case of the editor initialization.
+		if ( event.error.context === null ) {
+			return;
+		}
+
 		if ( this._isErrorComingFromThisEditor( event.error ) ) {
 		if ( this._isErrorComingFromThisEditor( event.error ) ) {
 			this.crashes.push( {
 			this.crashes.push( {
 				message: event.error.message,
 				message: event.error.message,
@@ -352,6 +369,7 @@ export default class Watchdog {
 	 */
 	 */
 	static for( Editor ) {
 	static for( Editor ) {
 		const watchdog = new Watchdog();
 		const watchdog = new Watchdog();
+
 		watchdog.setCreator( ( elementOrData, config ) => Editor.create( elementOrData, config ) );
 		watchdog.setCreator( ( elementOrData, config ) => Editor.create( elementOrData, config ) );
 		watchdog.setDestructor( editor => editor.destroy() );
 		watchdog.setDestructor( editor => editor.destroy() );
 
 

+ 28 - 0
packages/ckeditor5-watchdog/tests/watchdog.js

@@ -481,6 +481,34 @@ describe( 'Watchdog', () => {
 			} );
 			} );
 		} );
 		} );
 
 
+		it( 'Watchdog should omit error if the CKEditorError context is equal to null', () => {
+			const watchdog = new Watchdog();
+
+			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
+			watchdog.setDestructor( editor => editor.destroy() );
+
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
+			const originalErrorHandler = window.onerror;
+			window.onerror = undefined;
+
+			sinon.stub( console, 'error' );
+
+			return watchdog.create( document.createElement( 'div' ) ).then( () => {
+				setTimeout( () => throwCKEditorError( 'foo', null ) );
+
+				return new Promise( res => {
+					setTimeout( () => {
+						window.onerror = originalErrorHandler;
+
+						expect( watchdog.crashes ).to.deep.equal( [] );
+						sinon.assert.notCalled( console.error );
+
+						watchdog.destroy().then( res );
+					} );
+				} );
+			} );
+		} );
+
 		it( 'editor should be restarted with the data before the crash #1', () => {
 		it( 'editor should be restarted with the data before the crash #1', () => {
 			const watchdog = new Watchdog();
 			const watchdog = new Watchdog();