From 72abb9f5b5994ea4b2f06e7a36a16d65d119888b Mon Sep 17 00:00:00 2001 From: Moussa Kayad Date: Fri, 19 Jul 2024 11:52:45 +0100 Subject: [PATCH 1/3] FPO-326 add 404 and 500 page --- src/app.ts | 8 ++------ views/404.njk | 16 ++++++++++++++++ views/500.njk | 11 +++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 views/404.njk create mode 100644 views/500.njk diff --git a/src/app.ts b/src/app.ts index 28ed3e29..c507631f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,6 @@ import { type Express, type Request, type Response, type NextFunction } from 'express' import cookieSession from 'cookie-session' -import createError from 'http-errors' import express from 'express' import favicon from 'serve-favicon' import morgan from 'morgan' @@ -84,7 +83,7 @@ app.use('/dashboard', dashboardRoutes) // catch 404 and forward to error handler app.use(function (_req: Request, _res: Response, next: NextFunction) { - next(createError(404)) + _res.render('404') }) // Error handler @@ -96,10 +95,7 @@ app.use(function (err: any, req: Request, res: Response, _next: NextFunction) { res.status(statusCode) - res.json({ - message: err.message, - error: req.app.get('env') === 'development' ? err : {} - }) + res.render('500') }) app.listen(port, () => { diff --git a/views/404.njk b/views/404.njk new file mode 100644 index 00000000..cc572f60 --- /dev/null +++ b/views/404.njk @@ -0,0 +1,16 @@ +{% extends "layout.njk" %} +{% set mainClasses = "govuk-main-wrapper--l" %} +{% block title %}Page not found{% endblock %} +{% block content %} +
+
+

Page not found

+

+ If you typed the web address, check it is correct. +

+

+ If you pasted the web address, check you copied the entire address. +

+
+
+{% endblock %} diff --git a/views/500.njk b/views/500.njk new file mode 100644 index 00000000..d85ba9fe --- /dev/null +++ b/views/500.njk @@ -0,0 +1,11 @@ +{% extends "layout.njk" %} +{% set mainClasses = "govuk-main-wrapper--l" %} +{% block title %}Sorry, there is a problem with the service{% endblock %} +{% block content %} +
+
+

Sorry, there is a problem with the service

+

Try again later.

+
+
+{% endblock %} From 0e86232252e47f4b4649bfaa2e4d295d4ffb6df8 Mon Sep 17 00:00:00 2001 From: Moussa Kayad Date: Thu, 25 Jul 2024 10:17:12 +0100 Subject: [PATCH 2/3] Improve the error handling --- src/app.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/app.ts b/src/app.ts index c507631f..d9ea54c9 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,7 @@ import { type Express, type Request, type Response, type NextFunction } from 'express' import cookieSession from 'cookie-session' +import createError from 'http-errors' import express from 'express' import favicon from 'serve-favicon' import morgan from 'morgan' @@ -83,7 +84,7 @@ app.use('/dashboard', dashboardRoutes) // catch 404 and forward to error handler app.use(function (_req: Request, _res: Response, next: NextFunction) { - _res.render('404') + next(createError(404)) }) // Error handler @@ -91,11 +92,16 @@ app.use(function (err: any, req: Request, res: Response, _next: NextFunction) { res.locals.message = err.message res.locals.error = req.app.get('env') === 'development' ? err : {} - const statusCode: number = err.statusCode ?? 500 - + const statusCode: number = err.statusCode + console.log(err.statusCode) res.status(statusCode) - - res.render('500') + switch (statusCode) { + case 404: + res.render('404') + break + default: + res.render('500') + } }) app.listen(port, () => { From 4f3573072fb979b75672aeea061726814ca0d5d2 Mon Sep 17 00:00:00 2001 From: Moussa Kayad Date: Thu, 25 Jul 2024 14:39:50 +0100 Subject: [PATCH 3/3] Remove log --- src/app.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 73225e73..4e02560b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -96,7 +96,6 @@ app.use(function (err: any, req: Request, res: Response, _next: NextFunction) { res.locals.error = req.app.get('env') === 'development' ? err : {} const statusCode: number = err.statusCode - console.log(err.statusCode) res.status(statusCode) switch (statusCode) { case 404: