Skip to content

Commit

Permalink
fix: catch exceptions from SourceMapConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
afischer-sfly committed Mar 26, 2015
1 parent 63db4b0 commit 5d42e64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ var createErrorFormatter = function(basePath, emitter, SourceMapConsumer) {
column = parseInt(column || '0', 10);

var smc = new SourceMapConsumer(file.sourceMap);
var original = smc.originalPositionFor({line: line, column: column});
try {
var original = smc.originalPositionFor({line: line, column: column});

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

return path + (line ? ':' + line : '') + (column ? ':' + column : '');
Expand Down
16 changes: 16 additions & 0 deletions test/unit/reporter.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ describe 'reporter', ->
constructor: (sourceMap) ->
@source = sourceMap.replace 'SOURCE MAP ', '/original/'
originalPositionFor: (position) ->
if position.line == 0
throw new TypeError('Line must be greater than or equal to 1, got 0')

source: @source
line: position.line + 2
column: position.column + 2
Expand All @@ -95,6 +98,19 @@ describe 'reporter', ->
expect(formatError ERROR).to.equal 'at /some/base/b.js:2:6 <- /original/b.js:4:8\n'
done()

it 'should fall back to non-source-map format if originalPositionFor throws', (done) ->
formatError = m.createErrorFormatter '/some/base', emitter, MockSourceMapConsumer
servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
servedFiles[0].sourceMap = 'SOURCE MAP a.js'
servedFiles[1].sourceMap = 'SOURCE MAP b.js'

emitter.emit 'file_list_modified', q(served: servedFiles)

scheduleNextTick ->
ERROR = 'at https://localhost:123/base/b.js:0:0'
expect(formatError ERROR).to.equal 'at /some/base/b.js\n'
done()

describe 'Windows', ->
formatError = null
servedFiles = null
Expand Down

0 comments on commit 5d42e64

Please sign in to comment.