Inherit from Error object

1,124 views
Skip to first unread message

Branko Vukelić

unread,
Aug 11, 2011, 8:38:03 AM8/11/11
to nod...@googlegroups.com
Is it possible to make a custom error object that inherits from the Error
object? I would like to associate a few extra pieces of data and still
keep the ability to have stack traces and such if the custom object is
thrown.

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

Bradley Meck

unread,
Aug 11, 2011, 8:43:22 AM8/11/11
to nod...@googlegroups.com
best way:

use require('util').inherits(myError, Error);

manual way:

do not set properties on the prototype before you set the prototype to a new object (MyError.prototype = ... should occur before you add any properties to the prototype)
set the prototype to new Error not Error
set the prototype.constructor to MyError
call Error.call(this) in the constructor

Branko Vukelić

unread,
Aug 11, 2011, 8:49:19 AM8/11/11
to nod...@googlegroups.com

Thanks for the tips!

Marcel Laverdet

unread,
Aug 11, 2011, 11:43:30 AM8/11/11
to nod...@googlegroups.com
If you are interested in stack traces, be sure to do this:
Error.captureStackTrace(this, MyError);

Even calling the Error ctor manually won't get you a stack trace, and error.stack will be empty.

2011/8/11 Branko Vukelić <bg.b...@gmail.com>
--
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

Isaac Schlueter

unread,
Aug 11, 2011, 11:56:13 AM8/11/11
to nod...@googlegroups.com
Inheriting from the Error class is a bit unusual. To do it properly,
you have to do some extra steps to get the stack trace properly. The
good news is that the API gives you quite a bit of control over this.

Check out node's lib/assert.js where it defines the AssertionError class.

Branko Vukelić

unread,
Aug 11, 2011, 12:19:06 PM8/11/11
to nod...@googlegroups.com
On 2011-08-11 08:56 -0700, Isaac Schlueter wrote:
> Check out node's lib/assert.js where it defines the AssertionError class.

Thanks.

Branko Vukelić

unread,
Aug 11, 2011, 12:20:00 PM8/11/11
to nod...@googlegroups.com
On 2011-08-11 10:43 -0500, Marcel Laverdet wrote:
> Error.captureStackTrace(this, MyError);

Is this done in the constructor or somewhere else?

Aseem Kishore

unread,
Aug 11, 2011, 1:14:09 PM8/11/11
to nod...@googlegroups.com
Yep, constructor.

2011/8/11 Branko Vukelić <bg.b...@gmail.com>

Branko Vukelić

unread,
Aug 11, 2011, 6:44:42 PM8/11/11
to nod...@googlegroups.com
On 2011-08-11 10:14 -0700, Aseem Kishore wrote:
> Yep, constructor.

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

Michael Jackson

unread,
Aug 11, 2011, 6:46:29 PM8/11/11
to nod...@googlegroups.com
Check out the custom Error subclass that Link uses:

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>:

Branko Vukelić

unread,
Aug 11, 2011, 6:56:06 PM8/11/11
to nod...@googlegroups.com
On 2011-08-11 15:46 -0700, Michael Jackson wrote:
> Check out the custom Error subclass that Link uses:
>
> 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.

Awesome! :)

Tim Smart

unread,
Aug 11, 2011, 7:39:51 PM8/11/11
to nod...@googlegroups.com
Some more examples: https://github.com/Tim-Smart/express-aid/blob/master/index.js#L66-113

Tim.

2011/8/12 Branko Vukelić <bg.b...@gmail.com>

Michael Jackson

unread,
Aug 11, 2011, 7:47:49 PM8/11/11
to nod...@googlegroups.com
Tim,

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

Nuno Job

unread,
Aug 11, 2011, 7:48:28 PM8/11/11
to nod...@googlegroups.com
This is what I did in the driver I'm writing:
Reply all
Reply to author
Forward
0 new messages