<commit_before>const errorCreator = require('../objects/error/errorCreator');
const appConfig = require('../config/defaults/config').app;

/**
 * Checks the type of error and sends it in response
 * @param {Object} params.response Router response
 * @param {Object} params.error Error object
 * @param {string} [params.title] Error title
 * @param {string} [params.detail] Error detail
 * @param {Object} [params.sentData] Data sent
 */
function checkAndSendError({ response, error, title, detail, sentData }) {
  const sendError = {
    status: 500,
    title: title || 'Internal server error',
    detail: detail || 'Internal server error',
  };

  if ((appConfig.mode === appConfig.Modes.TEST || appConfig.mode === appConfig.Modes.DEV) && sentData) { sendError.sentData = sentData; }

  switch (error.type) {
    case errorCreator.ErrorTypes.DOESNOTEXIST: {
      sendError.status = 404;
      sendError.title = title || 'Does not exist';
      sendError.detail = detail || 'Does not exist';

      break;
    }
    case errorCreator.ErrorTypes.NOTALLOWED: {
      sendError.status = 401;
      sendError.title = title || 'Unauthorized';
      sendError.detail = detail || 'Not allowed';

      break;
    }
    case errorCreator.ErrorTypes.INVALIDCHARACTERS: {
      sendError.status = 400;
      sendError.title = title || 'Invalid characters or length';
      sendError.detail = detail || 'Invalid characters or length';

      break;
    }
    case errorCreator.ErrorTypes.ALREADYEXISTS: {
      sendError.status = 403;
      sendError.title = title || 'Already exists';
      sendError.detail = detail || 'Already exists';

      break;
    }
    case errorCreator.ErrorTypes.INVALIDDATA: {
      sendError.status = 400;
      sendError.title = title || 'Invalid data';
      sendError.detail = detail || 'Invalid data';

      break;
    }
    case errorCreator.ErrorTypes.INVALIDMAIL: {
      sendError.status = 400;
      sendError.title = title || 'Invalid mail address';
      sendError.detail = detail || 'Invalid mail address';

      break;
    }
    default: {
      break;
    }
  }

  response.status(sendError.status).json({
    error: sendError,
  });
}

exports.checkAndSendError = checkAndSendError;
<commit_msg>Better error detail is sent, taken from the error object<commit_after>const errorCreator = require('../objects/error/errorCreator');
const appConfig = require('../config/defaults/config').app;

/**
 * Checks the type of error and sends it in response
 * @param {Object} params.response Router response
 * @param {Object} params.error Error object
 * @param {string} [params.title] Error title
 * @param {string} [params.detail] Error detail
 * @param {Object} [params.sentData] Data sent
 */
function checkAndSendError({ response, error, title, detail, sentData }) {
  const errorDetail = error.text.join('') || detail;
  const sendError = {
    status: 500,
    title: title || 'Internal server error',
    detail: errorDetail || 'Internal server error',
  };

  if ((appConfig.mode === appConfig.Modes.TEST || appConfig.mode === appConfig.Modes.DEV) && sentData) { sendError.sentData = sentData; }

  switch (error.type) {
    case errorCreator.ErrorTypes.DOESNOTEXIST: {
      sendError.status = 404;
      sendError.title = title || 'Does not exist';
      sendError.detail = errorDetail || 'Does not exist';

      break;
    }
    case errorCreator.ErrorTypes.NOTALLOWED: {
      sendError.status = 401;
      sendError.title = title || 'Unauthorized';
      sendError.detail = errorDetail || 'Not allowed';

      break;
    }
    case errorCreator.ErrorTypes.INVALIDCHARACTERS: {
      sendError.status = 400;
      sendError.title = title || 'Invalid characters or length';
      sendError.detail = errorDetail || 'Invalid characters or length';

      break;
    }
    case errorCreator.ErrorTypes.ALREADYEXISTS: {
      sendError.status = 403;
      sendError.title = title || 'Already exists';
      sendError.detail = errorDetail || 'Already exists';

      break;
    }
    case errorCreator.ErrorTypes.INVALIDDATA: {
      sendError.status = 400;
      sendError.title = title || 'Invalid data';
      sendError.detail = errorDetail || 'Invalid data';

      break;
    }
    case errorCreator.ErrorTypes.INVALIDMAIL: {
      sendError.status = 400;
      sendError.title = title || 'Invalid mail address';
      sendError.detail = errorDetail || 'Invalid mail address';

      break;
    }
    default: {
      break;
    }
  }

  response.status(sendError.status).json({
    error: sendError,
  });
}

exports.checkAndSendError = checkAndSendError;

{'fraction_non_alphanumeric': 0.12323407954792437, 'fraction_numerical': 0.009128450336883287, 'mean_word_length': 3.1016042780748663, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5257259', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1327, 'n_words': 341}