Skip to content

Instantly share code, notes, and snippets.

@saschwarz
Last active March 12, 2018 11:33
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save saschwarz/bee0618f5604646dfe56ca61124e5122 to your computer and use it in GitHub Desktop.
Save saschwarz/bee0618f5604646dfe56ca61124e5122 to your computer and use it in GitHub Desktop.
Rollbar JS in Angular 4/Typescript
...
import { RollbarService, RollbarErrorHandler, rollbarFactory } from './rollbar';
@NgModule({
...
providers: [
{
provide: ErrorHandler,
useClass: RollbarErrorHandler
},
{
provide: RollbarService,
useFactory: rollbarFactory
},
...
]
})
export class AppModule { }
import * as Rollbar from 'rollbar';
import { ErrorHandler } from '@angular/core';
import { Injectable, Injector } from '@angular/core';
import { InjectionToken } from '@angular/core';
// config here or put in config module and import
export let rollbarConfig = {
accessToken: 'YOUR_TOKEN',
captureUncaught: true,
captureUnhandledRejections: true,
enabled: true,
environment: 'YOUR_ENV_NAME'
}
@Injectable()
export class RollbarErrorHandler implements ErrorHandler {
constructor(private injector: Injector) {}
handleError(err:any) : void {
var rollbar = this.injector.get(RollbarService);
rollbar.error(err.originalError || err);
}
}
export function rollbarFactory() {
return new Rollbar(rollbarConfig);
}
export const RollbarService = new InjectionToken<Rollbar>('rollbar');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment