Last active
November 14, 2017 09:04
-
-
Save ngot/e0be4b9aa7e905add51039e5b2dfc4c5 to your computer and use it in GitHub Desktop.
v8 custom Error & Global Error
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
inline void ThrowError(const char* msg) | |
{ | |
Isolate* isolate = Isolate::current(); | |
// create custom error class/function | |
auto code = | |
"class MyError extends TypeError {" | |
"constructor(){" | |
"super();" | |
"this.name = 'MyError';" | |
"this.message = 'This is custom Error Object!" | |
"}" | |
"}" | |
"MyError;"; | |
auto errorFunc = v8::Script::Compile(isolate->NewString(code))->Run().As<v8::Object>(); | |
v8::Local<v8::Context> _context = isolate->context(); | |
v8::Local<v8::Object> glob = _context->Global(); | |
auto URIError = (glob->Get(isolate->NewString("URIError"))).As<v8::Object>(); | |
// create custom error instance | |
auto message = isolate->NewString("This is error!!!"); | |
v8::Local<v8::Value> args[] = { message }; | |
// auto error = URIError->CallAsConstructor(1, args); // URIError | |
auto error = errorFunc->CallAsConstructor(1, args); // MyError | |
isolate->ThrowException(error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment