Skip to content

Commit 4636688

Browse files
fix(reporter): Better handling of non string error
Ref karma-runner#1969, karma-runner#1988
1 parent b2cb31a commit 4636688

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/reporter.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var MultiReporter = require('./reporters/multi')
44
var baseReporterDecoratorFactory = require('./reporters/base').decoratorFactory
55
var SourceMapConsumer = require('source-map').SourceMapConsumer
66
var WeakMap = require('core-js/es6/weak-map')
7+
var _ = require('./helper')._
78

89
var createErrorFormatter = function (basePath, emitter, SourceMapConsumer) {
910
var lastServedFiles = []
@@ -39,10 +40,19 @@ var createErrorFormatter = function (basePath, emitter, SourceMapConsumer) {
3940
}
4041
}())
4142

42-
return function (msg, indentation) {
43+
return function (input, indentation) {
44+
indentation = _.isString(indentation) ? indentation : ''
45+
if (_.isError(input)) {
46+
input = input.message
47+
} else if (_.isEmpty(input)) {
48+
input = ''
49+
} else if (!_.isString(input)) {
50+
input = JSON.stringify(input, null, indentation)
51+
}
52+
4353
// remove domain and timestamp from source files
4454
// and resolve base path / absolute path urls into absolute path
45-
msg = (msg || '').replace(URL_REGEXP, function (_, prefix, path, __, ___, line, ____, column) {
55+
var msg = input.replace(URL_REGEXP, function (_, prefix, path, __, ___, line, ____, column) {
4656
if (prefix === 'base') {
4757
path = basePath + path
4858
}

test/unit/reporter.spec.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ describe('reporter', () => {
1111
m = loadFile(path.join(__dirname, '/../../lib/reporter.js'))
1212
})
1313

14-
// ==============================================================================
15-
// formatError() [PRIVATE]
16-
// ==============================================================================
1714
describe('formatError', () => {
1815
var emitter
1916
var formatError = emitter = null
@@ -31,6 +28,22 @@ describe('reporter', () => {
3128
expect(formatError(null)).to.equal('\n')
3229
})
3330

31+
it('should handle arbitrary error objects', () => {
32+
expect(
33+
formatError({hello: 'world'})
34+
).to.equal(
35+
JSON.stringify({hello: 'world'}) + '\n'
36+
)
37+
})
38+
39+
it('should handle error objects', () => {
40+
expect(
41+
formatError(new Error('fail'))
42+
).to.equal(
43+
'fail\n'
44+
)
45+
})
46+
3447
it('should remove domain from files', () => {
3548
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')
3649
})

0 commit comments

Comments
 (0)