import Ember  from 'ember';
import config from '../config/environment';
import { getContext, generateError } from 'ember-cli-bugsnag/utils/errors';

var currentEnv = config.environment;

export default {
  name: 'bugsnag-error-service',

  initialize: function(container) {
    if (typeof Bugsnag === 'undefined') { return; }

    if (currentEnv !== 'test' && Bugsnag.notifyReleaseStages.indexOf(currentEnv) !== -1) {
      Ember.onerror = function (error) {
        Bugsnag.context = getContext(container.lookup('router:main'));
        Bugsnag.notifyException(error);
        console.error(error.stack);
      };

      Ember.RSVP.on('error', function(error) {
        Bugsnag.context = getContext(container.lookup('router:main'));
        Bugsnag.notifyException(error);
        console.error(error.stack);
      });

      Ember.Logger.error = function (message, cause, stack) {
        Bugsnag.context = getContext(container.lookup('router:main'));
        Bugsnag.notifyException(generateError(cause, stack), message);
        console.error(stack);
      };
    }
  }
};

import Ember  from 'ember';
import config from '../config/environment';
import { getContext, generateError } from 'ember-cli-bugsnag/utils/errors';

var currentEnv = config.environment;

export default {
  name: 'bugsnag-error-service',

  initialize: function(container) {
    if (typeof Bugsnag === 'undefined') { return; }

    if (currentEnv !== 'test' && Bugsnag.notifyReleaseStages.indexOf(currentEnv) !== -1) {
      Ember.onerror = function (error) {
        Bugsnag.context = getContext(container.lookup('router:main'));
        Bugsnag.notifyException(error);
        console.error(error.stack);
      };

      Ember.RSVP.on('error', function(error) {
        Bugsnag.context = getContext(container.lookup('router:main'));
        Bugsnag.notifyException(error);
        console.error(error.stack);
      });

      Ember.Logger.error = function (message, cause, stack) {
        Bugsnag.context = getContext(container.lookup('router:main'));
        Bugsnag.notifyException(generateError(cause, stack), message);
        console.error(stack);
      };

      const router = container.lookup('router:main');
      const originalDidTransition = router.didTransition || Ember.K;
      router.didTransition = function() {
        Bugsnag.refresh();
        return originalDidTransition.apply(this, arguments);
      };
    }
  }
};

Refresh Bugsnag's rate limit on each transition
Refresh Bugsnag's rate limit on each transition

From https://bugsnag.com/docs/notifiers/js#rate-limiting:

> By default only 10 errors are allowed per page load. This is to prevent wasting a
> user's bandwidth sending thousands of exceptions to Bugsnag. If you have a long-running
> single page app, you can reset this rate-limit from your router by using:
> `Bugsnag.refresh()`
