Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable sentry on dev environment when using angular plugin #436

Closed
odedfos opened this issue Dec 17, 2015 · 15 comments
Closed

Disable sentry on dev environment when using angular plugin #436

odedfos opened this issue Dec 17, 2015 · 15 comments

Comments

@odedfos
Copy link

odedfos commented Dec 17, 2015

At the moment it is not possible to disable the sentry plugin because the ngRaven module fails to load.

On the development environment I use the following without providing the DSN
Raven.config().install()

The callback that registers the raven provider is not called

Raven.addPlugin(function () {
    angular.module('ngRaven', [])
        .provider('Raven',  RavenProvider)
        .config(['$provide', ExceptionHandlerProvider]);
});

Because the install function does not call the plugin that installs the module
isSetup() failed because the globalServer is null

install: function() {
    if (isSetup() && !isRavenInstalled) {
        TraceKit.report.subscribe(handleStackInfo);

        // Install all of the plugins
        each(plugins, function(_, plugin) {
            plugin();
        });

        isRavenInstalled = true;
    }

    return Raven;
},
@Sija
Copy link
Contributor

Sija commented Dec 17, 2015

Related to #414 and #413

@benvinegar
Copy link
Contributor

@Sija @odedfos – can you comment on #414? Like, will that PR address this issue?

@Sija
Copy link
Contributor

Sija commented Dec 18, 2015

@benvinegar seems legit for me, although I could imagine case where you'd want Raven provider to be available, even without calling .config() — like setting Raven.debug flag for instance

@nblasgen
Copy link

I had a similar request from @benvinegar and he pointed out that you can use:

Raven.config('your dsn', {
  shouldSendCallback: function () {
    return false;
  }
}).install();

And that will stop Raven from reporting. Switch the return status to TRUE and it will start reporting. At least this is what I've been told. We plan to use this plus a deployment sed command to flip the boolean at deployment time.

@chancez
Copy link

chancez commented Feb 15, 2017

I did something similar to the above to allow install() to suceed, and to avoid making any http requests to actually send the errors (window.SERVER_FLAGS is how I pass my sentryURL from my backend to the frontend).

if (!window.SERVER_FLAGS.sentryURL) {
  // Never bother sending data to sentry
  Raven.setShouldSendCallback(function() { return false });
  // Allow Raven.install() to succeed
  Raven.isSetup = function() {
    return true;
  }
}
Raven
.config(window.SERVER_FLAGS.sentryURL, {
  release: window.SERVER_FLAGS.version,
  debug: true,
})
.addPlugin(Raven.Plugins.Angular)
.install();

This seems to be working fairly well, and still lets Raven run, but it simply doesn't report. I really dislike having to hack isSetup but its the only way I could find that was minimally invasive to get ngRaven loaded.

@jo-tham
Copy link

jo-tham commented Jun 5, 2017

For others who stumble upon this

If you are using webpack to build your application, you use environment variables to configure raven by using the environment plugin

https://webpack.js.org/plugins/environment-plugin/

e.g.

if (process.env.NODE_ENV === 'dev') {
  Raven.setShouldSendCallback(() => { return false; });
  Raven.isSetup = () => { return true; };
}

Raven
  .config(process.env.RAVEN_DSN, {
    debug: process.env.RAVEN_DEBUG,
  })
  .addPlugin(require('raven-js/plugins/angular'), angular)
  .install();

@JoannaFalkowska
Copy link

JoannaFalkowska commented Aug 2, 2017

isSetup is now read-only, so the above method no longer works.

I found a following workaround, which seems to work, although I have no idea why.

let isProduction = process.env.ENV === 'build'; // variable provided by webpack

Raven
.config('https://<key>@sentry.io/<project>', {
  shouldSendCallback: function () {
    return isProduction;
  }
})
.install();

if (!isProduction) {
  Raven.uninstall(); // this is necessary! for some reason
}

export class RavenErrorHandler implements ErrorHandler {
  handleError(err: any): void {
    console.error(err); // this still fires after uninstalling!!! it's because it's already listed as Angular provider 
    Raven.captureException(err)
  }
}

It's still an awful solution because it hijacks all my errors so I don't know from which line they were thrown. At least I guess it's not as terrible as not seeing them at all.

@paragjnath
Copy link

I have used this way. It seems working for me

`if (environment.production) {
Raven.config('https://@sentry.io/')
.install();
}

and in providers
providers: [environment.production ? { provide: ErrorHandler, useClass: RavenErrorHandler } : [], ...`

Please let me know if I am doing anything wrong here.

@kamilogorek
Copy link
Contributor

Looks fine to me. Apparently, original issue has been answered here already, so closing this one. Feel free to reopen if it's still relevant in any way.

@jimmykane
Copy link

jimmykane commented Apr 23, 2018

More easy at 'app.module.ts'

import {environment} from '../environments/environment';
...
    providers: [
    LocalStorageService,
    EventLocalStorageService,
    EventService,
    ActionButtonService,
    WeatherUndergroundWeatherService,
    GeoLocationInfoService,
    AppEventColorService,
    // {provide: ErrorHandler, useClass: RavenErrorHandler}
    {provide: ErrorHandler, useClass: environment.production ? RavenErrorHandler : ErrorHandler} // See here
  ],

and

Raven
  .config('key', {
    shouldSendCallback: function () {
      return environment.production;
    }
  })
  .install();

This way you have the localdev messages in your console (else Sentry grabs some) and at production you get the ones you want to have

@igregson
Copy link

igregson commented May 11, 2018

At least in Chrome (not sure about other browsers), another way to prevent Raven from "hijacking" the dev console is to blackbox raven.js:

https://gist.github.com/paulirish/c307a5a585ddbcc17242
https://developer.chrome.com/devtools/docs/blackboxing

2018-05-11_12-45-12

@artuska
Copy link

artuska commented Dec 8, 2019

And what about Sentry? I don't use Raven, i have Sentry and i want to disable it on development environment on my localhost.

https://dev.to/angular/tracking-errors-in-angular-with-sentry-4oo0 — here it is

@kamilogorek
Copy link
Contributor

@artuska you can either provide empty DSN or use beforeSend to halt the transport.

Sentry.init({
  dsn: process.env.development ? '' : 'your-real-dsn'
})

or

Sentry.init({
  dsn: 'your-real-dsn',
  beforeSend(event) {
    if (process.env.development) return null;
    return event;
  }
})

@jimmykane
Copy link

jimmykane commented Dec 9, 2019 via email

@kamilogorek
Copy link
Contributor

@jimmykane correct, if you want to disable Sentry, just call init conditionally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests