From 82f1c1207b34955602b7590a34f8bf50b1a5ba6a Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Sat, 19 Mar 2016 14:04:39 +0100 Subject: [PATCH] fix(reporter): Better handling of non string error Ref #1969, #1988 --- lib/reporter.js | 14 ++++++++++++-- test/unit/reporter.spec.js | 19 ++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/reporter.js b/lib/reporter.js index cac9deea3..5d1ae96a4 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -4,6 +4,7 @@ var MultiReporter = require('./reporters/multi') var baseReporterDecoratorFactory = require('./reporters/base').decoratorFactory var SourceMapConsumer = require('source-map').SourceMapConsumer var WeakMap = require('core-js/es6/weak-map') +var _ = require('./helper')._ var createErrorFormatter = function (basePath, emitter, SourceMapConsumer) { var lastServedFiles = [] @@ -39,10 +40,19 @@ var createErrorFormatter = function (basePath, emitter, SourceMapConsumer) { } }()) - return function (msg, indentation) { + return function (input, indentation) { + indentation = _.isString(indentation) ? indentation : '' + if (_.isError(input)) { + input = input.message + } else if (_.isEmpty(input)) { + input = '' + } else if (!_.isString(input)) { + input = JSON.stringify(input, null, indentation) + } + // remove domain and timestamp from source files // and resolve base path / absolute path urls into absolute path - msg = (msg || '').replace(URL_REGEXP, function (_, prefix, path, __, ___, line, ____, column) { + var msg = input.replace(URL_REGEXP, function (_, prefix, path, __, ___, line, ____, column) { if (prefix === 'base') { path = basePath + path } diff --git a/test/unit/reporter.spec.js b/test/unit/reporter.spec.js index e40d8298c..a60692d6c 100644 --- a/test/unit/reporter.spec.js +++ b/test/unit/reporter.spec.js @@ -11,9 +11,6 @@ describe('reporter', () => { m = loadFile(path.join(__dirname, '/../../lib/reporter.js')) }) - // ============================================================================== - // formatError() [PRIVATE] - // ============================================================================== describe('formatError', () => { var emitter var formatError = emitter = null @@ -31,6 +28,22 @@ describe('reporter', () => { expect(formatError(null)).to.equal('\n') }) + it('should handle arbitrary error objects', () => { + expect( + formatError({hello: 'world'}) + ).to.equal( + JSON.stringify({hello: 'world'}) + '\n' + ) + }) + + it('should handle error objects', () => { + expect( + formatError(new Error('fail')) + ).to.equal( + 'fail\n' + ) + }) + it('should remove domain from files', () => { expect(formatError('file http://localhost:8080/base/usr/a.js and http://127.0.0.1:8080/base/home/b.js')).to.be.equal('file /usr/a.js and /home/b.js\n') })