Skip to content

Commit c7a176c

Browse files
committed
feat: add better error logging for NativeScript exceptions
1 parent 6c20ee0 commit c7a176c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

test-app/runtime/src/main/cpp/Runtime.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ void SIG_handler(int sigNumber) {
7272
throw NativeScriptException(msg.str());
7373
}
7474

75+
void LogAndAbortUncaught() {
76+
try {
77+
throw; // rethrow the current unknown
78+
} catch (const tns::NativeScriptException& e) {
79+
// We only have message/stack; no safe ReThrowToJava here.
80+
__android_log_print(ANDROID_LOG_FATAL, "TNS.Native",
81+
"Uncaught NativeScriptException: %s",
82+
e.ToString().c_str());
83+
} catch (const std::exception& e) {
84+
__android_log_print(ANDROID_LOG_FATAL, "TNS.Native",
85+
"Uncaught std::exception: %s", e.what());
86+
} catch (...) {
87+
__android_log_print(ANDROID_LOG_FATAL, "TNS.Native",
88+
"Uncaught unknown native exception");
89+
}
90+
91+
// Preserve default abort behavior so crashes are visible to tooling
92+
std::_Exit(EXIT_FAILURE);
93+
}
94+
7595
void Runtime::Init(JavaVM* vm, void* reserved) {
7696
__android_log_print(ANDROID_LOG_INFO, "TNS.Runtime",
7797
"NativeScript Runtime Version %s, commit %s",
@@ -92,6 +112,8 @@ void Runtime::Init(JavaVM* vm, void* reserved) {
92112
sigaction(SIGABRT, &action, NULL);
93113
sigaction(SIGSEGV, &action, NULL);
94114
}
115+
// Set terminate handler for uncaught exceptions
116+
std::set_terminate(LogAndAbortUncaught);
95117
}
96118

97119
int Runtime::GetAndroidVersion() {

0 commit comments

Comments
 (0)