ckeditorerror.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /* global console */
  7. import { default as CKEditorError, DOCUMENTATION_URL, logError, logWarning } from '../src/ckeditorerror';
  8. import { expectToThrowCKEditorError } from './_utils/utils';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. describe( 'CKEditorError', () => {
  11. it( 'inherits from Error', () => {
  12. const error = new CKEditorError( 'foo', null );
  13. expect( error ).to.be.an.instanceOf( Error );
  14. expect( error ).to.be.an.instanceOf( CKEditorError );
  15. } );
  16. it( 'sets the name', () => {
  17. const error = new CKEditorError( 'foo', null );
  18. expect( error ).to.have.property( 'name', 'CKEditorError' );
  19. } );
  20. it( 'sets the message', () => {
  21. const error = new CKEditorError( 'foo', null );
  22. expect( error ).to.have.property( 'message' ).that.matches( /^foo/ );
  23. expect( error.data ).to.be.undefined;
  24. } );
  25. it( 'sets the message and data', () => {
  26. const data = { bar: 1 };
  27. const error = new CKEditorError( 'foo', null, data );
  28. expect( error ).to.have.property(
  29. 'message',
  30. `foo {"bar":1}\nRead more: ${ DOCUMENTATION_URL }#error-foo`
  31. );
  32. expect( error ).to.have.property( 'data', data );
  33. } );
  34. it( 'sets the context of the error', () => {
  35. const data = { bar: 1 };
  36. const editor = {};
  37. const error = new CKEditorError( 'foo', editor, data );
  38. expect( error.context ).to.equal( editor );
  39. } );
  40. it( 'appends stringified data to the message', () => {
  41. class Foo {
  42. constructor() {
  43. this.x = 1;
  44. }
  45. }
  46. const data = {
  47. bar: 'a',
  48. bom: new Foo(),
  49. bim: 10
  50. };
  51. const error = new CKEditorError( 'foo', null, data );
  52. expect( error ).to.have.property(
  53. 'message',
  54. `foo {"bar":"a","bom":{"x":1},"bim":10}\nRead more: ${ DOCUMENTATION_URL }#error-foo`
  55. );
  56. expect( error ).to.have.property( 'data', data );
  57. } );
  58. it( 'contains a link which leads to the documentation', () => {
  59. const error = new CKEditorError( 'model-schema-no-item', null );
  60. const errorMessage = `model-schema-no-item\nRead more: ${ DOCUMENTATION_URL }#error-model-schema-no-item`;
  61. expect( error ).to.have.property( 'message', errorMessage );
  62. } );
  63. it( 'link to documentation is added after the additional data message', () => {
  64. const error = new CKEditorError( 'model-schema-no-item', null, { foo: 1, bar: 2 } );
  65. const errorMessage = `model-schema-no-item {"foo":1,"bar":2}\nRead more: ${ DOCUMENTATION_URL }#error-model-schema-no-item`;
  66. expect( error ).to.have.property( 'message', errorMessage );
  67. } );
  68. describe( 'is()', () => {
  69. it( 'checks if error is an instance of CKEditorError', () => {
  70. const ckeditorError = new CKEditorError( 'foo', null );
  71. const regularError = new Error( 'foo' );
  72. expect( ( !!ckeditorError.is && ckeditorError.is( 'CKEditorError' ) ) ).to.be.true;
  73. expect( ( !!regularError.is && regularError.is( 'CKEditorError' ) ) ).to.be.false;
  74. } );
  75. } );
  76. describe( 'static rethrowUnexpectedError()', () => {
  77. it( 'should rethrow the original CKEditorError as it is', () => {
  78. const ckeditorError = new CKEditorError( 'foo', null );
  79. expectToThrowCKEditorError( () => {
  80. CKEditorError.rethrowUnexpectedError( ckeditorError, {} );
  81. }, /foo/, null );
  82. } );
  83. it( 'should rethrow an unexpected error wrapped in CKEditorError', () => {
  84. const error = new Error( 'foo' );
  85. error.stack = 'bar';
  86. const context = {};
  87. expectToThrowCKEditorError( () => {
  88. CKEditorError.rethrowUnexpectedError( error, context );
  89. }, /foo/, context );
  90. } );
  91. } );
  92. describe( 'logWarning()', () => {
  93. beforeEach( () => {
  94. testUtils.sinon.stub( console, 'warn' );
  95. } );
  96. afterEach( () => {
  97. console.warn.restore();
  98. } );
  99. it( 'should log warning with data and link to the documentation', () => {
  100. logWarning( 'foo', { name: 'foo' } );
  101. sinon.assert.calledOnce( console.warn );
  102. sinon.assert.calledWithExactly( console.warn,
  103. sinon.match( 'foo' ),
  104. { name: 'foo' },
  105. `\nRead more: ${ DOCUMENTATION_URL }#error-foo`
  106. );
  107. } );
  108. it( 'should log warning without data and with a link to the documentation', () => {
  109. logWarning( 'foo' );
  110. sinon.assert.calledOnce( console.warn );
  111. sinon.assert.calledWithExactly( console.warn,
  112. sinon.match( 'foo' ),
  113. `\nRead more: ${ DOCUMENTATION_URL }#error-foo`
  114. );
  115. } );
  116. } );
  117. describe( 'logError()', () => {
  118. beforeEach( () => {
  119. testUtils.sinon.stub( console, 'error' );
  120. } );
  121. afterEach( () => {
  122. console.error.restore();
  123. } );
  124. it( 'should log error with data and link to the documentation', () => {
  125. logError( 'foo', { name: 'foo' } );
  126. sinon.assert.calledOnce( console.error );
  127. sinon.assert.calledWithExactly( console.error,
  128. sinon.match( 'foo' ),
  129. { name: 'foo' },
  130. `\nRead more: ${ DOCUMENTATION_URL }#error-foo`
  131. );
  132. } );
  133. it( 'should log error without data and with a link to the documentation', () => {
  134. logError( 'foo' );
  135. sinon.assert.calledOnce( console.error );
  136. sinon.assert.calledWithExactly( console.error,
  137. sinon.match( 'foo' ),
  138. `\nRead more: ${ DOCUMENTATION_URL }#error-foo`
  139. );
  140. } );
  141. } );
  142. } );