Skip to content

Commit

Permalink
add useful stack traces
Browse files Browse the repository at this point in the history
  • Loading branch information
daaku committed Aug 17, 2011
1 parent 7ab2e8b commit ce6fc0a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/makeerror.js
Expand Up @@ -56,6 +56,24 @@ function makeError(name, defaultMessage, defaultData) {
} else {
this.message = defaultMessage(this.data)
}

var er = new Error()
this.stack = er.stack
if (this.stack) {
// remove TWO stack level:
if (typeof Components !== 'undefined') {
// Mozilla:
this.stack = this.stack.substring(this.stack.indexOf('\n') + 2)
} else if (typeof chrome !== 'undefined' || typeof process !== 'undefined') {
// Google Chrome/Node.js:
this.stack = this.stack.replace(/\n[^\n]*/, '')
this.stack = this.stack.replace(/\n[^\n]*/, '')
this.stack = this.name + this.stack.substring(5)
}
}

if ('fileName' in er) this.fileName = er.fileName
if ('lineNumber' in er) this.lineNumber = er.lineNumber
}

CustomError.prototype = defaultData.proto || new BaseError()
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "makeerror",
"description": "A library to make errors.",
"version": "1.0.5",
"version": "1.0.6",
"author": "Naitik Shah <n@daaku.org>",
"main": "lib/makeerror",
"repository": {
Expand Down
9 changes: 8 additions & 1 deletion test/makeerror-tests.js
Expand Up @@ -140,5 +140,12 @@ exports['proto must be created via makeError'] = function(beforeExit) {
makeError('Child', '', { proto: new Error() })
},
/created via makeError/
);
)
}

exports['stack trace'] = function(beforeExit) {
var name = 'MyError'
, MyError = makeError(name)
, er = MyError()
assert.match(er.stack, new RegExp(name + '\\n *at ' + __filename))
}

0 comments on commit ce6fc0a

Please sign in to comment.