8
0

watchdog.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals setTimeout, window, console, document */
  6. import Watchdog from '../src/watchdog';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'Watchdog', () => {
  12. let element;
  13. beforeEach( () => {
  14. element = document.createElement( 'div' );
  15. document.body.appendChild( element );
  16. } );
  17. afterEach( () => {
  18. element.remove();
  19. sinon.restore();
  20. } );
  21. describe( 'create()', () => {
  22. it( 'should create an editor instance', () => {
  23. const watchdog = new Watchdog();
  24. const editorCreateSpy = sinon.spy( ClassicTestEditor, 'create' );
  25. const editorDestroySpy = sinon.spy( ClassicTestEditor.prototype, 'destroy' );
  26. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  27. watchdog.setDestructor( editor => editor.destroy() );
  28. return watchdog.create( element, {} )
  29. .then( () => {
  30. sinon.assert.calledOnce( editorCreateSpy );
  31. sinon.assert.notCalled( editorDestroySpy );
  32. return watchdog.destroy();
  33. } )
  34. .then( () => {
  35. sinon.assert.calledOnce( editorCreateSpy );
  36. sinon.assert.calledOnce( editorDestroySpy );
  37. } );
  38. } );
  39. it( 'should throw an error when the creator is not defined', () => {
  40. const watchdog = new Watchdog();
  41. watchdog.setDestructor( editor => editor.destroy() );
  42. expectToThrowCKEditorError(
  43. () => watchdog.create(),
  44. /^watchdog-creator-not-defined/,
  45. null
  46. );
  47. } );
  48. it( 'should throw an error when the destructor is not defined', () => {
  49. const watchdog = new Watchdog();
  50. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  51. expectToThrowCKEditorError(
  52. () => watchdog.create(),
  53. /^watchdog-destructor-not-defined/,
  54. null
  55. );
  56. } );
  57. it( 'should properly copy the config', () => {
  58. const watchdog = new Watchdog();
  59. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  60. watchdog.setDestructor( editor => editor.destroy() );
  61. const config = {
  62. foo: [],
  63. bar: document.createElement( 'div' )
  64. };
  65. return watchdog.create( element, config ).then( () => {
  66. expect( watchdog.editor.config._config.foo ).to.not.equal( config.foo );
  67. expect( watchdog.editor.config._config.bar ).to.equal( config.bar );
  68. } );
  69. } );
  70. it( 'should support editor data passed as the first argument', () => {
  71. const watchdog = new Watchdog();
  72. watchdog.setCreator( ( data, config ) => ClassicTestEditor.create( data, config ) );
  73. watchdog.setDestructor( editor => editor.destroy() );
  74. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  75. const originalErrorHandler = window.onerror;
  76. const windowErrorSpy = sinon.spy();
  77. window.onerror = windowErrorSpy;
  78. return watchdog.create( '<p>foo</p>', { plugins: [ Paragraph ] } )
  79. .then( () => {
  80. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  81. return new Promise( res => {
  82. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  83. watchdog.on( 'restart', () => {
  84. window.onerror = originalErrorHandler;
  85. res();
  86. } );
  87. } );
  88. } )
  89. .then( () => {
  90. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  91. return watchdog.destroy();
  92. } );
  93. } );
  94. } );
  95. describe( 'editor', () => {
  96. it( 'should be the current editor instance', () => {
  97. const watchdog = Watchdog.for( ClassicTestEditor );
  98. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  99. const originalErrorHandler = window.onerror;
  100. const windowErrorSpy = sinon.spy();
  101. window.onerror = windowErrorSpy;
  102. expect( watchdog.editor ).to.be.null;
  103. let oldEditor;
  104. return watchdog.create( element, {} )
  105. .then( () => {
  106. oldEditor = watchdog.editor;
  107. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  108. return new Promise( res => {
  109. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  110. watchdog.on( 'restart', () => {
  111. window.onerror = originalErrorHandler;
  112. res();
  113. } );
  114. } );
  115. } )
  116. .then( () => {
  117. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  118. expect( watchdog.editor ).to.not.equal( oldEditor );
  119. return watchdog.destroy();
  120. } )
  121. .then( () => {
  122. expect( watchdog.editor ).to.be.null;
  123. } );
  124. } );
  125. } );
  126. describe( 'error handling', () => {
  127. it( 'Watchdog should not restart editor during the initialization', () => {
  128. const watchdog = new Watchdog();
  129. watchdog.setCreator( el =>
  130. ClassicTestEditor.create( el )
  131. .then( () => Promise.reject( new Error( 'foo' ) ) )
  132. );
  133. watchdog.setDestructor( editor => editor.destroy() );
  134. return watchdog.create( element ).then(
  135. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  136. err => {
  137. expect( err ).to.be.instanceOf( Error );
  138. expect( err.message ).to.equal( 'foo' );
  139. }
  140. );
  141. } );
  142. it( 'Watchdog should not restart editor during the destroy', () => {
  143. const watchdog = new Watchdog();
  144. watchdog.setCreator( el => ClassicTestEditor.create( el ) );
  145. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  146. return Promise.resolve()
  147. .then( () => watchdog.create( element ) )
  148. .then( () => watchdog.destroy() )
  149. .then(
  150. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  151. err => {
  152. expect( err ).to.be.instanceOf( Error );
  153. expect( err.message ).to.equal( 'foo' );
  154. }
  155. );
  156. } );
  157. it( 'Watchdog should not hide intercepted errors', () => {
  158. const watchdog = new Watchdog();
  159. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  160. watchdog.setDestructor( editor => editor.destroy() );
  161. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  162. const originalErrorHandler = window.onerror;
  163. const windowErrorSpy = sinon.spy();
  164. window.onerror = windowErrorSpy;
  165. return watchdog.create( element ).then( () => {
  166. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  167. return new Promise( res => {
  168. watchdog.on( 'restart', () => {
  169. window.onerror = originalErrorHandler;
  170. sinon.assert.calledOnce( windowErrorSpy );
  171. // Various browsers will display the error slightly differently.
  172. expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.match( /foo/ );
  173. watchdog.destroy().then( res );
  174. } );
  175. } );
  176. } );
  177. } );
  178. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  179. const watchdog = new Watchdog();
  180. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  181. watchdog.setDestructor( editor => editor.destroy() );
  182. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  183. const originalErrorHandler = window.onerror;
  184. window.onerror = undefined;
  185. return watchdog.create( element ).then( () => {
  186. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  187. return new Promise( res => {
  188. watchdog.on( 'restart', () => {
  189. window.onerror = originalErrorHandler;
  190. watchdog.destroy().then( res );
  191. } );
  192. } );
  193. } );
  194. } );
  195. it( 'Watchdog should not intercept non-editor errors', () => {
  196. const watchdog = new Watchdog();
  197. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  198. watchdog.setDestructor( editor => editor.destroy() );
  199. const editorErrorSpy = sinon.spy();
  200. watchdog.on( 'error', editorErrorSpy );
  201. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  202. const originalErrorHandler = window.onerror;
  203. window.onerror = undefined;
  204. return watchdog.create( element ).then( () => {
  205. setTimeout( () => {
  206. throw new Error( 'foo' );
  207. } );
  208. setTimeout( () => {
  209. throw 'bar';
  210. } );
  211. return new Promise( res => {
  212. setTimeout( () => {
  213. window.onerror = originalErrorHandler;
  214. sinon.assert.notCalled( editorErrorSpy );
  215. watchdog.destroy().then( res );
  216. } );
  217. } );
  218. } );
  219. } );
  220. it( 'Watchdog should not intercept other editor errors', () => {
  221. const watchdog1 = Watchdog.for( ClassicTestEditor );
  222. const watchdog2 = Watchdog.for( ClassicTestEditor );
  223. const config = {
  224. plugins: []
  225. };
  226. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  227. const originalErrorHandler = window.onerror;
  228. window.onerror = undefined;
  229. return Promise.all( [
  230. watchdog1.create( element, config ),
  231. watchdog2.create( element, config )
  232. ] ).then( () => {
  233. return new Promise( res => {
  234. const watchdog1ErrorSpy = sinon.spy();
  235. const watchdog2ErrorSpy = sinon.spy();
  236. watchdog1.on( 'restart', watchdog1ErrorSpy );
  237. watchdog2.on( 'restart', watchdog2ErrorSpy );
  238. setTimeout( () => throwCKEditorError( 'foo', watchdog2.editor ) );
  239. setTimeout( () => {
  240. window.onerror = originalErrorHandler;
  241. sinon.assert.notCalled( watchdog1ErrorSpy );
  242. sinon.assert.calledOnce( watchdog2ErrorSpy );
  243. Promise.all( [ watchdog1.destroy(), watchdog2.destroy() ] )
  244. .then( res );
  245. } );
  246. } );
  247. } );
  248. } );
  249. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context', () => {
  250. const watchdog = new Watchdog();
  251. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  252. watchdog.setDestructor( editor => editor.destroy() );
  253. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  254. const originalErrorHandler = window.onerror;
  255. window.onerror = undefined;
  256. return watchdog.create( element ).then( () => {
  257. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor.model.document ) );
  258. return new Promise( res => {
  259. watchdog.on( 'restart', () => {
  260. window.onerror = originalErrorHandler;
  261. watchdog.destroy().then( res );
  262. } );
  263. } );
  264. } );
  265. } );
  266. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context #2', () => {
  267. const watchdog = new Watchdog();
  268. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  269. watchdog.setDestructor( editor => editor.destroy() );
  270. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  271. const originalErrorHandler = window.onerror;
  272. window.onerror = undefined;
  273. return watchdog.create( element ).then( () => {
  274. setTimeout( () => throwCKEditorError( 'foo', {
  275. foo: [ 1, 2, 3, {
  276. bar: new Set( [
  277. new Map( [
  278. [ 'foo', 'bar' ],
  279. [ 0, watchdog.editor ]
  280. ] )
  281. ] )
  282. } ]
  283. } ) );
  284. return new Promise( res => {
  285. watchdog.on( 'restart', () => {
  286. window.onerror = originalErrorHandler;
  287. watchdog.destroy().then( res );
  288. } );
  289. } );
  290. } );
  291. } );
  292. it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
  293. ' and the average time between errors is lower than `minimumNonErrorTimePeriod` (default values)', () => {
  294. const watchdog = new Watchdog();
  295. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  296. watchdog.setDestructor( editor => editor.destroy() );
  297. const errorSpy = sinon.spy();
  298. watchdog.on( 'error', errorSpy );
  299. const restartSpy = sinon.spy();
  300. watchdog.on( 'restart', restartSpy );
  301. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  302. const originalErrorHandler = window.onerror;
  303. window.onerror = undefined;
  304. return watchdog.create( element ).then( () => {
  305. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  306. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  307. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  308. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  309. return new Promise( res => {
  310. setTimeout( () => {
  311. expect( errorSpy.callCount ).to.equal( 4 );
  312. expect( watchdog.crashes.length ).to.equal( 4 );
  313. expect( restartSpy.callCount ).to.equal( 3 );
  314. window.onerror = originalErrorHandler;
  315. watchdog.destroy().then( res );
  316. } );
  317. } );
  318. } );
  319. } );
  320. it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
  321. ' and the average time between errors is lower than `minimumNonErrorTimePeriod` (custom values)', () => {
  322. const watchdog = new Watchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 1000 } );
  323. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  324. watchdog.setDestructor( editor => editor.destroy() );
  325. const errorSpy = sinon.spy();
  326. watchdog.on( 'error', errorSpy );
  327. const restartSpy = sinon.spy();
  328. watchdog.on( 'restart', restartSpy );
  329. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  330. const originalErrorHandler = window.onerror;
  331. window.onerror = undefined;
  332. return watchdog.create( element ).then( () => {
  333. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  334. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  335. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  336. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  337. return new Promise( res => {
  338. setTimeout( () => {
  339. expect( errorSpy.callCount ).to.equal( 3 );
  340. expect( watchdog.crashes.length ).to.equal( 3 );
  341. expect( restartSpy.callCount ).to.equal( 2 );
  342. window.onerror = originalErrorHandler;
  343. watchdog.destroy().then( res );
  344. } );
  345. } );
  346. } );
  347. } );
  348. it( 'Watchdog should not crash permantently when average time between errors is longer than `minimumNonErrorTimePeriod`', () => {
  349. const watchdog = new Watchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 0 } );
  350. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  351. watchdog.setDestructor( editor => editor.destroy() );
  352. const errorSpy = sinon.spy();
  353. watchdog.on( 'error', errorSpy );
  354. const restartSpy = sinon.spy();
  355. watchdog.on( 'restart', restartSpy );
  356. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  357. const originalErrorHandler = window.onerror;
  358. window.onerror = undefined;
  359. return watchdog.create( element ).then( () => {
  360. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ), 5 );
  361. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ), 10 );
  362. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ), 15 );
  363. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ), 20 );
  364. return new Promise( res => {
  365. setTimeout( () => {
  366. expect( errorSpy.callCount ).to.equal( 4 );
  367. expect( watchdog.crashes.length ).to.equal( 4 );
  368. expect( restartSpy.callCount ).to.equal( 4 );
  369. window.onerror = originalErrorHandler;
  370. watchdog.destroy().then( res );
  371. }, 20 );
  372. } );
  373. } );
  374. } );
  375. it( 'Watchdog should warn if the CKEditorError missing its context', () => {
  376. const watchdog = new Watchdog();
  377. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  378. watchdog.setDestructor( editor => editor.destroy() );
  379. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  380. const originalErrorHandler = window.onerror;
  381. window.onerror = undefined;
  382. sinon.stub( console, 'error' );
  383. return watchdog.create( element ).then( () => {
  384. setTimeout( () => throwCKEditorError( 'foo' ) );
  385. return new Promise( res => {
  386. setTimeout( () => {
  387. window.onerror = originalErrorHandler;
  388. expect( watchdog.crashes ).to.deep.equal( [] );
  389. sinon.assert.calledWithExactly(
  390. console.error,
  391. 'The error is missing its context and Watchdog cannot restart the proper editor.'
  392. );
  393. watchdog.destroy().then( res );
  394. } );
  395. } );
  396. } );
  397. } );
  398. it( 'Watchdog should omit error if the CKEditorError context is equal to null', () => {
  399. const watchdog = new Watchdog();
  400. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  401. watchdog.setDestructor( editor => editor.destroy() );
  402. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  403. const originalErrorHandler = window.onerror;
  404. window.onerror = undefined;
  405. sinon.stub( console, 'error' );
  406. return watchdog.create( element ).then( () => {
  407. setTimeout( () => throwCKEditorError( 'foo', null ) );
  408. return new Promise( res => {
  409. setTimeout( () => {
  410. window.onerror = originalErrorHandler;
  411. expect( watchdog.crashes ).to.deep.equal( [] );
  412. sinon.assert.notCalled( console.error );
  413. watchdog.destroy().then( res );
  414. } );
  415. } );
  416. } );
  417. } );
  418. it( 'editor should be restarted with the data before the crash #1', () => {
  419. const watchdog = new Watchdog();
  420. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  421. watchdog.setDestructor( editor => editor.destroy() );
  422. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  423. const originalErrorHandler = window.onerror;
  424. window.onerror = undefined;
  425. return watchdog.create( element, {
  426. initialData: '<p>foo</p>',
  427. plugins: [ Paragraph ]
  428. } ).then( () => {
  429. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  430. return new Promise( res => {
  431. watchdog.on( 'restart', () => {
  432. window.onerror = originalErrorHandler;
  433. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  434. watchdog.destroy().then( res );
  435. } );
  436. } );
  437. } );
  438. } );
  439. it( 'editor should be restarted with the data before the crash #2', () => {
  440. const watchdog = new Watchdog();
  441. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  442. watchdog.setDestructor( editor => editor.destroy() );
  443. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  444. const originalErrorHandler = window.onerror;
  445. window.onerror = undefined;
  446. return watchdog.create( element, {
  447. initialData: '<p>foo</p>',
  448. plugins: [ Paragraph ]
  449. } ).then( () => {
  450. const doc = watchdog.editor.model.document;
  451. watchdog.editor.model.change( writer => {
  452. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  453. } );
  454. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  455. return new Promise( res => {
  456. watchdog.on( 'restart', () => {
  457. window.onerror = originalErrorHandler;
  458. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  459. watchdog.destroy().then( res );
  460. } );
  461. } );
  462. } );
  463. } );
  464. it( 'editor should be restarted with the data of the latest document version before the crash', () => {
  465. const watchdog = new Watchdog();
  466. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  467. watchdog.setDestructor( editor => editor.destroy() );
  468. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  469. const originalErrorHandler = window.onerror;
  470. window.onerror = undefined;
  471. return watchdog.create( element, {
  472. initialData: '<p>foo</p>',
  473. plugins: [ Paragraph ]
  474. } ).then( () => {
  475. const model = watchdog.editor.model;
  476. const doc = model.document;
  477. // Decrement the document version to simulate a situation when an operation
  478. // don't produce new document version.
  479. doc.version--;
  480. model.change( writer => {
  481. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  482. } );
  483. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  484. return new Promise( res => {
  485. watchdog.on( 'restart', () => {
  486. window.onerror = originalErrorHandler;
  487. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  488. watchdog.destroy().then( res );
  489. } );
  490. } );
  491. } );
  492. } );
  493. it( 'editor should be restarted with the latest available data before the crash', () => {
  494. const watchdog = new Watchdog();
  495. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  496. watchdog.setDestructor( editor => editor.destroy() );
  497. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  498. const originalErrorHandler = window.onerror;
  499. window.onerror = undefined;
  500. sinon.stub( console, 'error' );
  501. return watchdog.create( element, {
  502. initialData: '<p>foo</p>',
  503. plugins: [ Paragraph ]
  504. } ).then( () => {
  505. const editorGetDataError = new Error( 'Some error' );
  506. const getDataStub = sinon.stub( watchdog.editor.data, 'get' )
  507. .throwsException( editorGetDataError );
  508. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  509. return new Promise( res => {
  510. const doc = watchdog.editor.model.document;
  511. watchdog.editor.model.change( writer => {
  512. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  513. } );
  514. watchdog.on( 'restart', () => {
  515. window.onerror = originalErrorHandler;
  516. // It is called second time by during the default editor destruction
  517. // to update the source element.
  518. sinon.assert.calledTwice( getDataStub );
  519. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  520. sinon.assert.calledWith(
  521. console.error,
  522. editorGetDataError,
  523. 'An error happened during restoring editor data. Editor will be restored from the previously saved data.'
  524. );
  525. sinon.assert.calledWith(
  526. console.error,
  527. 'An error happened during the editor destructing.'
  528. );
  529. watchdog.destroy().then( res );
  530. } );
  531. } );
  532. } );
  533. } );
  534. } );
  535. describe( 'async error handling', () => {
  536. let unhandledRejectionEventSupported;
  537. before( () => {
  538. return isUnhandledRejectionEventSupported()
  539. .then( val => {
  540. unhandledRejectionEventSupported = val;
  541. } );
  542. } );
  543. it( 'Watchdog should handle async CKEditorError errors', () => {
  544. if ( !unhandledRejectionEventSupported ) {
  545. return;
  546. }
  547. const watchdog = Watchdog.for( ClassicTestEditor );
  548. const originalErrorHandler = window.onerror;
  549. window.onerror = undefined;
  550. return watchdog.create( element, {
  551. initialData: '<p>foo</p>',
  552. plugins: [ Paragraph ]
  553. } ).then( () => {
  554. const oldEditor = watchdog.editor;
  555. Promise.resolve().then( () => throwCKEditorError( 'foo', watchdog.editor ) );
  556. return new Promise( res => {
  557. watchdog.on( 'restart', () => {
  558. window.onerror = originalErrorHandler;
  559. expect( watchdog.editor ).to.not.equal( oldEditor );
  560. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  561. watchdog.destroy().then( res );
  562. } );
  563. } );
  564. } );
  565. } );
  566. it( 'Watchdog should not react to non-editor async errors', () => {
  567. if ( !unhandledRejectionEventSupported ) {
  568. return;
  569. }
  570. const watchdog = Watchdog.for( ClassicTestEditor );
  571. const originalErrorHandler = window.onerror;
  572. const editorErrorSpy = sinon.spy();
  573. window.onerror = undefined;
  574. return watchdog.create( element, {
  575. initialData: '<p>foo</p>',
  576. plugins: [ Paragraph ]
  577. } ).then( () => {
  578. watchdog.on( 'error', editorErrorSpy );
  579. Promise.resolve().then( () => Promise.reject( 'foo' ) );
  580. Promise.resolve().then( () => Promise.reject( new Error( 'bar' ) ) );
  581. // Wait a cycle.
  582. return new Promise( res => setTimeout( res ) );
  583. } ).then( () => {
  584. window.onerror = originalErrorHandler;
  585. sinon.assert.notCalled( editorErrorSpy );
  586. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  587. return watchdog.destroy();
  588. } );
  589. } );
  590. } );
  591. describe( 'static for()', () => {
  592. it( 'should be a shortcut method for creating the watchdog', () => {
  593. const watchdog = Watchdog.for( ClassicTestEditor );
  594. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  595. const originalErrorHandler = window.onerror;
  596. window.onerror = undefined;
  597. return watchdog.create( element, {
  598. initialData: '<p>foo</p>',
  599. plugins: [ Paragraph ]
  600. } ).then( () => {
  601. const oldEditor = watchdog.editor;
  602. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  603. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  604. return new Promise( res => {
  605. watchdog.on( 'restart', () => {
  606. window.onerror = originalErrorHandler;
  607. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  608. expect( watchdog.editor ).to.not.equal( oldEditor );
  609. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  610. watchdog.destroy().then( res );
  611. } );
  612. } );
  613. } );
  614. } );
  615. } );
  616. describe( 'crashes', () => {
  617. it( 'should be an array of caught errors by the watchdog', () => {
  618. const watchdog = Watchdog.for( ClassicTestEditor );
  619. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  620. const originalErrorHandler = window.onerror;
  621. window.onerror = undefined;
  622. return watchdog.create( element ).then( () => {
  623. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  624. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  625. return new Promise( res => {
  626. setTimeout( () => {
  627. window.onerror = originalErrorHandler;
  628. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  629. expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
  630. watchdog.destroy().then( res );
  631. } );
  632. } );
  633. } );
  634. } );
  635. } );
  636. describe( 'state', () => {
  637. it( 'should reflect the state of the watchdog', () => {
  638. const watchdog = Watchdog.for( ClassicTestEditor );
  639. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  640. const originalErrorHandler = window.onerror;
  641. window.onerror = undefined;
  642. expect( watchdog.state ).to.equal( 'initializing' );
  643. return watchdog.create( element ).then( () => {
  644. expect( watchdog.state ).to.equal( 'ready' );
  645. return watchdog.create( element ).then( () => {
  646. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  647. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  648. return new Promise( res => {
  649. setTimeout( () => {
  650. window.onerror = originalErrorHandler;
  651. expect( watchdog.state ).to.equal( 'ready' );
  652. watchdog.destroy().then( () => {
  653. expect( watchdog.state ).to.equal( 'destroyed' );
  654. res();
  655. } );
  656. } );
  657. } );
  658. } );
  659. } );
  660. } );
  661. it( 'should be observable', () => {
  662. const watchdog = Watchdog.for( ClassicTestEditor );
  663. const states = [];
  664. watchdog.on( 'change:state', ( evt, propName, newValue ) => {
  665. states.push( newValue );
  666. } );
  667. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  668. const originalErrorHandler = window.onerror;
  669. window.onerror = undefined;
  670. return watchdog.create( element ).then( () => {
  671. return watchdog.create( element ).then( () => {
  672. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  673. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  674. setTimeout( () => throwCKEditorError( 'baz', watchdog.editor ) );
  675. setTimeout( () => throwCKEditorError( 'biz', watchdog.editor ) );
  676. return new Promise( res => {
  677. setTimeout( () => {
  678. window.onerror = originalErrorHandler;
  679. watchdog.destroy().then( () => {
  680. expect( states ).to.deep.equal( [
  681. 'ready',
  682. 'crashed',
  683. 'initializing',
  684. 'ready',
  685. 'crashed',
  686. 'initializing',
  687. 'ready',
  688. 'crashed',
  689. 'initializing',
  690. 'ready',
  691. 'crashed',
  692. 'crashedPermanently',
  693. 'destroyed'
  694. ] );
  695. res();
  696. } );
  697. } );
  698. } );
  699. } );
  700. } );
  701. } );
  702. } );
  703. } );
  704. function throwCKEditorError( name, context ) {
  705. throw new CKEditorError( name, context );
  706. }
  707. // Feature detection works as a race condition - if the `unhandledrejection` event
  708. // is supported then the listener should be called first. Otherwise the timeout will be reached.
  709. function isUnhandledRejectionEventSupported() {
  710. return new Promise( res => {
  711. window.addEventListener( 'unhandledrejection', function listener() {
  712. res( true );
  713. window.removeEventListener( 'unhandledrejection', listener );
  714. } );
  715. Promise.resolve().then( () => Promise.reject( new Error() ) );
  716. setTimeout( () => res( false ) );
  717. } );
  718. }