Handling errors
When your code throws, AngularLynx reports the error to Lynx and keeps it as text you can show. To keep one broken part from taking down a screen, wrap it in @boundary and show a fallback instead. To keep a broken screen from taking down the app, give its route an error component.
What happens when an error isn't caught
An error is uncaught when nothing handles it, for example a throw in a template binding, a lifecycle hook or an event handler. What you see depends on where the app runs:
What provideRenderer() sets up
Every app's config calls provideRenderer(). Among other things, it registers LynxErrorHandler as Angular's ErrorHandler. For each uncaught error, LynxErrorHandler:
- saves the error's name, message and stack as text in
globalThis.__lynxLastError, so you can show it on screen; - reports the error to Lynx, which opens the error screen in Lynx Explorer.
AngularLynx also installs global onerror and onunhandledrejection handlers. They save and report errors that happen outside Angular, such as a promise rejection that nothing handles.
Add your own handling
Extend LynxErrorHandler and call super to keep the default behavior:
Provide it after provideRenderer(), so it replaces the default:
- Don't write signals directly in the handler. Angular can call it in the middle of rendering, where a signal write throws
NG0600. Defer the write withqueueMicrotask(), as Show errors on screen does. - To upload reports, call
fetch(). It works on the main thread, where most errors happen. See HTTP requests.
Contain failures with @boundary
@boundary catches errors while Angular creates or updates the views inside it. When one happens, Angular removes that part of the screen and renders the @error block in its place. The rest of the screen keeps working.
@boundary needs Angular 22.2 or newer, and it's a developer preview API. Its syntax and behavior may change in later Angular releases.
- It works on Lynx with no setup. AngularLynx swaps the native elements for the fallback in place.
erris the caught error, andretry()($reset) renders the primary block again. Fix the cause first, asrestorePrice()does here, or the block throws again.- Use
(bindtap)for the retry button. Angular's own examples use(click), but on Lynx a tap arrives asbindtap. See Handling taps and events. - Everything else, such as
whenconditions, nested boundaries andresource()errors, works as Angular documents it in Error boundaries with@boundary.
What a boundary catches
Show an error screen for a route
Angular's route error boundaries replace a screen that throws with an error component, and leave the rest of the app working. Turn them on with withErrorBoundaries():
A route can have its own, { path: 'orders/:id', component: Order, errorComponent: OrderError }.
Route error boundaries need Angular 22.3 or newer, and they're a developer preview API.
- They work in every outlet:
<router-outlet>,<lynx-stack-outlet>and<lynx-tab-outlet>. The error component takes the screen's place: in a stack, it slides in as the screen would have, and Back or a swipe takes it away. - They catch what a screen throws as it's made (its constructor,
inject()) and as it's checked (its template, lifecycle hooks, aresource()its template reads), as@boundarydoes. The exception is a tab built ahead that throws in its first check: it's left out, and its error goes to theErrorHandler, not to the boundary's error component oronError(see When a tab fails). erroris the caught error. WithwithComponentInputBinding(), the route's params and data go to the error component's other inputs too, but only on the screen the router is on. A stack's covered screen or a hidden tab that fails gets onlyerror: the router binds the inputs of the one screen it shows. Make inputs that come from the route optional (input<string>()).- Coming back to the route builds the screen again, as on the web. That's true of a stack's screen you go back to, and of a tab you switch back to.
- A
RedirectCommandthe screen throws navigates where it says. onErrorhears of every error a route boundary catches. They don't reach the app'sErrorHandler, not even itsonViewError().- An error there's no error component for goes on to the
ErrorHandler, as without boundaries. A screen that throws as it's made then fails the navigation. ARedirectCommandis the exception: it still navigates, and doesn't reach theErrorHandler.
See Error boundaries in routing on angular.dev for the rest.
Report errors a boundary caught
Angular passes caught errors to the app's ErrorHandler too, through its onViewError() method. LynxErrorHandler implements it:
- It saves the error in
globalThis.__lynxLastError, like any other error. - It reports the error to Lynx as a warning, not an error. The user is already looking at a working fallback, so Lynx Explorer doesn't open its error screen, and monitoring doesn't count a crash.
To act on caught errors yourself, override onViewError():
onViewError()
Angular calls onViewError() synchronously, in the middle of rendering. Writing a signal there throws NG0600 and can break the boundary. Defer the write with queueMicrotask(), as above.
Related
- Debugging: logs, Lynx DevTool and on-screen errors.
- Testing patterns: test a
@boundaryfallback. LynxErrorHandlerAPI- Error boundaries with
@boundaryon angular.dev - Error boundaries in routing on angular.dev
