Angular angular logo for bugsplat
7 minute read

Introduction

BugSplat supports the collection of errors in Angular 4 applications. The bugsplat-ng4 npm package implements Angular’s ErrorHandler interface in order to post errors to BugSplat where they can be tracked and managed. Adding BugSplat to your Angular application is extremely easy. Before getting started please complete the following tasks:

Simple Configuration

To collect errors and crashes in your Angular 4 application, run the following command in terminal or cmd at the root of your project to install bugsplat-ng4:

npm install bugsplat-ng4 --save

Import BugSplat, BugSplatErrorHandler, BugSplatConfig and BugSplatConfigToken into your app module from bugsplat-ng4:

app.module.ts

import { BugSplatConfig, BugSplatConfigToken, BugSplatErrorHandler } from 'bugsplat-ng4';

Create a configuration for BugSplat in your app module and add a provider for BugSplatConfigToken with the useValue property set to the value of your configuration:

app.module.ts

...
const BUGSPLAT_CONFIG: BugSplatConfig = {
	appName: "my-angular-4-crasher",
	appVersion: "1.0.0.0",
	database: "fred"
            };

@NgModule({
   providers: [
     { provide: BugSplatConfigToken, useValue: BUGSPLAT_CONFIG },
   ],
 ...
})

Add a provider for ErrorHandler with the useClass property set to BugSplatErrorHandler:

app.module.ts

...
@NgModule({
    providers: [
       { provide: BugSplatConfigToken, useValue: BUGSPLAT_CONFIG },
        { provide: ErrorHandler, useClass: BugSplatErrorHandler }
    ],
  ...
})

Import the HttpClient module from @angular/common/http and add it to your module’s imports:

app.module.ts

import { HttpClientModule } from '@angular/common/http';
...
@NgModule({
  imports: [
   ...
   HttpClientModule
  ],
  providers: [
   { provide: BugSplatConfigToken, useValue: BUGSPLAT_CONFIG },
   { provide: ErrorHandler, useClass: BugSplatErrorHandler }
  ],
    ...
})

Throw a new error in your application to test the bugsplat-ng4 integration:

app.component.ts

throw new Error("foobar!");

Navigate to the All Crashes page in BugSplat and you should see a new crash report for the application you just configured. Click the link in the Id column to see details about your crash on the Individual Crash page:

AllCrashNg4

IndividualCrashNg4

Extended Configuration

You can post additional information to BugSplat by creating a wrapper around the BugSplat object. To do so, create a new class that implements Angular’s ErrorHandler interface. In the handlerError method make a call to BugSplat.post passing it the error object:

my-angular-error-handler.ts

import { ErrorHandler, Injectable, Inject, InjectionToken } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { BugSplat, BugSplatConfigToken, BugSplatConfig } from 'bugsplat-ng4';

@Injectable()
export class MyAngularErrorHandler implements ErrorHandler {
    public bugsplat: BugSplat;
    constructor(@Inject(BugSplatConfigToken) public config: BugSplatConfig, private http: HttpClient) {
                    this.bugsplat = new BugSplat(this.config, this.http);
    }
    handleError(error) {
        // Add additional functionality here
        this.bugsplat.post(error);
    }
}

BugSplat provides the following properties and methods that allow you to customize its functionality:

bugsplat-error-handler.ts

BugSplat.appkey: string; // A unique identifier for your application
BugSplat.user: string; // The name or id of your user
BugSplat.email: string; // The email of your user 
BugSplat.description: string; // Additional info about your crash that gets reset after every post
BugSplat.addAdditionalFile(file); // Add a file to a list of files to be uploaded at post time (total upload limit 2MB)
BugSplat.getObservable(); // Returns an Observable<BugSplatPostEvent>. Subscribing to this method will allow you to hook into the results of BugSplatPost events in your components. Make sure to unsubscribe in ngOnDestroy.
BugSplat.post(error); // Post an arbitrary Error object to BugSplat

In your app module, update the useClass property in your ErrorHandler provider to the name of the class you just created:

app.module.ts

...
@NgModule({
  providers: [
    { provide: BugSplatConfigToken, useValue: BUGSPLAT_CONFIG },
    { provide: ErrorHandler, useClass: MyAngularErrorHandler }
  ]
  ...
})

Contributing

BugSplat loves open source software! If you have suggestions on how we can improve this integration, please reach out to support@bugsplat.com, create an issue in our GitHub repo or send us a pull request.