text: const {
      ValidationError,
      NotFoundError,
      DBError,
      ConstraintViolationError,
      UniqueViolationError,
      NotNullViolationError,
      ForeignKeyViolationError,
      CheckViolationError,
      DataError,
  } = require("objection");
  
  function notFound(req, res, next) {
      res.status(404);
      const error = new Error(`🔍 - Not Found - ${req.originalUrl}`);
      next(error);
  }
  
  /* eslint-disable no-unused-vars */
  function errorHandler(err, req, res, next) {
      /* eslint-enable no-unused-vars */
      const statusCode = res.statusCode !== 200 ? res.statusCode : 500;
      const error = err.message || err;
      res.status(statusCode);
      console.error(err.stack, "********************", "Stack Error");
  
      if (process.env.NODE_ENV == "development" || "test") {
          console.log(err.stack);
      }
  
      console.error(error, "=========================", "Error");
      //TODO use objection validation for error messages https://vincit.github.io/objection.js/recipes/error-handling.html#examples
  
      if (err instanceof ValidationError) {
          switch (err.type) {
              case "ModelValidation":
                  res.status(400).send({
                      message: err.message,
                      type: err.type,
                      data: err.data,
                  });
                  break;
              case "RelationExpression":
                  res.status(400).send({
                      message: err.message,
                      type: "RelationExpression",
                      data: {},
                  });
                  break;
              case "UnallowedRelation":
                  res.status(400).send({
                      message: err.message,
                      type: err.type,
                      data: {},
                  });
                  break;
              case "InvalidGraph":
                  res.status(400).send({
                      message: err.message,
                      type: err.type,
                      data: {},
                  });
                  break;
              default:
                  res.status(400).send({
                      message: err.message,
                      type: "UnknownValidationError",
                      data: {},
                  });
                  break;
          }
      } else if (err instanceof NotFoundError) {
          res.status(404).send({
              message: err.message,
              type: "NotFound",
              data: {},
          });
      } else if (err instanceof UniqueViolationError) {
          res.status(409).send({
              message: err.message,
              type: "UniqueViolation",
              data: {
                  columns: err.columns,
                  table: err.table,
                  constraint: err.constraint,
              },
          });
      } else if (err instanceof NotNullViolationError) {
          res.status(400).send({
              message: err.message,
              type: "NotNullViolation",
              data: {
                  column: err.column,
                  table: err.table,
              },
          });
      } else if (err instanceof ForeignKeyViolationError) {
          res.status(409).send({
              message: err.message,
              type: "ForeignKeyViolation",
              data: {
                  table: err.table,
                  constraint: err.constraint,
              },
          });
      } else if (err instanceof CheckViolationError) {
          res.status(400).send({
              message: err.message,
              type: "CheckViolation",
              data: {
                  table: err.table,
                  constraint: err.constraint,
              },
          });
      } else if (err instanceof DataError) {
          res.status(400).send({
              message: err.message,
              type: "InvalidData",
              data: {},
          });
      } else if (err instanceof DBError) {
          res.status(500).send({
              message: err.message,
              type: "UnknownDatabaseError",
              data: {},
          });
      } else {
          switch (true) {
              case typeof err === "string":
                  // custom application error
                  const is404 = err.toLowerCase().endsWith("not found");
                  const statusCode = is404 ? 404 : 400;
                  return res.status(statusCode).json({
                      message: error,
                      stack:
                          process.env.NODE_ENV === "production"
                              ? "🥞"
                              : err.stack,
                  });
              case err.name === "UnauthorizedError":
                  // jwt authentication error
                  return res.status(401).json({
                      message: "Unauthorized",
                      stack:
                          process.env.NODE_ENV === "production"
                              ? "🥞"
                              : err.stack,
                  });
              default:
                  return res.status(500).json({
                      message: error,
                      stack:
                          process.env.NODE_ENV === "production"
                              ? "🥞"
                              : err.stack,
                  });
          }
      }
  }
  
  module.exports = {
      notFound,
      errorHandler,
  };
  