Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #186 from ckeditor/t/185
Browse files Browse the repository at this point in the history
Other: `CKEditorError.message`, `log.error()` and `log.warn()` will contain a link to the error documentation. Closes #185.
  • Loading branch information
Reinmar authored Sep 22, 2017
2 parents a5c27ea + e6a4994 commit b7a00c9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/ckeditorerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 );
}
Expand Down Expand Up @@ -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`;
}
6 changes: 4 additions & 2 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @module utils/log
*/

import { attachLinkToDocumentation } from './ckeditorerror';

/**
* The logging module.
*
Expand Down Expand Up @@ -53,7 +55,7 @@ const log = {
* @param {Object} [data] Additional data describing the error.
*/
error( message, data ) {
console.error( message, data );
console.error( attachLinkToDocumentation( message ), data );
},

/**
Expand All @@ -67,7 +69,7 @@ const log = {
* @param {Object} [data] Additional data describing the warning.
*/
warn( message, data ) {
console.warn( message, data );
console.warn( attachLinkToDocumentation( message ), data );
}
};

Expand Down
21 changes: 20 additions & 1 deletion tests/ckeditorerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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' );
Expand Down
29 changes: 27 additions & 2 deletions tests/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/* globals console */

import log from '../src/log';
import { DOCUMENTATION_URL } from '../src/ckeditorerror';

describe( 'log', () => {
let spy;
Expand All @@ -18,7 +19,7 @@ describe( 'log', () => {

describe( 'warn()', () => {
it( 'logs the message to the console using console.warn()', () => {
const spy = sinon.stub( console, 'warn' );
spy = sinon.stub( console, 'warn' );
const data = { bar: 1 };

log.warn( 'foo', data );
Expand All @@ -30,11 +31,23 @@ describe( 'log', () => {
sinon.assert.calledTwice( spy );
sinon.assert.calledWith( spy, 'bar' );
} );

it( 'contains a link which leads to the documentation', () => {
spy = sinon.stub( console, 'warn' );

log.warn( 'model-schema-no-item: Specified item cannot be found.' );

const logMessage = 'model-schema-no-item: Specified item cannot be found. ' +
`Read more: ${ DOCUMENTATION_URL }#model-schema-no-item.\n`;

sinon.assert.calledOnce( spy );
sinon.assert.calledWith( spy, logMessage );
} );
} );

describe( 'error()', () => {
it( 'logs the message to the console using console.error()', () => {
const spy = sinon.stub( console, 'error' );
spy = sinon.stub( console, 'error' );
const data = { bar: 1 };

log.error( 'foo', data );
Expand All @@ -46,5 +59,17 @@ describe( 'log', () => {
sinon.assert.calledTwice( spy );
sinon.assert.calledWith( spy, 'bar' );
} );

it( 'contains a link which leads to the documentation', () => {
spy = sinon.stub( console, 'error' );

log.error( 'model-schema-no-item: Specified item cannot be found.' );

const logMessage = 'model-schema-no-item: Specified item cannot be found. ' +
`Read more: ${ DOCUMENTATION_URL }#model-schema-no-item.\n`;

sinon.assert.calledOnce( spy );
sinon.assert.calledWith( spy, logMessage );
} );
} );
} );

0 comments on commit b7a00c9

Please sign in to comment.