'use strict';

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

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

/**
 * global callback error handler
 * @param req
 * @param res
 */
var errorCallback = function errorCallback(err, req, res, code) {
  if (err instanceof EntityTooLargeException.EntityTooLargeException) {
    return res.jsonError(413, err.message);
  }

  return res.jsonError(code || 500, 'internal server error');
};

var setErrorCallback = function setErrorCallback(callback) {
  errorCallback = callback;
};
var handleError = function handleError(err, callback, logger, request, response, code) {
  err = utils.isString(err) ? new Error(err) : err;
  logger.fatal(err);

  if (response.writableEnded || response.finished) {
    return Promise.resolve(true);
  } else {
    return (callback || errorCallback)(err, request, response, code);
  }
};
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;
exports.setErrorCallback = setErrorCallback;
//# sourceMappingURL=Utils.js.map
