Przeglądaj źródła

CKEditorError will contain a link which leads to the documentation.

Kamil Piechaczek 8 lat temu
rodzic
commit
356bc7f444

+ 23 - 0
packages/ckeditor5-utils/src/ckeditorerror.js

@@ -7,6 +7,11 @@
  * @module utils/ckeditorerror
  */
 
+/**
+ * URL to the documentation with error codes.
+ */
+export const DOCUMENTATION_URL = 'https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/framework/guides/error-codes.html';
+
 /**
  * The CKEditor error class.
  *
@@ -31,6 +36,8 @@ export default class CKEditorError extends Error {
 	 * data object will also be later available under the {@link #data} property.
 	 */
 	constructor( message, data ) {
+		message = attachLinkToDocumentation( message );
+
 		if ( data ) {
 			message += ' ' + JSON.stringify( data );
 		}
@@ -60,3 +67,19 @@ export default class CKEditorError extends Error {
 		return error instanceof CKEditorError;
 	}
 }
+
+/**
+ * Attaches link to the documentation at the end of the error message.
+ *
+ * @param {String} message Message to be logged.
+ * @returns {String}
+ */
+export function attachLinkToDocumentation( message ) {
+	const matchedErrorName = message.match( /^([^:]+):/ );
+
+	if ( !matchedErrorName ) {
+		return message;
+	}
+
+	return message + ` Read more: ${ DOCUMENTATION_URL }#${ matchedErrorName[ 1 ] }.\n`;
+}

+ 1 - 15
packages/ckeditor5-utils/src/log.js

@@ -9,7 +9,7 @@
  * @module utils/log
  */
 
-const DOCUMENTATION_URL = 'https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/framework/guides/error-codes.html';
+import { attachLinkToDocumentation } from './ckeditorerror';
 
 /**
  * The logging module.
@@ -74,17 +74,3 @@ const log = {
 };
 
 export default log;
-
-// Attaches link to the documentation at the end of the log message.
-//
-// @param {String} message
-// @returns {String}
-function attachLinkToDocumentation( message ) {
-	const matchedErrorName = message.match( /^([^:]+):/ );
-
-	if ( !matchedErrorName ) {
-		return message;
-	}
-
-	return message + ` Read more: ${ DOCUMENTATION_URL }#${ matchedErrorName[ 1 ] }.\n`;
-}

+ 20 - 1
packages/ckeditor5-utils/tests/ckeditorerror.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md.
  */
 
-import CKEditorError from '../src/ckeditorerror';
+import { default as CKEditorError, DOCUMENTATION_URL } from '../src/ckeditorerror';
 
 describe( 'CKEditorError', () => {
 	it( 'inherits from Error', () => {
@@ -52,6 +52,25 @@ describe( 'CKEditorError', () => {
 		expect( error ).to.have.property( 'data', data );
 	} );
 
+	it( 'contains a link which leads to the documentation', () => {
+		const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.' );
+
+		const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
+			`Read more: ${ DOCUMENTATION_URL }#model-schema-no-item.\n`;
+
+		expect( error ).to.have.property( 'message', errorMessage );
+	} );
+
+	it( 'link to documentation is added before the additional data message', () => {
+		const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.', { foo: 1, bar: 2 } );
+
+		const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
+			`Read more: ${ DOCUMENTATION_URL }#model-schema-no-item.\n ` +
+			'{"foo":1,"bar":2}';
+
+		expect( error ).to.have.property( 'message', errorMessage );
+	} );
+
 	describe( 'isCKEditorError', () => {
 		it( 'checks if error is an instance of CKEditorError', () => {
 			const ckeditorError = new CKEditorError( 'foo' );

+ 3 - 6
packages/ckeditor5-utils/tests/log.js

@@ -6,6 +6,7 @@
 /* globals console */
 
 import log from '../src/log';
+import { DOCUMENTATION_URL } from '../src/ckeditorerror';
 
 describe( 'log', () => {
 	let spy;
@@ -36,10 +37,8 @@ describe( 'log', () => {
 
 			log.warn( 'model-schema-no-item: Specified item cannot be found.' );
 
-			/* eslint-disable max-len */
 			const logMessage = 'model-schema-no-item: Specified item cannot be found. ' +
-				'Read more: https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/framework/guides/error-codes.html#model-schema-no-item.\n';
-			/* eslint-enable max-len */
+				`Read more: ${ DOCUMENTATION_URL }#model-schema-no-item.\n`;
 
 			sinon.assert.calledOnce( spy );
 			sinon.assert.calledWith( spy, logMessage );
@@ -66,10 +65,8 @@ describe( 'log', () => {
 
 			log.error( 'model-schema-no-item: Specified item cannot be found.' );
 
-			/* eslint-disable max-len */
 			const logMessage = 'model-schema-no-item: Specified item cannot be found. ' +
-				'Read more: https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/framework/guides/error-codes.html#model-schema-no-item.\n';
-			/* eslint-enable max-len */
+				`Read more: ${ DOCUMENTATION_URL }#model-schema-no-item.\n`;
 
 			sinon.assert.calledOnce( spy );
 			sinon.assert.calledWith( spy, logMessage );