ckeditorerror.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import { default as CKEditorError, DOCUMENTATION_URL } from '../src/ckeditorerror';
  6. import { expectToThrowCKEditorError } from './_utils/utils';
  7. describe( 'CKEditorError', () => {
  8. it( 'inherits from Error', () => {
  9. const error = new CKEditorError( 'foo', null );
  10. expect( error ).to.be.an.instanceOf( Error );
  11. expect( error ).to.be.an.instanceOf( CKEditorError );
  12. } );
  13. it( 'sets the name', () => {
  14. const error = new CKEditorError( 'foo', null );
  15. expect( error ).to.have.property( 'name', 'CKEditorError' );
  16. } );
  17. it( 'sets the message', () => {
  18. const error = new CKEditorError( 'foo', null );
  19. expect( error ).to.have.property( 'message', 'foo' );
  20. expect( error.data ).to.be.undefined;
  21. } );
  22. it( 'sets the message and data', () => {
  23. const data = { bar: 1 };
  24. const error = new CKEditorError( 'foo', null, data );
  25. expect( error ).to.have.property( 'message', 'foo {"bar":1}' );
  26. expect( error ).to.have.property( 'data', data );
  27. } );
  28. it( 'sets the context of the error', () => {
  29. const data = { bar: 1 };
  30. const editor = {};
  31. const error = new CKEditorError( 'foo', editor, data );
  32. expect( error.context ).to.equal( editor );
  33. } );
  34. it( 'appends stringified data to the message', () => {
  35. class Foo {
  36. constructor() {
  37. this.x = 1;
  38. }
  39. }
  40. const data = {
  41. bar: 'a',
  42. bom: new Foo(),
  43. bim: 10
  44. };
  45. const error = new CKEditorError( 'foo', null, data );
  46. expect( error ).to.have.property( 'message', 'foo {"bar":"a","bom":{"x":1},"bim":10}' );
  47. expect( error ).to.have.property( 'data', data );
  48. } );
  49. it( 'contains a link which leads to the documentation', () => {
  50. const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.', null );
  51. const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
  52. `Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n`;
  53. expect( error ).to.have.property( 'message', errorMessage );
  54. } );
  55. it( 'link to documentation is added before the additional data message', () => {
  56. const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.', null, { foo: 1, bar: 2 } );
  57. const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
  58. `Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n ` +
  59. '{"foo":1,"bar":2}';
  60. expect( error ).to.have.property( 'message', errorMessage );
  61. } );
  62. describe( 'is()', () => {
  63. it( 'checks if error is an instance of CKEditorError', () => {
  64. const ckeditorError = new CKEditorError( 'foo', null );
  65. const regularError = new Error( 'foo' );
  66. expect( ( !!ckeditorError.is && ckeditorError.is( 'CKEditorError' ) ) ).to.be.true;
  67. expect( ( !!regularError.is && regularError.is( 'CKEditorError' ) ) ).to.be.false;
  68. } );
  69. } );
  70. describe( 'static rethrowUnexpectedError()', () => {
  71. it( 'should rethrow the original CKEditorError as it is', () => {
  72. const ckeditorError = new CKEditorError( 'foo', null );
  73. expectToThrowCKEditorError( () => {
  74. CKEditorError.rethrowUnexpectedError( ckeditorError, {} );
  75. }, /foo/, null );
  76. } );
  77. it( 'should rethrow an unexpected error wrapped in CKEditorError', () => {
  78. const error = new Error( 'foo' );
  79. error.stack = 'bar';
  80. const context = {};
  81. expectToThrowCKEditorError( () => {
  82. CKEditorError.rethrowUnexpectedError( error, context );
  83. }, /foo/, context );
  84. } );
  85. } );
  86. } );