Skip to content

Commit db5e0e2

Browse files
Lms24lizokm
andauthored
feat(angular): Add documentation for Sentry's ErrorHandler (#11361)
The new page about the Angular SDK's error handler explains - what the error handler does / why it's necessary - a couple of options (omitted the old `reportDialog` options in favour of User Feedback) - how to handle multiple error handlers --------- Co-authored-by: Liza Mock <[email protected]>
1 parent a623415 commit db5e0e2

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: Angular Error Handler
3+
description: "Learn about Sentry's Angular SDK Error Handler and how to configure it."
4+
---
5+
6+
Angular often wraps errors in its own error classes, making it hard for Sentry to extract the important data correctly without a custom error handler, which is where Sentry's Angular SDK `ErrorHandler` can help. On this page, you'll learn how to customize the SDK's `ErrorHandler` to work for your use case.
7+
8+
## Using the Sentry `ErrorHandler`
9+
10+
To get started, register the Sentry `ErrorHandler` in your Angular application's providers. We recommend to do this in your main `app.config.ts` or `app.module.ts` file to ensure that the `ErrorHandler` is available everywhere in your application.
11+
12+
```typescript {tabTitle:App Config} {filename:app.config.ts} {3,8-11}
13+
import { ApplicationConfig, ErrorHandler } from "@angular/core";
14+
15+
import * as Sentry from "@sentry/angular";
16+
17+
export const appConfig: ApplicationConfig = {
18+
providers: [
19+
// ...
20+
{
21+
provide: ErrorHandler,
22+
useValue: Sentry.createErrorHandler(),
23+
},
24+
],
25+
};
26+
export class AppModule {}
27+
```
28+
29+
```typescript {tabTitle:NGModule Config} {filename:app.module.ts} {3,8-11}
30+
import { ErrorHandler, NgModule } from "@angular/core";
31+
32+
import * as Sentry from "@sentry/angular";
33+
34+
@NgModule({
35+
// ...
36+
providers: [
37+
{
38+
provide: ErrorHandler,
39+
useValue: Sentry.createErrorHandler(),
40+
},
41+
],
42+
})
43+
export class AppModule {}
44+
```
45+
46+
### Options
47+
48+
As an alternative, you can configure the behavior of the `ErrorHandler` by passing an object to the `createErrorHandler` function. The following options are available:
49+
50+
#### `logErrors`
51+
52+
_Type: `boolean` Default: `true`_
53+
54+
Lets you log errors to the console. This is `true` by default to ensure consistency with Angular's default behavior.
55+
56+
#### `extractor`
57+
58+
_Type: `function` Default: `undefined`_
59+
60+
Lets you extract the raw, incoming error object. The SDK uses its own extraction logic by default, but you can override it by providing your own function.
61+
62+
## Multiple Error Handlers
63+
64+
Angular doesn't let you provide multiple `ErrorHandler` instances in the same Angular application in `app.config.ts` or `app.module.ts`.
65+
Therefore, if you're using your own error handler or you want to add additional logic to Sentry's `ErrorHandler`, you have to extend Sentry's `ErrorHandler` like in the example below:
66+
67+
```typescript
68+
import * as Sentry from "@sentry/angular";
69+
70+
export class CustomErrorHandler extends Sentry.ErrorHandler {
71+
constructor() {
72+
// Pass options to Sentry's ErrorHandler here. For Example:
73+
super({ logErrors: false });
74+
}
75+
76+
handleError(error: any): void {
77+
// Your custom error handling logic
78+
// Call Sentry's error handler to capture errors:
79+
super.handleError(error);
80+
}
81+
}
82+
```

platform-includes/getting-started-config/javascript.angular.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ import * as Sentry from "@sentry/angular";
139139
export class AppModule {}
140140
```
141141

142-
The `Sentry.createErrorHandler` function initializes a Sentry-specific `ErrorHandler` that automatically sends errors caught by Angular to Sentry. You can also customize the behavior by setting a couple of handler [options](https://github.com/getsentry/sentry-javascript/blob/master/packages/angular/src/errorhandler.ts).
142+
The `Sentry.createErrorHandler` function initializes a Sentry-specific `ErrorHandler` that automatically sends errors caught by Angular to Sentry. Learn how to further configure Sentry's `ErrorHandler` behavior [here](./features/error-handler).
143143

144144
<Alert>
145145

0 commit comments

Comments
 (0)