Created
November 15, 2012 14:19
-
-
Save Evangenieur/4078830 to your computer and use it in GitHub Desktop.
Node.js Javascript / CoffeeScript Stack Trace Obj with line from code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StackObjError = require "stack_trace_with_line_from_code" | |
first_func = -> | |
second_func = -> | |
console.log JSON.stringify(new StackObjError().stack) | |
second_func() | |
first_func() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"where": "second_func", | |
"file": "/home/user/test/main.coffee", | |
"row": "49", | |
"col": "41", | |
"code": " return console.log(JSON.stringify(new StackObjError().stack));" | |
}, | |
{ | |
"where": "first_func", | |
"file": "/home/user/test/main.coffee", | |
"row": "51", | |
"col": "12", | |
"code": " return second_func();" | |
}, | |
{ | |
"where": "Object.<anonymous>", | |
"file": "/home/user/test/main.coffee", | |
"row": "54", | |
"col": "3", | |
"code": " first_func();" | |
}, | |
{ | |
"where": "Object.<anonymous>", | |
"file": "/home/user/test/main.coffee", | |
"row": "105", | |
"col": "4", | |
"code": "}).call(this);" | |
}, | |
{ "where": "Module._compile", "file": "module.js", "row": "449", "col": "26" }, | |
{ | |
"where": "Object.exports.run", | |
"file": "/opt/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js", | |
"row": "83", | |
"col": "25", | |
"code": " return mainModule._compile(compile(code, options), mainModule.filename);" | |
}, | |
{ | |
"where": "compileScript", | |
"file": "/opt/local/lib/node_modules/coffee-script/lib/coffee-script/command.js", | |
"row": "177", | |
"col": "29", | |
"code": " return CoffeeScript.run(t.input, t.options);" | |
}, | |
" at fs.stat.notSources.(anonymous function) (/opt/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:152:18)" | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CoffeeScript = require "coffee-script" | |
_ = require "underscore" | |
fs = require "fs" | |
coffeeFile2js = _.memoize (filename) -> | |
CoffeeScript.compile \ | |
fs.readFileSync(filename, "utf-8") | |
jsFile = _.memoize (filename) -> | |
fs.readFileSync(filename, "utf-8") | |
StackObjError = -> | |
err = Error.apply @, arguments | |
@__defineGetter__ "stack", -> | |
#console.log err.stack | |
_(err.stack.split(/\n/)[3..-1]).map (trace) -> | |
if matches = trace.match /at ([^\(]+) \(([^:]+):(\d+):(\d+)\)/ | |
#console.log "file #{matches[2]}, line #{matches[3]}, col #{matches[4]}" | |
trace = | |
where: matches[1] | |
file: matches[2] | |
row: matches[3] | |
col: matches[4] | |
if fs.existsSync trace.file | |
trace.code = ( | |
if trace.file.match /\.coffee/ | |
coffeeFile2js trace.file | |
else if trace.file | |
jsFile trace.file | |
).split(/\n/)[trace.row - 1] | |
trace | |
StackObjError:: = new StackObjError() | |
StackObjError::constructor = StackObjError | |
StackObjError::name = "StackObjError" | |
module.exports = StackObjError | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment