docs: add error boundaries entries · angular/angular@e205075 · GitHub
Skip to content

Commit e205075

Browse files
JeanMecheatscott
authored andcommitted
docs: add error boundaries entries
1 parent 29f9dd5 commit e205075

5 files changed

Lines changed: 158 additions & 0 deletions

File tree

adev/src/app/routing/navigation-entries/index.ts

Lines changed: 6 additions & 0 deletions

adev/src/content/guide/components/programmatic-rendering.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,18 @@ export class PopupService {
396396
}
397397
}
398398
```
399+
400+
## Handling rendering errors
401+
402+
When dynamically creating components using `ViewContainerRef.createComponent` or the standalone `createComponent` function, you can provide an `onError` callback in the options object to handle errors that occur during the rendering or change detection phases. This is the programmatic equivalent of using an `@error` block in templates.
403+
404+
```ts
405+
viewContainerRef.createComponent(DynamicComponent, {
406+
onError: (err: Error, details: ErrorDetails) => {
407+
console.error('Component rendering failed:', err);
408+
// Render an alternative UI or log metrics
409+
},
410+
});
411+
```
412+
413+
NOTE: The `onError` callback only catches errors that occur during the rendering or change detection phases. It does not catch errors that occur during component instantiation (for example, in the constructor). Angular throws construction errors synchronously when you call the API.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.

pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions

0 commit comments

Comments
 (0)