Last active
March 12, 2018 11:33
-
-
Save saschwarz/bee0618f5604646dfe56ca61124e5122 to your computer and use it in GitHub Desktop.
Rollbar JS in Angular 4/Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import { RollbarService, RollbarErrorHandler, rollbarFactory } from './rollbar'; | |
@NgModule({ | |
... | |
providers: [ | |
{ | |
provide: ErrorHandler, | |
useClass: RollbarErrorHandler | |
}, | |
{ | |
provide: RollbarService, | |
useFactory: rollbarFactory | |
}, | |
... | |
] | |
}) | |
export class AppModule { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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