Skip to content

Commit aee27fb

Browse files
authored
Merge pull request jashkenas#4399 from murrayju/issue4391
Add a try/catch in the Error.prepareStackTrace implementation
2 parents 26cfd19 + 2858543 commit aee27fb

File tree

2 files changed

+0
-91
lines changed

2 files changed

+0
-91
lines changed

src/coffee-script.coffee

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -280,83 +280,3 @@ parser.yy.parseError = (message, {token}) ->
280280
# from the lexer.
281281
helpers.throwSyntaxError "unexpected #{errorText}", errorLoc
282282

283-
# Based on http://v8.googlecode.com/svn/branches/bleeding_edge/src/messages.js
284-
# Modified to handle sourceMap
285-
formatSourcePosition = (frame, getSourceMapping) ->
286-
fileName = undefined
287-
fileLocation = ''
288-
289-
if frame.isNative()
290-
fileLocation = "native"
291-
else
292-
if frame.isEval()
293-
fileName = frame.getScriptNameOrSourceURL()
294-
fileLocation = "#{frame.getEvalOrigin()}, " unless fileName
295-
else
296-
fileName = frame.getFileName()
297-
298-
fileName or= "<anonymous>"
299-
300-
line = frame.getLineNumber()
301-
column = frame.getColumnNumber()
302-
303-
# Check for a sourceMap position
304-
source = getSourceMapping fileName, line, column
305-
fileLocation =
306-
if source
307-
"#{fileName}:#{source[0]}:#{source[1]}"
308-
else
309-
"#{fileName}:#{line}:#{column}"
310-
311-
functionName = frame.getFunctionName()
312-
isConstructor = frame.isConstructor()
313-
isMethodCall = not (frame.isToplevel() or isConstructor)
314-
315-
if isMethodCall
316-
methodName = frame.getMethodName()
317-
typeName = frame.getTypeName()
318-
319-
if functionName
320-
tp = as = ''
321-
if typeName and functionName.indexOf typeName
322-
tp = "#{typeName}."
323-
if methodName and functionName.indexOf(".#{methodName}") isnt functionName.length - methodName.length - 1
324-
as = " [as #{methodName}]"
325-
326-
"#{tp}#{functionName}#{as} (#{fileLocation})"
327-
else
328-
"#{typeName}.#{methodName or '<anonymous>'} (#{fileLocation})"
329-
else if isConstructor
330-
"new #{functionName or '<anonymous>'} (#{fileLocation})"
331-
else if functionName
332-
"#{functionName} (#{fileLocation})"
333-
else
334-
fileLocation
335-
336-
# Map of filenames -> sourceMap object.
337-
sourceMaps = {}
338-
339-
# Generates the source map for a coffee file and stores it in the local cache variable.
340-
getSourceMap = (filename) ->
341-
return sourceMaps[filename] if sourceMaps[filename]
342-
for ext in exports.FILE_EXTENSIONS
343-
if helpers.ends filename, ext
344-
answer = exports._compileFile filename, true
345-
return sourceMaps[filename] = answer.sourceMap
346-
return null
347-
348-
# Based on [michaelficarra/CoffeeScriptRedux](http://goo.gl/ZTx1p)
349-
# NodeJS / V8 have no support for transforming positions in stack traces using
350-
# sourceMap, so we must monkey-patch Error to display CoffeeScript source
351-
# positions.
352-
Error.prepareStackTrace = (err, stack) ->
353-
getSourceMapping = (filename, line, column) ->
354-
sourceMap = getSourceMap filename
355-
answer = sourceMap.sourceLocation [line - 1, column - 1] if sourceMap
356-
if answer then [answer[0] + 1, answer[1] + 1] else null
357-
358-
frames = for frame in stack
359-
break if frame.getFunction() is exports.run
360-
" at #{formatSourcePosition frame, getSourceMapping}"
361-
362-
"#{err.toString()}\n#{frames.join '\n'}\n"

test/error_messages.coffee

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,6 @@ if require?
5757
fs = require 'fs'
5858
path = require 'path'
5959

60-
test "patchStackTrace line patching", ->
61-
err = new Error 'error'
62-
ok err.stack.match /test[\/\\]error_messages\.coffee:\d+:\d+\b/
63-
64-
test "patchStackTrace stack prelude consistent with V8", ->
65-
err = new Error
66-
ok err.stack.match /^Error\n/ # Notice no colon when no message.
67-
68-
err = new Error 'error'
69-
ok err.stack.match /^Error: error\n/
70-
7160
test "#2849: compilation error in a require()d file", ->
7261
# Create a temporary file to require().
7362
ok not fs.existsSync 'test/syntax-error.coffee'

0 commit comments

Comments
 (0)