Skip to content

Commit 5d42e64

Browse files
committed
fix: catch exceptions from SourceMapConsumer
1 parent 63db4b0 commit 5d42e64

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/reporter.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,15 @@ var createErrorFormatter = function(basePath, emitter, SourceMapConsumer) {
4646
column = parseInt(column || '0', 10);
4747

4848
var smc = new SourceMapConsumer(file.sourceMap);
49-
var original = smc.originalPositionFor({line: line, column: column});
49+
try {
50+
var original = smc.originalPositionFor({line: line, column: column});
5051

51-
return util.format('%s:%d:%d <- %s:%d:%d', path, line, column, original.source,
52+
return util.format('%s:%d:%d <- %s:%d:%d', path, line, column, original.source,
5253
original.line, original.column);
54+
} catch (e) {
55+
log.warn('SourceMap position not found for trace: %s', msg);
56+
// Fall back to non-source-mapped formatting.
57+
}
5358
}
5459

5560
return path + (line ? ':' + line : '') + (column ? ':' + column : '');

test/unit/reporter.spec.coffee

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ describe 'reporter', ->
7878
constructor: (sourceMap) ->
7979
@source = sourceMap.replace 'SOURCE MAP ', '/original/'
8080
originalPositionFor: (position) ->
81+
if position.line == 0
82+
throw new TypeError('Line must be greater than or equal to 1, got 0')
83+
8184
source: @source
8285
line: position.line + 2
8386
column: position.column + 2
@@ -95,6 +98,19 @@ describe 'reporter', ->
9598
expect(formatError ERROR).to.equal 'at /some/base/b.js:2:6 <- /original/b.js:4:8\n'
9699
done()
97100

101+
it 'should fall back to non-source-map format if originalPositionFor throws', (done) ->
102+
formatError = m.createErrorFormatter '/some/base', emitter, MockSourceMapConsumer
103+
servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
104+
servedFiles[0].sourceMap = 'SOURCE MAP a.js'
105+
servedFiles[1].sourceMap = 'SOURCE MAP b.js'
106+
107+
emitter.emit 'file_list_modified', q(served: servedFiles)
108+
109+
scheduleNextTick ->
110+
ERROR = 'at http://localhost:123/base/b.js:0:0'
111+
expect(formatError ERROR).to.equal 'at /some/base/b.js\n'
112+
done()
113+
98114
describe 'Windows', ->
99115
formatError = null
100116
servedFiles = null

0 commit comments

Comments
 (0)