ckeditorerror.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module utils/ckeditorerror
  7. */
  8. /**
  9. * URL to the documentation with error codes.
  10. */
  11. export const DOCUMENTATION_URL =
  12. 'https://ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html';
  13. /**
  14. * The CKEditor error class.
  15. *
  16. * All errors will be shortened during the minification process in order to reduce the code size.
  17. * Therefore, all error messages should be documented in the same way as those in {@link module:utils/log}.
  18. *
  19. * Read more in the {@link module:utils/log} module.
  20. *
  21. * @extends Error
  22. */
  23. export default class CKEditorError extends Error {
  24. /**
  25. * Creates an instance of the CKEditorError class.
  26. *
  27. * Read more about error logging in the {@link module:utils/log} module.
  28. *
  29. * @param {String} message The error message in an `error-name: Error message.` format.
  30. * During the minification process the "Error message" part will be removed to limit the code size
  31. * and a link to this error documentation will be added to the `message`.
  32. * @param {Object} [data] Additional data describing the error. A stringified version of this object
  33. * will be appended to the error message, so the data are quickly visible in the console. The original
  34. * data object will also be later available under the {@link #data} property.
  35. */
  36. constructor( message, data ) {
  37. message = attachLinkToDocumentation( message );
  38. if ( data ) {
  39. message += ' ' + JSON.stringify( data );
  40. }
  41. super( message );
  42. /**
  43. * @member {String}
  44. */
  45. this.name = 'CKEditorError';
  46. /**
  47. * The additional error data passed to the constructor. Undefined if none was passed.
  48. *
  49. * @member {Object|undefined}
  50. */
  51. this.data = data;
  52. }
  53. /**
  54. * Checks if error is an instance of CKEditorError class.
  55. *
  56. * @param {Object} error Object to check.
  57. * @returns {Boolean}
  58. */
  59. static isCKEditorError( error ) {
  60. return error instanceof CKEditorError;
  61. }
  62. }
  63. /**
  64. * Attaches link to the documentation at the end of the error message.
  65. *
  66. * @param {String} message Message to be logged.
  67. * @returns {String}
  68. */
  69. export function attachLinkToDocumentation( message ) {
  70. const matchedErrorName = message.match( /^([^:]+):/ );
  71. if ( !matchedErrorName ) {
  72. return message;
  73. }
  74. return message + ` Read more: ${ DOCUMENTATION_URL }#error-${ matchedErrorName[ 1 ] }\n`;
  75. }