'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _raven = require('raven');

var _raven2 = _interopRequireDefault(_raven);

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _printTools = require('./print-tools');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

process.noDeprecation = true;

process.on('unhandledRejection', (reason, p) => {
  if (reason.name === 'RequestError' && reason.status === 403) {
    if (reason.errors.detail === 'No such API Key.') {
      const errorMessage = `API key from your config file is not valid. Use ${_chalk2.default.cyan('syncano-cli login')} to log in once again.`;
      (0, _printTools.echo)(errorMessage);
      (0, _printTools.echo)();
      return;
    }
  }

  const message = `Unhandled Rejection! ${reason}`;
  const fileIssue = 'Please file an issue here:\nhttps://github.com/Syncano/syncano-node-cli/issues';
  (0, _printTools.error)(message, fileIssue);
  console.log(reason.stack);
  if (process.env.NODE_ENV === 'test' || process.env.CLI_SHOW_STACK) {
    console.log(reason.stack);
  }
});

class ErrorResponse {
  constructor(context) {
    this.context = context;
    this.name = context.name;
    this.contextName = context.constructor.name.toLowerCase();
    this.session = this.context.session || null;
  }
  static checkErrorType(err) {
    if (err.name === 'RequestError') return 'requestError';
    if (err.errno < 0) return 'systemError';

    return 'default';
  }
  static handleRequestError(err, name, contextName) {
    if (err.status === 404) {
      const errorMessage = `"${name}" ${contextName} could not be found on your remote Syncano account!`;

      const url = _lodash2.default.has(err, 'response.request.url') ? `We've tried reaching the following url: ${err.response.request.url}` : null;

      (0, _printTools.error)(err, errorMessage, url);
      (0, _printTools.echo)();
      (0, _printTools.echo)(`Did you run ${_chalk2.default.green('syncano-cli deploy')} command?`);
      return;
    }
    (0, _printTools.error)(err);
  }

  static handleSystemError(err, name, contextName) {
    if (err.code === 'ENOENT') {
      (0, _printTools.error)('File or directory not found at:', err.path);
      return;
    }
    (0, _printTools.error)(err);
  }

  captureException(err) {
    const context = this.context;
    const session = context.session;

    if (session) {
      const account = session.settings.account;
      delete account.attributes.auth_key;
      _raven2.default.setContext({ user: { session, account } });
    } else {
      _raven2.default.setContext({ user: { context } });
    }

    _raven2.default.captureException(err);
  }

  handle(err) {
    const name = this.name;
    const contextName = this.contextName;
    const errorType = ErrorResponse.checkErrorType(err);

    this.captureException(err);
    const handleError = {
      requestError: () => ErrorResponse.handleRequestError(err, name, contextName),
      systemError: () => ErrorResponse.handleSystemError(err, name, contextName),
      default: () => (0, _printTools.error)(err)
    };

    return handleError[errorType]();
  }
}

exports.default = ErrorResponse;
module.exports = exports['default'];
//# sourceMappingURL=error-response.js.map