Created
June 27, 2012 00:09
-
-
Save mmalecki/3000364 to your computer and use it in GitHub Desktop.
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
function getLine(offset) { | |
var stack = new Error().stack.split('\n'), | |
line = stack[(offset || 1) + 1].split(':'); | |
return parseInt(line[line.length - 2], 10); | |
} | |
global.__defineGetter__('__LINE__', function () { | |
return getLine(2); | |
}); | |
console.log(__LINE__); | |
console.log(getLine()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modified this to
And now I can pass in an error object after catching it and before re-throwing to preserve the line where it occurred, where I would normally get the line that I re-throw the error at in Node.JS. A bit more complex than the below, but this seems like a good example:
Of course this requires looking at the error message, but that's what logging is for, and I think it will make [my] life a little easier by helping to find some bugs that get past the standard-js linter like the above example.
Thank you :D
Edits: eventually fixed formatting. I really hate MD sometimes. https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet helped.
Another edit: That's also without assigning it to global or window; just using it as a private function in a module without that part.