'use strict';

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

var EntityTooLargeException = require('../Exceptions/EntityTooLargeException');
var utils = require('@teclone/utils');
var Constants = require('./Constants');

var handleError = function handleError(err, response, code) {
  var _err;

  var request = response.request;
  var logger = response.logger;
  var errorCallback = response.errorCallback;
  err = utils.isString(err) ? new Error(err) : err;
  logger.logError(err);

  if (response.writableEnded || response.finished) {
    return Promise.resolve(true);
  }

  if (errorCallback) {
    return errorCallback(err, request, response, code);
  }

  if (err instanceof EntityTooLargeException.EntityTooLargeException) {
    return response.jsonError({
      statusCode: 413,
      message: err.message
    });
  }

  return response.jsonError({
    statusCode: code || 500,
    message: 'internal server error',
    data: response.config.env === 'production' ? null : {
      errorStack: ((_err = err) === null || _err === void 0 ? void 0 : _err.stack) || ''
    }
  });
};
var getRouteKeys = function getRouteKeys() {
  var method = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '*';
  method = utils.makeArray(method);

  if (method.findIndex(function (current) {
    return current === '*';
  }) > -1) {
    return Constants.ROUTE_KEYS;
  } else {
    return method;
  }
};

exports.getRouteKeys = getRouteKeys;
exports.handleError = handleError;
//# sourceMappingURL=Utils.js.map
