Repository navigation
feat: web-compliant error handling (unhandled rejections, error events, interop.escapeException) #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: web-compliant error handling (unhandled rejections, error events, interop.escapeException) #409
Changes from 1 commit
7f19473
f7b5332
642c338
259a740
abc0e92
14d1dfa
9ab61e3
05389dd
30f5079
75708c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Replace the never-released crashOnUncaughtJsExceptions flag with the
cross-runtime uncaughtErrorPolicy contract:
{ "uncaughtErrorPolicy": "report" } // default: report, keep running
{ "uncaughtErrorPolicy": "throw" } // rethrow unprevented errors natively
"throw" keeps the same mechanics (deferred NativeScriptFatalJSException
from a clean scope-free frame, JS stack in reason/userInfo) under an
honest name: it is a throw, not a crash guarantee - the app terminates
only if no native handler catches it. Unknown values warn once and fall
back to "report".
discardUncaughtJsExceptions is deprecated but still honored in full for
now (routes to __onDiscardedError, skips the fatal log, suppresses the
throw policy); it logs a one-time deprecation warning pointing at
preventDefault() and uncaughtErrorPolicy.- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # Error handling | ||
|
|
||
| The runtime implements the WHATWG error model at the global level: uncaught JavaScript exceptions and unhandled promise rejections are dispatched as cancelable events on `globalThis`, native `NSException`s round-trip into JavaScript with the original object attached, and `interop.escapeException` forwards a JavaScript throw to a native caller as a real `@throw`. By default nothing crashes the app — reporting is opt-out per error (`preventDefault()`), and crashing is opt-in (`crashOnUncaughtJsExceptions`). | ||
| The runtime implements the WHATWG error model at the global level: uncaught JavaScript exceptions and unhandled promise rejections are dispatched as cancelable events on `globalThis`, native `NSException`s round-trip into JavaScript with the original object attached, and `interop.escapeException` forwards a JavaScript throw to a native caller as a real `@throw`. By default nothing crashes the app — reporting is opt-out per error (`preventDefault()`), and rethrowing unprevented errors natively is opt-in (`uncaughtErrorPolicy: "throw"`). | ||
|
|
||
| ## Quick reference | ||
|
|
||
|
|
@@ -40,7 +40,7 @@ globalThis.addEventListener("rejectionhandled", (e) => { | |
|
|
||
| Notes: | ||
|
|
||
| - `error` and `unhandledrejection` are `cancelable`; `preventDefault()` suppresses every downstream consequence (legacy hooks, fatal log, error modal, `crashOnUncaughtJsExceptions`). | ||
| - `error` and `unhandledrejection` are `cancelable`; `preventDefault()` suppresses every downstream consequence (legacy hooks, fatal log, error modal, the `uncaughtErrorPolicy: "throw"` rethrow). | ||
| - Events fire even if app code overwrites `globalThis.dispatchEvent` — native dispatch goes through closures captured at startup. | ||
| - A listener that throws does not stop the remaining listeners; the thrown value is routed to the fatal reporting tail directly (never recursively dispatched as another `error` event). | ||
| - The events also fire on worker globals. A worker's unhandled rejection dispatches `unhandledrejection` on the worker's own global first; only when unprevented does it continue to the worker-global `onerror` and then to the main isolate's `worker.onerror`, mirroring uncaught worker errors. | ||
|
|
@@ -149,27 +149,33 @@ The accessor is uniform across all cases; underneath: | |
| |---|---| | ||
| | Synthesized escape | `userInfo[TNSJavaScriptStackTraceKey]` (`@"JavaScriptStack"`), plus `userInfo[TNSJavaScriptEscapeStackTraceKey]` (`@"JavaScriptEscapeStack"`) when the `escapeException` call site differs from the error's origin; also appended to `reason` | | ||
| | Rethrown original `NSException` | attached as an associated object (identity and `userInfo` are untouched); the string combines the origin stack and the escape site | | ||
| | `crashOnUncaughtJsExceptions` fatal (`NativeScriptFatalJSException`) | `userInfo[TNSJavaScriptStackTraceKey]`, also in `reason` | | ||
| | `uncaughtErrorPolicy: "throw"` fatal (`NativeScriptFatalJSException`) | `userInfo[TNSJavaScriptStackTraceKey]`, also in `reason` | | ||
|
|
||
| `tns_javascriptStackTrace` and the two `userInfo` keys are the stable contract for crash-SDK integrations. | ||
|
|
||
| ## Configuration | ||
|
|
||
| Both flags live in the app's `package.json`, both default to off: | ||
| `uncaughtErrorPolicy` in the app's `package.json` is the uncaught-error contract, unified across NativeScript runtimes (9.1+): | ||
|
|
||
| | Flag | Effect | | ||
| |---|---| | ||
| | `discardUncaughtJsExceptions` | Unprevented errors call `__onDiscardedError` instead of `__onUncaughtError` and skip the fatal log. Also disables `crashOnUncaughtJsExceptions`. | | ||
| | `crashOnUncaughtJsExceptions` | After an unprevented error is reported, the app crashes with a real `NSException` (JS stack in `reason` and `userInfo`), thrown from a clean frame on the runtime loop so crash reporters capture it properly. | | ||
| ```jsonc | ||
| { "uncaughtErrorPolicy": "report" } // default: report and keep running | ||
| { "uncaughtErrorPolicy": "throw" } // unprevented errors are rethrown natively | ||
| ``` | ||
|
|
||
| - `"report"` — after the event dispatch and legacy hooks, log the fatal message and keep the app alive. | ||
| - `"throw"` — additionally rethrow the error as a real `NSException` (`NativeScriptFatalJSException`, JS stack in `reason` and `userInfo`), from a clean frame on the runtime loop. This is a **throw, not a crash guarantee**: the app terminates only if no native handler catches it — which also means crash reporters capture it properly when it does. | ||
| - Unknown values log a warning once and fall back to `"report"`. | ||
|
|
||
| Deprecated: `discardUncaughtJsExceptions` is still honored in full for now — unprevented errors route to `__onDiscardedError` instead of `__onUncaughtError`, the fatal log is skipped, and any `"throw"` policy is suppressed — but it logs a deprecation warning. Migrate to an `error`/`unhandledrejection` listener calling `preventDefault()` (per-error, more precise) and `uncaughtErrorPolicy`. | ||
|
|
||
| Terminal-path decision table for an uncaught error / unhandled rejection: | ||
|
|
||
| | Condition | legacy hook called | fatal log / modal | native crash | | ||
| | Condition | legacy hook called | fatal log / modal | native throw | | ||
| |---|---|---|---| | ||
| | default | `__onUncaughtError` | yes | no | | ||
| | `discardUncaughtJsExceptions` | `__onDiscardedError` | no | no | | ||
| | `"report"` (default) | `__onUncaughtError` | yes | no | | ||
| | `"throw"`, unprevented | `__onUncaughtError` | yes | yes | | ||
| | listener called `preventDefault()` | no | no | no | | ||
| | `crashOnUncaughtJsExceptions`, unprevented | `__onUncaughtError` | yes | yes | | ||
| | `discardUncaughtJsExceptions` (deprecated) | `__onDiscardedError` | no | no | | ||
|
|
||
| ## Crash reporter integration | ||
|
|
||
|
|
@@ -193,7 +199,7 @@ globalThis.addEventListener("error", (e) => { | |
| }); | ||
| ``` | ||
|
|
||
| Native side — for the `NSUncaughtExceptionHandler`/signal layer, read `exception.tns_javascriptStackTrace` (or the `userInfo` keys) to attach the JS stack to crashes that never pass through the JS event layer (escaped exceptions, `crashOnUncaughtJsExceptions`). | ||
| Native side — for the `NSUncaughtExceptionHandler`/signal layer, read `exception.tns_javascriptStackTrace` (or the `userInfo` keys) to attach the JS stack to crashes that never pass through the JS event layer (escaped exceptions, `uncaughtErrorPolicy: "throw"`). | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Correct the policy-throw event-path description.
🤖 Prompt for AI Agents |
||
|
|
||
| ## Legacy hooks (deprecated) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Include unhandled rejections in the crash-reporter example.
The documented reporting model includes
unhandledrejection, but this sample only listens forerror. Add a shared capture helper and anunhandledrejectionlistener, or explicitly scope the example to uncaught exceptions.🤖 Prompt for AI Agents