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

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

  initialize: function(instance) {
    var content = '';
    var bugsnagConfig;
    var envArray;

    if (!config.bugsnag) {
      console.warn('`config.bugsnag` is not defined, using environment variables instead.');

      var envApiKey = process.env['BUGSNAG_API_KEY'];
      var envReleases = process.env['BUGSNAG_NOTIFY_RELEASE'];
      var envEndpoint = process.env['BUGSNAG_ENDPOINT'];

      if (!envApiKey || !envReleases) {
        console.error('Environment variables `BUGSNAG_API_KEY` and `BUGSNAG_NOTIFY_RELEASE` are not specified. Bugsnag will not be injected.');

        return '';
      }

      bugsnagConfig = {
        apiKey: envApiKey,
        notifyReleaseStages: envReleases.split(','),
        releaseStage: process.env['BUGSNAG_RELEASE_STAGE'],
        endpoint: envEndpoint
      };
    } else {
      bugsnagConfig = config.bugsnag;
    }

    if (!bugsnagConfig.notifyReleaseStages) {
      bugsnagConfig.notifyReleaseStages = ["production"]
    }

    Object.keys(bugsnagConfig).forEach(function(key) {
      Bugsnag[key] = bugsnagConfig[key];
    });
  }
};

import Ember  from 'ember';
import config from '../config/environment';
import Bugsnag from 'bugsnag';

export default {
  name: 'ember-cli-bugsnag',

  initialize: function(instance) {
    let bugsnagConfig;

    if (!config.bugsnag) {
      console.info('[ember-cli-bugsnag] `config.bugsnag` is not defined, using environment variables instead.');

      bugsnagConfig = {
        apiKey: process.env['BUGSNAG_API_KEY'],
        notifyReleaseStages: process.env['BUGSNAG_NOTIFY_RELEASE'].split(','),
        releaseStage: process.env['BUGSNAG_RELEASE_STAGE'],
        endpoint: process.env['BUGSNAG_ENDPOINT']
      };
    } else {
      bugsnagConfig = config.bugsnag;
    }

    if (!bugsnagConfig.apiKey) {
      console.error('[ember-cli-bugsnag] Bugsnag reporting could not be started because `apiKey` was not configred. Please add it to `bugsnag.apiKey` in `config/environment` or define environment variable `BUGSNAG_API_KEY`');
      return;
    }

    if (!bugsnagConfig.notifyReleaseStages) {
      console.warn('[ember-cli-bugsnag] Notify release stages not defined in configuration, defaulting to `["production"]`.');
      bugsnagConfig.notifyReleaseStages = ["production"]
    }

    Object.keys(bugsnagConfig).forEach(function(key) {
      Bugsnag[key] = bugsnagConfig[key];
    });
  }
};

Tidy up initialization code and error messages
Tidy up initialization code and error messages
