'use strict';

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

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var EntityTooLargeException = _interopDefault(require('../Exceptions/EntityTooLargeException'));
var utils = require('@forensic-js/utils');

/**
 * global callback error handler
 * @param req
 * @param res
 */

var errorCallback = function errorCallback(err, req, res, code) {
  if (err instanceof 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.finished) {
    return (callback || errorCallback)(err, request, response, code);
  } else {
    return Promise.resolve(true);
  }
};

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