|
| 1 | +# Error boundaries with `@boundary` |
| 2 | + |
| 3 | +IMPORTANT: `@boundary` is in [developer preview](reference/releases#developer-preview). |
| 4 | + |
| 5 | +Angular templates support error boundaries to gracefully handle runtime errors that occur during rendering and change detection. |
| 6 | + |
| 7 | +Error boundaries prevent a single component's failure from crashing the entire application and provide a way to display fallback UI to the user. |
| 8 | + |
| 9 | +## Catching errors with `@boundary` and `@error` |
| 10 | + |
| 11 | +The `@boundary` block wraps a section of your template. If any component or directive inside this boundary throws an error during initialization or change detection, the framework catches the error and renders the `@error` block instead. |
| 12 | + |
| 13 | +```angular-html |
| 14 | +@boundary { |
| 15 | + <app-risky-component /> |
| 16 | +} @error { |
| 17 | + <p>Something went wrong!</p> |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +## Accessing the error object |
| 22 | + |
| 23 | +You can access the caught error by accessing the implicit `$error` variable: |
| 24 | + |
| 25 | +```angular-html |
| 26 | +@boundary { |
| 27 | + <app-risky-component /> |
| 28 | +} @error { |
| 29 | + <p>Error occurred: {{ $error.message }}</p> |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Resetting the boundary |
| 34 | + |
| 35 | +You can attempt to re-render the content of the `@boundary` by calling the implicit `$reset` function in the `@error` block. When called, it resets the boundary state and tries to render the original content again. |
| 36 | + |
| 37 | +```angular-html |
| 38 | +@boundary { |
| 39 | + <app-flaky-component /> |
| 40 | +} @error { |
| 41 | + <p>Loading failed.</p> |
| 42 | + <button (click)="$reset()">Try again</button> |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## Conditional error handling with `when` |
| 47 | + |
| 48 | +You can use `when` clauses to conditionally handle specific types of errors, allowing you to provide different fallback UIs. Angular evaluates this condition when it catches an error. |
| 49 | + |
| 50 | +```angular-html |
| 51 | +@boundary { |
| 52 | + <app-chart-dashboard /> |
| 53 | +} @error (let err; reset = $reset; when isRenderError(err)) { |
| 54 | + <p>Network issue. Check your connection.</p> |
| 55 | + <button (click)="reset()">Retry</button> |
| 56 | +} @error { |
| 57 | + <p>An unexpected error occurred: {{ $error.message }}</p> |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +Order your `@error` blocks from most specific to least specific, as Angular evaluates the `when` clauses in order and uses the first one that evaluates to true. A final `@error` block without a `when` clause acts as a catch-all fallback. |
| 62 | + |
| 63 | +## Global error handler integration |
| 64 | + |
| 65 | +When a boundary catches an error, Angular can still notify the global `ErrorHandler`. You can implement the optional `onViewError` hook in your custom `ErrorHandler` to log these caught errors to your error tracking service. |
| 66 | + |
| 67 | +```ts |
| 68 | +@Injectable() |
| 69 | +export class MyErrorHandler implements ErrorHandler { |
| 70 | + handleError(error: any): void { |
| 71 | + // Handle uncaught errors |
| 72 | + } |
| 73 | + |
| 74 | + onViewError(error: Error, details: ErrorDetails): void { |
| 75 | + // Handle errors caught by a @boundary |
| 76 | + console.warn('Caught by boundary:', details.boundary); |
| 77 | + myErrorTrackingService.log(error); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +IMPORTANT: If an `@error` block itself throws an error, the error propagates to the next outer `@boundary` or Angular treats it as an unhandled application error. |
| 83 | + |
| 84 | +## Dynamic views and programmatic error handling |
| 85 | + |
| 86 | +Error handling isn't limited to template syntax. If you are creating components or embedded views dynamically, you can use the `onError` option to handle errors. See the [Handling rendering errors](guide/components/programmatic-rendering#handling-rendering-errors) section in the programmatic rendering guide for more information. |
0 commit comments