@@ -404,13 +404,68 @@ class TryCatch : public v8::TryCatch {
404404 explicit TryCatch (napi_env env) : v8::TryCatch(env->isolate), _env(env) {}
405405
406406 ~TryCatch () {
407+ // [BABYLON-NATIVE-ADDITION]
407408 if (HasCaught ()) {
408- _env->last_exception .Reset (_env->isolate , Exception ());
409+ v8::Local<v8::Value> exception = Exception ();
410+ EnhanceV8Exception (exception);
411+ _env->last_exception .Reset (_env->isolate , exception);
409412 }
413+ // [BABYLON-NATIVE-ADDITION]
410414 }
411415
412416 private:
413417 napi_env _env;
418+
419+ // [BABYLON-NATIVE-ADDITION]
420+ void EnhanceV8Exception (v8::Local<v8::Value> exception) {
421+ v8::Local<v8::Object> exception_obj = exception.As <v8::Object>();
422+
423+ v8::Isolate* isolate = _env->isolate ;
424+
425+ v8::Local<v8::Message> message = Message ();
426+ if (!message.IsEmpty ()) {
427+ v8::Local<v8::Context> context (isolate->GetCurrentContext ());
428+
429+ v8::ScriptOrigin script_origin = message->GetScriptOrigin ();
430+ v8::Local<v8::Value> filename = script_origin.ResourceName ();
431+
432+ setLocalValueAsProperty (context, exception_obj, filename, " url" );
433+
434+ int column_start = message->GetStartColumn ();
435+ int column_end = message->GetEndColumn ();
436+ int length = column_end - column_start;
437+ setIntAsProperty (isolate, context, exception_obj, message->GetLineNumber (context).ToChecked (), " line" );
438+ setIntAsProperty (isolate, context, exception_obj, column_start, " column" );
439+ setIntAsProperty (isolate, context, exception_obj, length, " length" );
440+
441+ v8::Local<v8::String> sourceLine = message->GetSourceLine (context).ToLocalChecked ();
442+ setLocalValueAsProperty (context, exception_obj, sourceLine, " source" );
443+
444+ v8::Local<v8::Value> stack_trace_string;
445+ if (StackTrace (context).ToLocal (&stack_trace_string) &&
446+ stack_trace_string->IsString () &&
447+ stack_trace_string.As <v8::String>()->Length () > 0 ) {
448+ setLocalValueAsProperty (context, exception_obj, stack_trace_string, " stack" );
449+ }
450+ }
451+ }
452+
453+ // [BABYLON-NATIVE-ADDITION]
454+ void setIntAsProperty (v8::Isolate* isolate, v8::Local<v8::Context> context, v8::Local<v8::Object> obj, int value, const char * value_property) {
455+ v8::Local<v8::Number> v8_number = v8::Number::New (isolate, value);
456+ setLocalValueAsProperty (context, obj, v8_number, value_property);
457+ }
458+
459+ // [BABYLON-NATIVE-ADDITION]
460+ void setLocalValueAsProperty (v8::Local<v8::Context> context, v8::Local<v8::Object> obj, v8::Local<v8::Value> value, const char * value_property) {
461+ v8::Local<v8::String> key = createV8String (value_property);
462+ obj->Set (context, key, value);
463+ }
464+
465+ // [BABYLON-NATIVE-ADDITION]
466+ v8::Local<v8::String> createV8String (const char * text) {
467+ return v8::String::NewFromUtf8 (_env->isolate , text).ToLocalChecked ();
468+ }
414469};
415470
416471// Ownership of a reference.
0 commit comments