I've tried this method:
function MyError(msg, someData) {
this.msg = msg;
this.data = someData;
}
MyError.prototype.toString = function() {
return 'ERR: ' + this.msg + ' ' + this.data.toString();
};
MyError.prototype = Error;
Of course, the above doesn't really work. So how do I do this properly?
Or do I avoid this pattern?
--
Branko Vukelic
bra...@herdhound.com
bg.b...@gmail.com
Lead Developer
Herd Hound (tm) - Travel that doesn't bite
www.herdhound.com
Love coffee? You might love Loveffee, too.
loveffee.appspot.com
Thanks for the tips!
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
Check out node's lib/assert.js where it defines the AssertionError class.
Thanks.
Is this done in the constructor or somewhere else?
Thanks everyone, it's working beatifully now. If anyone stumbles upon
this convo later looking for the same things, here's the V8
documentation on stack traces that might be useful:
http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
https://github.com/mjijackson/link/blob/master/lib/link.js#L153
It should be exactly what you need, plus it allows itself to be
subclassed and contain nested errors, while preserving the full stack
trace.
Michael
2011/8/11 Branko Vukelić <bg.b...@gmail.com>:
Awesome! :)
It's slightly better to use
Error.captureStackTrace(this, this.constructor)
instead of
Error.captureStackTrace(this, arguments.callee)
if you want to omit the call to the constructor from the stack trace.
"this.constructor" will always be a reference to the error's
constructor function, but "arguments.callee" will always be a
reference to the same parent constructor, even when calling it from
inside a "subclass" constructor.
Michael