Skip to content

Commit

Permalink
fix(reporter): Better handling of non string error
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Mar 19, 2016
1 parent 288f5d5 commit 82f1c12
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
14 changes: 12 additions & 2 deletions lib/reporter.js
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 16 additions & 3 deletions test/unit/reporter.spec.js
Expand Up @@ -11,9 +11,6 @@ describe('reporter', () => {
m = loadFile(path.join(__dirname, '/../../lib/reporter.js'))
})

// ==============================================================================
// formatError() [PRIVATE]
// ==============================================================================
describe('formatError', () => {
var emitter
var formatError = emitter = null
Expand All @@ -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')
})
Expand Down

0 comments on commit 82f1c12

Please sign in to comment.