Skip to content

Commit d1a3abe

Browse files
committed
fix: error handling:
- end process for uncaught js exceptions - remove extra try/catch statements around js callbacks
1 parent 3f5c4ad commit d1a3abe

3 files changed

Lines changed: 19 additions & 23 deletions

File tree

NativeScript/runtime/ArgConverter.mm

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,35 +125,22 @@
125125
}
126126

127127
Local<Value> result;
128-
TryCatch tc(isolate);
129128
Local<v8::Function> callback = poCallback->Get(isolate).As<v8::Function>();
130129
if (!callback->Call(context, thiz, (int)v8Args.size(), v8Args.data()).ToLocal(&result)) {
131130
memset(retValue, 0, cif->rtype->size);
132-
throw NativeScriptException(isolate, tc, "Error calling function");
131+
return;
133132
}
134133

135134
ArgConverter::SetValue(isolate, retValue, result, data->typeEncoding_);
136135
};
137136

138137
if ([NSThread isMainThread]) {
139-
try {
140-
cb();
141-
} catch (NativeScriptException& ex) {
142-
MethodCallbackWrapper* data = static_cast<MethodCallbackWrapper*>(userData);
143-
Isolate* isolate = data->isolate_;
144-
ex.ReThrowToV8(isolate);
145-
}
138+
cb();
146139
} else {
147140
dispatch_group_t group = dispatch_group_create();
148141
dispatch_group_enter(group);
149-
tns::ExecuteOnMainThread([cb, group, userData]() {
150-
try {
151-
cb();
152-
} catch (NativeScriptException& ex) {
153-
MethodCallbackWrapper* data = static_cast<MethodCallbackWrapper*>(userData);
154-
Isolate* isolate = data->isolate_;
155-
ex.ReThrowToV8(isolate);
156-
}
142+
tns::ExecuteOnMainThread([cb, group]() {
143+
cb();
157144
dispatch_group_leave(group);
158145
});
159146

NativeScript/runtime/NativeScriptException.cpp renamed to NativeScript/runtime/NativeScriptException.mm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ void NativeScriptException::OnUncaughtError(Local<Message> message, Local<Value>
2727
Local<Value> handler;
2828
bool success = global->Get(context, tns::ToV8String(isolate, "__onUncaughtError")).ToLocal(&handler);
2929

30+
std::string stackTrace = GetErrorStackTrace(isolate, message->GetStackTrace());
3031
if (success && handler->IsFunction()) {
31-
std::string stackTrace = GetErrorStackTrace(isolate, message->GetStackTrace());
3232
if (error->IsObject()) {
3333
assert(error.As<Object>()->Set(context, tns::ToV8String(isolate, "stackTrace"), tns::ToV8String(isolate, stackTrace)).FromMaybe(false));
3434
}
@@ -40,6 +40,15 @@ void NativeScriptException::OnUncaughtError(Local<Message> message, Local<Value>
4040
success = errorHandlerFunc->Call(context, thiz, 1, args).ToLocal(&result);
4141
assert(success);
4242
}
43+
44+
Local<v8::String> messageV8String = message->Get();
45+
std::string messageString = tns::ToString(isolate, messageV8String);
46+
NSString* name = [NSString stringWithFormat:@"NativeScript encountered a fatal error: %s\n at \n%s", messageString.c_str(), stackTrace.c_str()];
47+
NSException* objcException = [NSException exceptionWithName:name reason:nil userInfo:@{ @"sender": @"onUncaughtError" }];
48+
49+
NSLog(@"***** Fatal JavaScript exception - application has been terminated. *****\n");
50+
NSLog(@"%@", [objcException description]);
51+
@throw objcException;
4352
}
4453

4554
void NativeScriptException::ReThrowToV8(Isolate* isolate) {

v8ios.xcodeproj/project.pbxproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
2B7EA6AF2353477000E5184E /* NativeScriptException.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2B7EA6AD2353476F00E5184E /* NativeScriptException.cpp */; };
10+
2B7EA6AF2353477000E5184E /* NativeScriptException.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2B7EA6AD2353476F00E5184E /* NativeScriptException.mm */; };
1111
2B7EA6B02353477000E5184E /* NativeScriptException.h in Headers */ = {isa = PBXBuildFile; fileRef = 2B7EA6AE2353477000E5184E /* NativeScriptException.h */; };
1212
C2229973235449B400C1DFD6 /* InspectorServer.mm in Sources */ = {isa = PBXBuildFile; fileRef = C2229971235449B300C1DFD6 /* InspectorServer.mm */; };
1313
C2229974235449B400C1DFD6 /* InspectorServer.h in Headers */ = {isa = PBXBuildFile; fileRef = C2229972235449B400C1DFD6 /* InspectorServer.h */; };
@@ -474,7 +474,7 @@
474474
/* End PBXCopyFilesBuildPhase section */
475475

476476
/* Begin PBXFileReference section */
477-
2B7EA6AD2353476F00E5184E /* NativeScriptException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NativeScriptException.cpp; sourceTree = "<group>"; };
477+
2B7EA6AD2353476F00E5184E /* NativeScriptException.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = NativeScriptException.mm; sourceTree = "<group>"; };
478478
2B7EA6AE2353477000E5184E /* NativeScriptException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NativeScriptException.h; sourceTree = "<group>"; };
479479
C2229971235449B300C1DFD6 /* InspectorServer.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = InspectorServer.mm; sourceTree = "<group>"; };
480480
C2229972235449B400C1DFD6 /* InspectorServer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InspectorServer.h; sourceTree = "<group>"; };
@@ -1624,8 +1624,6 @@
16241624
C2DDEB3B229EAB8600345BFE /* runtime */ = {
16251625
isa = PBXGroup;
16261626
children = (
1627-
2B7EA6AD2353476F00E5184E /* NativeScriptException.cpp */,
1628-
2B7EA6AE2353477000E5184E /* NativeScriptException.h */,
16291627
C2DDEB66229EAC8100345BFE /* ArgConverter.h */,
16301628
C2DDEB6A229EAC8200345BFE /* ArgConverter.mm */,
16311629
C2DDEB6F229EAC8200345BFE /* ArrayAdapter.h */,
@@ -1665,6 +1663,8 @@
16651663
C2DDEB71229EAC8200345BFE /* MetadataBuilder.mm */,
16661664
C2DDEB89229EAC8300345BFE /* ModuleInternal.h */,
16671665
C2DDEB81229EAC8200345BFE /* ModuleInternal.mm */,
1666+
2B7EA6AE2353477000E5184E /* NativeScriptException.h */,
1667+
2B7EA6AD2353476F00E5184E /* NativeScriptException.mm */,
16681668
C266567A22AA630F00EE15CC /* NSDataAdapter.h */,
16691669
C266567922AA630F00EE15CC /* NSDataAdapter.mm */,
16701670
C2DDEB83229EAC8300345BFE /* ObjectManager.h */,
@@ -2364,7 +2364,7 @@
23642364
C247C35B22F828E3001D2CA2 /* wasm-translation.cc in Sources */,
23652365
C2DDEBB5229EAC8300345BFE /* Tasks.cpp in Sources */,
23662366
C2DDEBB1229EAC8300345BFE /* Caches.cpp in Sources */,
2367-
2B7EA6AF2353477000E5184E /* NativeScriptException.cpp in Sources */,
2367+
2B7EA6AF2353477000E5184E /* NativeScriptException.mm in Sources */,
23682368
C247C34122F828E3001D2CA2 /* v8-log-agent-impl.cpp in Sources */,
23692369
C247C39022F828E3001D2CA2 /* v8-runtime-agent-impl.cc in Sources */,
23702370
C247C39622F828E3001D2CA2 /* value-mirror.cc in Sources */,

0 commit comments

Comments
 (0)