errors.controller.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { HTTPStatus } from 'deps';
  2. import { logger } from 'infra/logger.ts';
  3. import Controller from 'if/controllers/controller.class.ts';
  4. function notFoundController(): Controller {
  5. const controller = new Controller();
  6. controller.setHandler('*', '*', (req: Request) => {
  7. return controller.response(req, 'not found', {
  8. status: HTTPStatus.NotFound,
  9. });
  10. });
  11. return controller;
  12. }
  13. function internalServerErrorController(): Controller {
  14. const controller = new Controller();
  15. controller.setHandler('*', '*', (req: Request, error?: string) => {
  16. if (error) {
  17. logger.error(error);
  18. }
  19. return controller.response(req, 'internal server error', {
  20. status: HTTPStatus.InternalServerError,
  21. });
  22. });
  23. return controller;
  24. }
  25. function handle(errorController: Controller, req: Request, error?: string) {
  26. return errorController.getHandler('*', '*')(req, error);
  27. }
  28. export const ControllerErrors = {
  29. NotFound: (req: Request) => handle(notFoundController(), req),
  30. InternalServerError: (req: Request, error: string) =>
  31. handle(internalServerErrorController(), req, error),
  32. };