const { constants } = require("./constants");
const errorHandler = (err, req, res, next) => {
  const statusCode = res.statusCode ? res.statusCode : 500;

  switch (statusCode) {
    case constants.VALIDATION_ERROR:
      res.status(statusCode).json({
        title: "Validation Failed ",
        msg: err.message,
        stackTrace: err.stack,
      });
      break;
      case constants.Method_Not_Allowed:
        res.status(statusCode).json({
          title: "Method Not Allowed ",
          msg: err.message,
          stackTrace: err.stack,
        });
        break;
    case constants.NOT_FOUND:
      res.status(statusCode).json({
        title: "Not found ",
        msg: err.message,
        stackTrace: err.stack,
      });
      break;
    case constants.FORBIDOEN:
      res.status(statusCode).json({
        title: "Forbidoen ",
        msg: err.message,
        stackTrace: err.stack,
      });
      break;
    case constants.SERVER_ERROR:
      res.status(statusCode).json({
        title: "Server Error ",
        msg: err.message,
        stackTrace: err.stack,
      });
      break;
    case constants.UNAUTHORIZED:
      res.status(statusCode).json({
        title: "Unauthorized ",
        msg: err.message,
        stackTrace: err.stack,
      });
      break;
    case constants.SITE_UNAUTHORIZED:
      res.status(statusCode).json({
        title: "Site Unauthorized ",
        msg: err.message,
        stackTrace: err.stack,
      });
      break;
    case constants.TOO_MANY:
      res.status(statusCode).json({
        title: "Too many request",
        msg: err.message,
        stackTrace: err.stack,
      });
      break;
    default:
      console.log(err);
      res.status(500).json({ 
       title: "Unhandled error",
       msg: err.message,
       stackTrace: err.stack 
      });
      break;
  }
};

module.exports = { name: "errorHandler", run: errorHandler };
