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.
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. - Uploading reports to a server isn't supported yet. Most errors happen on the main thread, which has no network access. See Network 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
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
