8
0

ckeditorerror.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /* eslint-disable ckeditor5-rules/ckeditor-error-message */
  6. import { default as CKEditorError, DOCUMENTATION_URL } from '../src/ckeditorerror';
  7. import { expectToThrowCKEditorError } from './_utils/utils';
  8. describe( 'CKEditorError', () => {
  9. it( 'inherits from Error', () => {
  10. const error = new CKEditorError( 'foo', null );
  11. expect( error ).to.be.an.instanceOf( Error );
  12. expect( error ).to.be.an.instanceOf( CKEditorError );
  13. } );
  14. it( 'sets the name', () => {
  15. const error = new CKEditorError( 'foo', null );
  16. expect( error ).to.have.property( 'name', 'CKEditorError' );
  17. } );
  18. it( 'sets the message', () => {
  19. const error = new CKEditorError( 'foo', null );
  20. expect( error ).to.have.property( 'message' ).that.matches( /^foo/ );
  21. expect( error.data ).to.be.undefined;
  22. } );
  23. it( 'sets the message and data', () => {
  24. const data = { bar: 1 };
  25. const error = new CKEditorError( 'foo', null, data );
  26. expect( error ).to.have.property(
  27. 'message',
  28. `foo Read more: ${ DOCUMENTATION_URL }#error-foo\n {"bar":1}`
  29. );
  30. expect( error ).to.have.property( 'data', data );
  31. } );
  32. it( 'sets the context of the error', () => {
  33. const data = { bar: 1 };
  34. const editor = {};
  35. const error = new CKEditorError( 'foo', editor, data );
  36. expect( error.context ).to.equal( editor );
  37. } );
  38. it( 'appends stringified data to the message', () => {
  39. class Foo {
  40. constructor() {
  41. this.x = 1;
  42. }
  43. }
  44. const data = {
  45. bar: 'a',
  46. bom: new Foo(),
  47. bim: 10
  48. };
  49. const error = new CKEditorError( 'foo', null, data );
  50. expect( error ).to.have.property(
  51. 'message',
  52. `foo Read more: ${ DOCUMENTATION_URL }#error-foo\n {"bar":"a","bom":{"x":1},"bim":10}`
  53. );
  54. expect( error ).to.have.property( 'data', data );
  55. } );
  56. it( 'contains a link which leads to the documentation', () => {
  57. const error = new CKEditorError( 'model-schema-no-item', null );
  58. const errorMessage = 'model-schema-no-item' +
  59. ` Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n`;
  60. expect( error ).to.have.property( 'message', errorMessage );
  61. } );
  62. it( 'link to documentation is added before the additional data message', () => {
  63. const error = new CKEditorError( 'model-schema-no-item', null, { foo: 1, bar: 2 } );
  64. const errorMessage = 'model-schema-no-item ' +
  65. `Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n ` +
  66. '{"foo":1,"bar":2}';
  67. expect( error ).to.have.property( 'message', errorMessage );
  68. } );
  69. describe( 'is()', () => {
  70. it( 'checks if error is an instance of CKEditorError', () => {
  71. const ckeditorError = new CKEditorError( 'foo', null );
  72. const regularError = new Error( 'foo' );
  73. expect( ( !!ckeditorError.is && ckeditorError.is( 'CKEditorError' ) ) ).to.be.true;
  74. expect( ( !!regularError.is && regularError.is( 'CKEditorError' ) ) ).to.be.false;
  75. } );
  76. } );
  77. describe( 'static rethrowUnexpectedError()', () => {
  78. it( 'should rethrow the original CKEditorError as it is', () => {
  79. const ckeditorError = new CKEditorError( 'foo', null );
  80. expectToThrowCKEditorError( () => {
  81. CKEditorError.rethrowUnexpectedError( ckeditorError, {} );
  82. }, /foo/, null );
  83. } );
  84. it( 'should rethrow an unexpected error wrapped in CKEditorError', () => {
  85. const error = new Error( 'foo' );
  86. error.stack = 'bar';
  87. const context = {};
  88. expectToThrowCKEditorError( () => {
  89. CKEditorError.rethrowUnexpectedError( error, context );
  90. }, /foo/, context );
  91. } );
  92. } );
  93. } );