From 5d42e64373345f1beed95218983861f77352c16c Mon Sep 17 00:00:00 2001 From: Andrew Fischer Date: Wed, 11 Jun 2014 09:43:53 -0700 Subject: [PATCH] fix: catch exceptions from SourceMapConsumer --- lib/reporter.js | 9 +++++++-- test/unit/reporter.spec.coffee | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/reporter.js b/lib/reporter.js index d7881b710..a674e1ed8 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -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 : ''); diff --git a/test/unit/reporter.spec.coffee b/test/unit/reporter.spec.coffee index 0cb587e86..c4016cb47 100644 --- a/test/unit/reporter.spec.coffee +++ b/test/unit/reporter.spec.coffee @@ -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 @@ -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 http://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