For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/error-handling.md.

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:

Your app runs inWhat you see
Lynx ExplorerLynx's red error screen, LogBox, with the message and stack trace
The web previewThe error in your browser's DevTools console
The apps --platform builds, with ng build or ng serveNo error screen. These apps don't include LogBox, so show errors on screen while you test them

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:

  1. saves the error's name, message and stack as text in globalThis.__lynxLastError, so you can show it on screen;
  2. 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:

src/app/app-error-handler.ts
import { inject, Service } from '@angular/core';
import { LynxErrorHandler, LynxLogger } from '@blotch/angular-lynx';

@Service({ autoProvided: false })
export class AppErrorHandler extends LynxErrorHandler {
  readonly #logger = inject(LynxLogger);

  override handleError(error: unknown): void {
    // Keep the default: save the error and report it to Lynx.
    super.handleError(error);
    // Also write it, with its stack, to .dev-logs while `ng serve` runs.
    this.#logger.error(error);
  }
}

Provide it after provideRenderer(), so it replaces the default:

src/app/app.config.ts
import { type ApplicationConfig, ErrorHandler } from '@angular/core';
import { provideRenderer } from '@blotch/angular-lynx';
import { AppErrorHandler } from './app-error-handler';

export const appConfig: ApplicationConfig = {
  providers: [
    provideRenderer(),
    { provide: ErrorHandler, useClass: AppErrorHandler },
  ],
};
  • 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 with queueMicrotask(), 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.

Developer preview

@boundary needs Angular 22.2 or newer, and it's a developer preview API. Its syntax and behavior may change in later Angular releases.

<view>
  <text>Portfolio</text>

  @boundary {
    <app-price-tile symbol="LYNX" [price]="price()" />
  } @error (let err, retry = $reset) {
    <text>{{ err.message }}</text>
    <view (bindtap)="restorePrice(); retry()">
      <text>Retry</text>
    </view>
  }

  <text>Everything outside the boundary keeps rendering.</text>
</view>
  • It works on Lynx with no setup. AngularLynx swaps the native elements for the fallback in place.
  • err is the caught error, and retry() ($reset) renders the primary block again. Fix the cause first, as restorePrice() 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 as bindtap. See Handling taps and events.
  • Everything else, such as when conditions, nested boundaries and resource() errors, works as Angular documents it in Error boundaries with @boundary.

What a boundary catches

CaughtNot caught
Component and directive constructorsEvent handlers such as (bindtap). These go to the app's ErrorHandler
Template bindings and interpolationsErrors thrown inside the @error block itself. These go to the next boundary up
Lifecycle hooksAsync work that never feeds back into rendering
@for and other control-flow expressionsProjected (<ng-content>) content, which belongs to the parent's view
Effects declared inside the boundary
Lazily and dynamically created components

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():

src/app/app.config.ts
import { inject } from '@angular/core';
import {
  withComponentInputBinding,
  withErrorBoundaries,
} from '@angular/router';
import { LynxLogger, provideRouter } from '@blotch/angular-lynx';
import { routes } from './app.routes';
import { ScreenError } from './screen-error';

// In appConfig's providers:
provideRouter(
  routes,
  withComponentInputBinding(),
  withErrorBoundaries({
    defaultErrorComponent: ScreenError,
    onError: (error) => inject(LynxLogger).error(error),
  }),
);
src/app/screen-error.ts
import { Component, input } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-screen-error',
  imports: [LYNX_ELEMENTS],
  template: `
    <view class="flex flex-1 flex-col items-center justify-center gap-2 p-6">
      <text class="text-lg font-semibold">Something went wrong</text>
      <text class="text-muted-foreground">{{ error()?.message }}</text>
    </view>
  `,
})
export class ScreenError {
  readonly error = input<Error>(); 
}

A route can have its own, { path: 'orders/:id', component: Order, errorComponent: OrderError }.

Developer preview

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, a resource() its template reads), as @boundary does. The exception is a tab built ahead that throws in its first check: it's left out, and its error goes to the ErrorHandler, not to the boundary's error component or onError (see When a tab fails).
  • error is the caught error. With withComponentInputBinding(), 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 only error: 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 RedirectCommand the screen throws navigates where it says.
  • onError hears of every error a route boundary catches. They don't reach the app's ErrorHandler, not even its onViewError().
  • 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. A RedirectCommand is the exception: it still navigates, and doesn't reach the ErrorHandler.

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():

src/app/app-error-handler.ts
import { type ErrorDetails, inject, Service, signal } from '@angular/core';
import { LynxErrorHandler } from '@blotch/angular-lynx';

/** Messages of errors that boundaries caught, newest first. */
@Service()
export class CaughtErrors {
  readonly messages = signal<readonly string[]>([]);
}

@Service({ autoProvided: false })
export class AppErrorHandler extends LynxErrorHandler {
  readonly #caught = inject(CaughtErrors);

  override onViewError(error: Error, details: ErrorDetails): void {
    super.onViewError(error, details);
    // details.declarationType is the component that threw.
    queueMicrotask(() =>
      this.#caught.messages.update((list) => [error.message, ...list]),
    ); 
  }
}
Don't write signals in

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.