exceptions.js 722 B

12345678910111213141516171819202122232425262728293031
  1. // ./common/exceptions.js
  2. import ApplicationError from "../libs/errors/application.error.js";
  3. import l from "./logger.js";
  4. const notifyException = async (req, error) => {
  5. l.error(req);
  6. l.error(error);
  7. if (process.env.APP_ENV !== "development") {
  8. // await airbrakeClient.notify(error)
  9. }
  10. };
  11. const handleException = (req, res, error) => {
  12. notifyException(req, error);
  13. if (error instanceof ApplicationError) {
  14. res.status(error.status).json(error);
  15. } else {
  16. res.status(500).json(error);
  17. }
  18. res.status(500).json(error);
  19. };
  20. const wrap =
  21. (fn) =>
  22. (...args) =>
  23. fn(...args).catch((e) => {
  24. handleException(args[0], args[1], e);
  25. });
  26. export { handleException, notifyException, wrap };