angularbugsnag

Conditionally turn off Bugsnag in Angular


I am running Bugsnag in Angular 8.x and want to completely turn off reporting based on a property in environment.ts. I know how to pass the property, but I cannot figure out how to turn off reporting. What is the process for doing this?


Solution

  • I had a short look into Bugsnag docs for Angular: https://docs.bugsnag.com/platforms/javascript/angular/

    And found out that bugsnag is used as a provider:

    @NgModule({
      providers: [ { provide: ErrorHandler, useFactory: errorHandlerFactory } ]
    })
    

    I would change by adding an if where its possible to only add the provider for a specific environment:

    
    const providers = [];
    
    if (enviroment.production) {
      providers.push({ provide: ErrorHandler, useFactory: errorHandlerFactory });
    }
    
    @NgModule({
      providers,
    })