Skip to content

Commit

Permalink
refactor: improve error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hdev14 committed Feb 11, 2024
1 parent 850668f commit f183609
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NODE_ENV=development

SERVER_PORT=8000
SERVER_DOMAIN=''
SERVER_URL='http://locahost:8000'
SERVER_URL='http://localhost:8000'

DB_HOST=localhost
DB_PORT=5432
Expand Down
6 changes: 5 additions & 1 deletion src/application/middlewares/error_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { NextFunction, Request, Response } from 'express';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default function errorHandler(
error: Error,
_request: Request,
request: Request,
response: Response,
_next: NextFunction,
) {
console.log(error);

if (request.originalUrl.startsWith('/forms') || request.originalUrl.startsWith('/pages')) {
return response.redirect('/pages/500');
}

return response.status(500).json({ message: 'Internal Server Error' });
}
5 changes: 5 additions & 0 deletions src/application/pages/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{> _head}}
<main>
<h1>500</h1>
</main>
{{> _footer}}
6 changes: 6 additions & 0 deletions src/application/routers/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ router.get('/forgot-password', (_request: Request, response: Response) => {
});
});

router.get('/500', (_request: Request, response: Response) => {
response.render('500', {
title: 'Internal Server Error!',
});
});

export default router;

0 comments on commit f183609

Please sign in to comment.