From 8bb352b67edf2928d172e96ec9859a31e5c78ada Mon Sep 17 00:00:00 2001 From: Sem Bauke Date: Tue, 11 Mar 2025 17:19:18 +0100 Subject: [PATCH] chore(api): add logging for 404 not found requests (#59068) Co-authored-by: Shaun Hamilton --- api/src/plugins/not-found.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/api/src/plugins/not-found.ts b/api/src/plugins/not-found.ts index 4ed4b187d65..ca66e4a1305 100644 --- a/api/src/plugins/not-found.ts +++ b/api/src/plugins/not-found.ts @@ -14,17 +14,20 @@ import { getRedirectParams } from '../utils/redirection'; const fourOhFour: FastifyPluginCallback = (fastify, _options, done) => { // If the request accepts JSON and does not specifically prefer text/html, // this will return a 404 JSON response. Everything else will be redirected. - fastify.setNotFoundHandler((request, reply) => { - const accepted = request.accepts().type(['json', 'html']); + fastify.setNotFoundHandler((req, reply) => { + const logger = fastify.log.child({ req }); + const accepted = req.accepts().type(['json', 'html']); + + logger.debug('User requested path that does not exist'); if (accepted == 'json') { void reply.code(404).send({ error: 'path not found' }); } else { - const { origin } = getRedirectParams(request); + const { origin } = getRedirectParams(req); void reply.status(302); void reply.redirectWithMessage(`${origin}/404`, { type: 'danger', - content: `We couldn't find path ${request.url}` + content: `We couldn't find path ${req.url}` }); } });