Skip to content

Commit 758b8d7

Browse files
committed
Call onerror on uncaught exception
1 parent c88ce39 commit 758b8d7

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

binding.cc

+48-29
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,52 @@ const char* ToCString(const String::Utf8Value& value) {
4444
return *value ? *value : "<string conversion failed>";
4545
}
4646

47+
/*
48+
bool AbortOnUncaughtExceptionCallback(Isolate* isolate) {
49+
return true;
50+
}
51+
52+
void MessageCallback2(Local<Message> message, Local<Value> data) {
53+
printf("MessageCallback2\n\n");
54+
}
55+
56+
void FatalErrorCallback2(const char* location, const char* message) {
57+
printf("FatalErrorCallback2\n");
58+
}
59+
*/
60+
61+
void ExitOnPromiseRejectCallback(PromiseRejectMessage promise_reject_message) {
62+
auto isolate = Isolate::GetCurrent();
63+
worker* w = (worker*)isolate->GetData(0);
64+
assert(w->isolate == isolate);
65+
HandleScope handle_scope(w->isolate);
66+
auto context = w->context.Get(w->isolate);
67+
68+
auto exception = promise_reject_message.GetValue();
69+
70+
auto message = Exception::CreateMessage(isolate, exception);
71+
auto onerrorStr = String::NewFromUtf8(w->isolate, "onerror");
72+
auto onerror = context->Global()->Get(onerrorStr);
73+
74+
if (onerror->IsFunction()) {
75+
Local<Function> func = Local<Function>::Cast(onerror);
76+
Local<Value> args[5];
77+
auto origin = message->GetScriptOrigin();
78+
args[0] = exception->ToString();
79+
args[1] = message->GetScriptResourceName();
80+
args[2] = origin.ResourceLineOffset();
81+
args[3] = origin.ResourceColumnOffset();
82+
args[4] = exception;
83+
func->Call(context->Global(), 5, args);
84+
/* message, source, lineno, colno, error */
85+
} else {
86+
printf("Unhandled Promise\n");
87+
message->PrintCurrentStackTrace(isolate, stdout);
88+
}
89+
90+
exit(1);
91+
}
92+
4793
// Exception details will be appended to the first argument.
4894
std::string ExceptionString(worker* w, TryCatch* try_catch) {
4995
std::string out;
@@ -176,8 +222,7 @@ void Recv(const FunctionCallbackInfo<Value>& args) {
176222

177223
HandleScope handle_scope(isolate);
178224

179-
Local<Context> context = Local<Context>::New(w->isolate, w->context);
180-
Context::Scope context_scope(context);
225+
auto context = w->context.Get(w->isolate);
181226

182227
Local<Value> v = args[0];
183228
assert(v->IsFunction());
@@ -195,8 +240,7 @@ void Send(const FunctionCallbackInfo<Value>& args) {
195240
Locker locker(w->isolate);
196241
EscapableHandleScope handle_scope(isolate);
197242

198-
Local<Context> context = Local<Context>::New(w->isolate, w->context);
199-
Context::Scope context_scope(context);
243+
auto context = w->context.Get(w->isolate);
200244

201245
Local<Value> v = args[0];
202246
assert(v->IsArrayBuffer());
@@ -262,31 +306,6 @@ void v8_init() {
262306
V8::Initialize();
263307
}
264308

265-
/*
266-
bool AbortOnUncaughtExceptionCallback(Isolate* isolate) {
267-
return true;
268-
}
269-
270-
void MessageCallback2(Local<Message> message, Local<Value> data) {
271-
printf("MessageCallback2\n\n");
272-
}
273-
274-
void FatalErrorCallback2(const char* location, const char* message) {
275-
printf("FatalErrorCallback2\n");
276-
}
277-
*/
278-
279-
void ExitOnPromiseRejectCallback(PromiseRejectMessage message) {
280-
auto exception = message.GetValue();
281-
282-
auto isolate = Isolate::GetCurrent();
283-
auto m = Exception::CreateMessage(isolate, exception);
284-
285-
printf("Unhandled Promise\n");
286-
m->PrintCurrentStackTrace(isolate, stdout);
287-
exit(1);
288-
}
289-
290309
worker* worker_new(int table_index) {
291310
worker* w = new (worker);
292311

0 commit comments

Comments
 (0)