浏览代码

Add error data to the error message, to make the message more useful.

Piotrek Koszuliński 10 年之前
父节点
当前提交
6470c0b55b

+ 7 - 1
packages/ckeditor5-utils/src/ckeditorerror.js

@@ -28,9 +28,15 @@ CKEDITOR.define( function() {
 		 * @param {String} message The error message in an `error-name: Error message.` format.
 		 * During the minification process the "Error message" part will be removed to limit the code size
 		 * and a link to this error documentation will be added to the `message`.
-		 * @param {Object} [data] Additional data describing the error.
+		 * @param {Object} [data] Additional data describing the error. A stringified version of this object
+		 * will be appended to the error {@link #message}, so the data are quickly visible in the console. The original
+		 * data object will also be later available under the {@link #data} property.
 		 */
 		constructor( message, data ) {
+			if ( data ) {
+				message += ' ' + JSON.stringify( data );
+			}
+
 			super( message );
 
 			this.name = 'CKEditorError';

+ 23 - 2
packages/ckeditor5-utils/tests/ckeditorerror/ckeditorerror.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
+/* globals document */
+
 'use strict';
 
 var modules = bender.amd.require( 'ckeditorerror' );
@@ -36,7 +38,26 @@ describe( 'CKEditorError', function() {
 		var data = { bar: 1 };
 		var error = new CKEditorError( 'foo', data );
 
-		expect( error ).to.have.property( 'message', 'foo' );
+		expect( error ).to.have.property( 'message', 'foo {"bar":1}' );
+		expect( error ).to.have.property( 'data', data );
+	} );
+
+	it( 'appends stringified data to the message', function() {
+		class Foo {
+			constructor() {
+				this.x = 1;
+			}
+		}
+
+		var CKEditorError = modules.ckeditorerror;
+		var data = {
+			bar: 'a',
+			bom: new Foo(),
+			bim: document.body
+		};
+		var error = new CKEditorError( 'foo', data );
+
+		expect( error ).to.have.property( 'message', 'foo {"bar":"a","bom":{"x":1},"bim":{}}' );
 		expect( error ).to.have.property( 'data', data );
 	} );
-} );
+} );