{"size": 651, "ext": "js", "max_stars_repo_path": "middlewares/errors/customErrorHandler.js", "max_stars_repo_name": "ksergenozel/question-answer-rest-api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "middlewares/errors/customErrorHandler.js", "max_issues_repo_name": "ksergenozel/question-answer-rest-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "middlewares/errors/customErrorHandler.js", "max_forks_repo_name": "ksergenozel/question-answer-rest-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.125, "max_line_length": 81, "alphanum_fraction": 0.6497695853}
const CustomError = require('../../helpers/errors/CustomError');

const customErrorHandler = (err, req, res, next) => {
  let customError = err;

  if (err.name === "SyntaxError") {
    customError = new CustomError('Unexpected Syntax', 400)
  }
  else if (err.name === "ValidationError") {
    customError = new CustomError(err.message, 400)
  }
  if (err.code === 11000) {
    customError = new CustomError('Duplicate key found. Check your input.', 400);
  }

  res
    .status(customError.status || 500)
    .json({
      success: false,
      message: customError.message || 'Internal server error.',
    })
}

module.exports = customErrorHandler;