8
0

ckeditorerror.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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', '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( 'message', 'foo {"bar":1}' );
  27. expect( error ).to.have.property( 'data', data );
  28. } );
  29. it( 'sets the context of the error', () => {
  30. const data = { bar: 1 };
  31. const editor = {};
  32. const error = new CKEditorError( 'foo', editor, data );
  33. expect( error.context ).to.equal( editor );
  34. } );
  35. it( 'appends stringified data to the message', () => {
  36. class Foo {
  37. constructor() {
  38. this.x = 1;
  39. }
  40. }
  41. const data = {
  42. bar: 'a',
  43. bom: new Foo(),
  44. bim: 10
  45. };
  46. const error = new CKEditorError( 'foo', null, data );
  47. expect( error ).to.have.property( 'message', 'foo {"bar":"a","bom":{"x":1},"bim":10}' );
  48. expect( error ).to.have.property( 'data', data );
  49. } );
  50. it( 'contains a link which leads to the documentation', () => {
  51. const error = new CKEditorError( 'model-schema-no-item', null );
  52. const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
  53. `Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n`;
  54. expect( error ).to.have.property( 'message', errorMessage );
  55. } );
  56. it( 'link to documentation is added before the additional data message', () => {
  57. const error = new CKEditorError( 'model-schema-no-item', null, { foo: 1, bar: 2 } );
  58. const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
  59. `Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n ` +
  60. '{"foo":1,"bar":2}';
  61. expect( error ).to.have.property( 'message', errorMessage );
  62. } );
  63. describe( 'is()', () => {
  64. it( 'checks if error is an instance of CKEditorError', () => {
  65. const ckeditorError = new CKEditorError( 'foo', null );
  66. const regularError = new Error( 'foo' );
  67. expect( ( !!ckeditorError.is && ckeditorError.is( 'CKEditorError' ) ) ).to.be.true;
  68. expect( ( !!regularError.is && regularError.is( 'CKEditorError' ) ) ).to.be.false;
  69. } );
  70. } );
  71. describe( 'static rethrowUnexpectedError()', () => {
  72. it( 'should rethrow the original CKEditorError as it is', () => {
  73. const ckeditorError = new CKEditorError( 'foo', null );
  74. expectToThrowCKEditorError( () => {
  75. CKEditorError.rethrowUnexpectedError( ckeditorError, {} );
  76. }, /foo/, null );
  77. } );
  78. it( 'should rethrow an unexpected error wrapped in CKEditorError', () => {
  79. const error = new Error( 'foo' );
  80. error.stack = 'bar';
  81. const context = {};
  82. expectToThrowCKEditorError( () => {
  83. CKEditorError.rethrowUnexpectedError( error, context );
  84. }, /foo/, context );
  85. } );
  86. } );
  87. } );