[Angular] Angular integrate Sentry

Angular Sentry

Sentry’s SDKs enable automatic reporting of errors and exceptions.

On this page, we get you up and running with Sentry’s SDK, so that it will automatically report errors and exceptions in your Angular application.

Prerequisites

You should have an sentry.io account or a Self-Hosted Sentry sever.

Install

Sentry captures data by using an SDK within your application’s runtime.

1
$ npm install --save @sentry/angular @sentry/tracing

Configure

Configuration should happen as early as possible in your Angular application’s lifecycle.

Once this is done, Sentry’s Angular SDK captures all unhandled exceptions and transactions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { enableProdMode } from "@angular/core";
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import * as Sentry from "@sentry/angular";
import { Integrations } from "@sentry/tracing";
import { AppModule } from "./app/app.module";

Sentry.init({
dsn: "https://[email protected]/0" ,
integrations: [
// Registers and configures the Tracing integration,
// which automatically instruments your application to monitor its
// performance, including custom Angular routing instrumentation
new Integrations.BrowserTracing({
tracingOrigins: ["localhost", "https://yourserver.io/api"],
routingInstrumentation: Sentry.routingInstrumentation,
}),
],

// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});


platformBrowserDynamic()
.bootstrapModule(AppModule)
.then(success => console.log(`Bootstrap success`))
.catch(err => console.error(err));

You can also configure @sentry/angular to catch any Angular-specific exceptions reported through the @angular/core/ErrorHandler provider.

@sentry/angular exports a Trace Service, Directive, and Decorators that leverages the @sentry/tracing, Sentry’s Tracing integration, to add Angular-related spans to transactions. The service itself tracks route changes and durations, where directive and decorators are tracking component initializations.

Automatically Send Errors with ErrorHandler

@sentry/angular exports a function to instantiate an ErrorHandler provider that will automatically send JavaScript errors captured by the Angular’s error handler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { NgModule, ErrorHandler } from "@angular/core";
import * as Sentry from "@sentry/angular";

@NgModule({
// ...
providers: [
{
provide: ErrorHandler,
useValue: Sentry.createErrorHandler({
showDialog: true,
}),
},
],
// ...
})

export class AppModule {}

You can configure the behavior of createErrorHandler. For more details see the ErrorHandlerOptions interface in our repository.

Register TraceService

In Angular’s DI system, register TraceService as a provider with a Router as its dependency:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { NgModule } from "@angular/core";
import { Router } from "@angular/router";
import * as Sentry from "@sentry/angular";

@NgModule({
// ...
providers: [
{
provide: Sentry.TraceService,
deps: [Router],
},
],
// ...
})

export class AppModule {}

Then, either require the TraceService from inside AppModule or use APP_INITIALIZER to force instantiate Tracing.

1
2
3
4
5
6
@NgModule({
// ...
})
export class AppModule {
constructor(trace: Sentry.TraceService) {}
}

or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { APP_INITIALIZER } from "@angular/core";
@NgModule({
// ...
providers: [
{
provide: APP_INITIALIZER,
useFactory: () => () => {},
deps: [Sentry.TraceService],
multi: true,
},
],
// ...
})

export class AppModule {}

Verify

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:

1
myUndefinedFunction();

Errors triggered from within Browser DevTools are sandboxed, so will not trigger an error handler. Place the snippet directly in your code instead.

Learn more about manually capturing an error or message in our Usage documentation.

To view and resolve the recorded error, log into sentry.io or your and open your project. Clicking on the error’s title will open a page where you can see detailed information and mark it as resolved.

References

[1] Angular | Sentry Documentation - https://docs.sentry.io/platforms/javascript/guides/angular/

[2] Sentry Documentation | Sentry Documentation - https://docs.sentry.io/

[4] Sentry | Error Tracking Software — JavaScript, Python, PHP, Ruby, more - https://sentry.io/welcome/

[5] Angular - https://angular.io/