Skip to content

Commit 1eaf03d

Browse files
authored
fix: re-throw exceptions in both debug and release modes for better error handling (#368)
1 parent e0d30a8 commit 1eaf03d

1 file changed

Lines changed: 35 additions & 44 deletions

File tree

NativeScript/runtime/NativeScriptException.mm

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#if __has_include(<UniformTypeIdentifiers/UniformTypeIdentifiers.h>)
55
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
66
#endif
7+
#include <TargetConditionals.h>
78
#import <objc/message.h>
89
#import <objc/runtime.h>
9-
#include <TargetConditionals.h>
10-
#include <sstream>
11-
#include <mutex>
12-
#include <limits>
1310
#include <algorithm>
11+
#include <limits>
12+
#include <mutex>
13+
#include <sstream>
1414
#include "Caches.h"
1515
#include "Helpers.h"
1616
#include "Runtime.h"
@@ -39,7 +39,7 @@
3939
static PendingErrorDisplay gPendingErrorDisplay;
4040
static uint64_t gNextErrorTicket = 1;
4141

42-
}
42+
} // namespace
4343

4444
namespace tns {
4545

@@ -50,8 +50,7 @@
5050
static void UpdateDisplayedStackText(const std::string& stackText);
5151
static void RenderErrorModalUI(v8::Isolate* isolate, const std::string& title,
5252
const std::string& message, const std::string& stackText);
53-
static void ShowErrorModalSynchronously(const std::string& title,
54-
const std::string& message,
53+
static void ShowErrorModalSynchronously(const std::string& title, const std::string& message,
5554
const std::string& stackTrace);
5655
static void ScheduleFallbackPresentation(uint64_t ticket);
5756
static void PresentFallbackIfNeeded(uint64_t ticket);
@@ -217,11 +216,7 @@ static void ConsiderStackCandidate(PendingErrorDisplay& state, v8::Isolate* isol
217216
}
218217
} @catch (NSException* exception) {
219218
Log(@"OnUncaughtError: Caught exception during error handling: %@", exception);
220-
if (RuntimeConfig.IsDebug) {
221-
Log(@"Debug mode - suppressing crash and continuing");
222-
} else {
223-
@throw exception; // Re-throw in release mode
224-
}
219+
@throw exception;
225220
}
226221
}
227222

@@ -341,21 +336,10 @@ static void ConsiderStackCandidate(PendingErrorDisplay& state, v8::Isolate* isol
341336
}
342337
}
343338

344-
// For non-critical exceptions:
345-
if (RuntimeConfig.IsDebug) {
346-
// Be gentle, state case in logs and allow developer to continue
347-
Log(@"Debug mode - suppressing throw to continue: %s", this->message_.c_str());
348-
} else {
349-
// just re-throw normally
350-
isolate->ThrowException(errObj);
351-
}
339+
isolate->ThrowException(errObj);
352340
} @catch (NSException* exception) {
353341
Log(@"ReThrowToV8: Caught exception during error handling: %@", exception);
354-
if (RuntimeConfig.IsDebug) {
355-
Log(@"Debug mode - suppressing crash and continuing");
356-
} else {
357-
@throw exception; // Re-throw in release mode
358-
}
342+
@throw exception;
359343
}
360344
}
361345

@@ -486,7 +470,8 @@ static void ConsiderStackCandidate(PendingErrorDisplay& state, v8::Isolate* isol
486470
{
487471
std::lock_guard<std::mutex> lock(gErrorDisplayMutex);
488472

489-
// If the console already presented this error (console-first scenario), just enrich the context.
473+
// If the console already presented this error (console-first scenario), just enrich the
474+
// context.
490475
if (gPendingErrorDisplay.ticket != 0 && !gPendingErrorDisplay.contextCaptured &&
491476
gPendingErrorDisplay.modalPresented) {
492477
gPendingErrorDisplay.contextCaptured = true;
@@ -519,7 +504,8 @@ static void ConsiderStackCandidate(PendingErrorDisplay& state, v8::Isolate* isol
519504
}
520505
}
521506

522-
void NativeScriptException::SubmitConsoleErrorPayload(Isolate* isolate, const std::string& payload) {
507+
void NativeScriptException::SubmitConsoleErrorPayload(Isolate* isolate,
508+
const std::string& payload) {
523509
if (!RuntimeConfig.IsDebug) {
524510
return;
525511
}
@@ -587,19 +573,19 @@ static void ConsiderStackCandidate(PendingErrorDisplay& state, v8::Isolate* isol
587573
}
588574

589575
if (presentNow) {
590-
std::string displayStack = stateSnapshot.canonicalStack.empty()
591-
? (stateSnapshot.consolePayload.empty()
592-
? ResolveDisplayStack(stateSnapshot)
593-
: stateSnapshot.consolePayload)
594-
: stateSnapshot.canonicalStack;
576+
std::string displayStack =
577+
stateSnapshot.canonicalStack.empty()
578+
? (stateSnapshot.consolePayload.empty() ? ResolveDisplayStack(stateSnapshot)
579+
: stateSnapshot.consolePayload)
580+
: stateSnapshot.canonicalStack;
595581
RenderErrorModalUI(stateSnapshot.isolate, stateSnapshot.title, stateSnapshot.message,
596582
displayStack);
597583
} else if (updateExisting) {
598584
std::string displayStack = gPendingErrorDisplay.canonicalStack.empty()
599-
? (gPendingErrorDisplay.consolePayload.empty()
600-
? ResolveDisplayStack(gPendingErrorDisplay)
601-
: gPendingErrorDisplay.consolePayload)
602-
: gPendingErrorDisplay.canonicalStack;
585+
? (gPendingErrorDisplay.consolePayload.empty()
586+
? ResolveDisplayStack(gPendingErrorDisplay)
587+
: gPendingErrorDisplay.consolePayload)
588+
: gPendingErrorDisplay.canonicalStack;
603589
UpdateDisplayedStackText(displayStack);
604590
}
605591
}
@@ -743,7 +729,10 @@ static void RenderErrorModalUI(v8::Isolate* isolate, const std::string& title,
743729
for (UIScene* scene in app.connectedScenes) {
744730
if ([scene isKindOfClass:[UIWindowScene class]]) {
745731
UIWindowScene* ws = (UIWindowScene*)scene;
746-
if (ws.windows.count > 0) { hasAnyWindows = YES; break; }
732+
if (ws.windows.count > 0) {
733+
hasAnyWindows = YES;
734+
break;
735+
}
747736
}
748737
}
749738
}
@@ -782,8 +771,7 @@ static void RenderErrorModalUI(v8::Isolate* isolate, const std::string& title,
782771
}
783772
}
784773

785-
static void ShowErrorModalSynchronously(const std::string& title,
786-
const std::string& message,
774+
static void ShowErrorModalSynchronously(const std::string& title, const std::string& message,
787775
const std::string& stackTrace) {
788776
// Use static variables to keep strong references and prevent deallocation
789777
static UIWindow* __attribute__((unused)) foundationWindowRef =
@@ -800,7 +788,10 @@ static void ShowErrorModalSynchronously(const std::string& title,
800788
if (@available(iOS 13.0, *)) {
801789
for (UIScene* scene in sharedApp.connectedScenes) {
802790
if ([scene isKindOfClass:[UIWindowScene class]]) {
803-
if (((UIWindowScene*)scene).windows.count > 0) { appHasWindows = YES; break; }
791+
if (((UIWindowScene*)scene).windows.count > 0) {
792+
appHasWindows = YES;
793+
break;
794+
}
804795
}
805796
}
806797
}
@@ -863,14 +854,16 @@ static void ShowErrorModalSynchronously(const std::string& title,
863854
// Give iOS a moment to process the new window hierarchy (we're already on main queue)
864855
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, false);
865856

866-
867857
// Detailed window hierarchy inspection
868858
BOOL appHasWindowsAfterBootstrap = NO;
869859
#if TARGET_OS_VISION
870860
if (@available(iOS 13.0, *)) {
871861
for (UIScene* scene in sharedApp.connectedScenes) {
872862
if ([scene isKindOfClass:[UIWindowScene class]]) {
873-
if (((UIWindowScene*)scene).windows.count > 0) { appHasWindowsAfterBootstrap = YES; break; }
863+
if (((UIWindowScene*)scene).windows.count > 0) {
864+
appHasWindowsAfterBootstrap = YES;
865+
break;
866+
}
874867
}
875868
}
876869
}
@@ -1069,7 +1062,6 @@ static void ShowErrorModalSynchronously(const std::string& title,
10691062
[stackTraceContainer addSubview:stackTraceTextView];
10701063
gErrorStackTextView = stackTraceTextView;
10711064

1072-
10731065
// Hot-reload indicator
10741066
UILabel* hotReloadLabel = [[UILabel alloc] init];
10751067
hotReloadLabel.text = @"Fix the error and save your changes to continue.";
@@ -1174,7 +1166,6 @@ static void ShowErrorModalSynchronously(const std::string& title,
11741166
// Log(@"Error window in app windows: %@", windowInHierarchy ? @"YES" : @"NO");
11751167

11761168
if (!windowInHierarchy) {
1177-
11781169
// Aggressive fix 1: Try to force the window to be key and make it the only visible window
11791170
Log(@"Total app windows before fix: %lu", (unsigned long)windows.count);
11801171

0 commit comments

Comments
 (0)