Creating custom Error classes from C++ code in V8
I want to create custom exceptions in a native NodeJS (0.11.5) extension.
I'm trying to create subclasses of the builtin Error class in V8
(3.20.11).
Basically I'm looking for a C++ equivalent of the following Javascript code:
function MyError(message) {
Error.apply(this, arguments);
this.message = message;
}
MyError.prototype = new Error;
MyError.prototype.name = MyError.name;
new MyError("message")
I tried digging in the V8 source and I found the following helper method
that appears to do part of what I want:
Handle<Object> Factory::NewError(const char* constructor,
Handle<String> message)
Unfortunately it looks like it's a private API and I don't understand
enough V8 to figure out how to construct something similar myself. It
would be great to be able to write a method that I could use in a similar
way as creating a built-in Error instance, for example:
ThrowException(v8::Exception::Error(v8::String::New(msg)))
// becomes...
ThrowException(MyError(v8::String::New(msg)))
I'm looking for a solution that is as close as possible to a subclass of
the builtin Error class. It should probably satisfy the following:
var e = new MyError("message");
assert(e instanceof MyError);
assert(e instanceof Error);
assert(e.name === "MyError");
assert(e.message === "message");
Any suggestions where to start?
No comments:
Post a Comment