chore(api): add logging for 404 not found requests (#59068)

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
This commit is contained in:
Sem Bauke
2025-03-11 17:19:18 +01:00
committed by GitHub
parent fb8bf0d804
commit 8bb352b67e

View File

@@ -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}`
});
}
});