Skip to content

Commit 7cc6af9

Browse files
SkyZeroZxAndrewKushnir
authored andcommitted
docs: Add example Unhandled errors in Angular documentation (#64356)
PR Close #64356
1 parent 1502b1b commit 7cc6af9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

adev/src/content/best-practices/error-handling.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,33 @@ Angular does _not_ catch errors inside of APIs that are called directly by your
1616

1717
Angular catches _asynchronous_ errors from user promises or observables only when:
1818

19-
* There is an explicit contract for Angular to wait for and use the result of the asynchronous operation, and
20-
* When errors are not presented in the return value or state.
19+
- There is an explicit contract for Angular to wait for and use the result of the asynchronous operation, and
20+
- When errors are not presented in the return value or state.
2121

2222
For example, `AsyncPipe` and `PendingTasks.run` forward errors to the `ErrorHandler`, whereas `resource` presents the error in the `status` and `error` properties.
2323

2424
Errors that Angular reports to the `ErrorHandler` are _unexpected_ errors. These errors may be unrecoverable or an indication that the state of the application is corrupted. Applications should provide error handling using `try` blocks or appropriate error handling operators (like `catchError` in RxJS) where the error occurs whenever possible rather than relying on the `ErrorHandler`, which is most frequently and appropriately used only as a mechanism to report potentially fatal errors to the error tracking and logging infrastructure.
2525

26+
```ts
27+
export class GlobalErrorHandler implements ErrorHandler {
28+
private readonly analyticsService = inject(AnalyticsService);
29+
private readonly router = inject(Router);
30+
31+
handleError(error: any) {
32+
const url = this.router.url;
33+
const errorMessage = error?.message ?? 'unknown';
34+
35+
this.analyticsService.trackEvent({
36+
eventName: 'exception',
37+
description: `Screen: ${url} | ${errorMessage}`,
38+
});
39+
40+
console.error(GlobalErrorHandler.name, { error });
41+
}
42+
}
43+
44+
```
45+
2646
### `TestBed` rethrows errors by default
2747

2848
In many cases, `ErrorHandler` may only log errors and otherwise allow the application to continue running. In tests, however, you almost always want to surface these errors. Angular's `TestBed` rethrows unexpected errors to ensure that errors caught by the framework cannot be unintentionally missed or ignored. In rare circumstances, a test may specifically attempt to ensure errors do not cause the application to be unresponsive or crash. In these situations, you can [configure `TestBed` to _not_ rethrow application errors](api/core/testing/TestModuleMetadata#rethrowApplicationErrors) with `TestBed.configureTestingModule({rethrowApplicationErrors: false})`.

packages/core/src/error_handler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ import {DestroyRef} from './linker/destroy_ref';
4545
* ```
4646
*
4747
* @publicApi
48+
*
49+
* @see [Unhandled errors in Angular](best-practices/error-handling)
50+
*
4851
*/
4952
export class ErrorHandler {
5053
/**

0 commit comments

Comments
 (0)