|
| 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 | +``` |
0 commit comments