From ff4a891b3500c9670a356c35a500f595a7da09c8 Mon Sep 17 00:00:00 2001 From: Benny Samir Hierl Date: Sat, 5 Feb 2022 10:16:23 +0100 Subject: [PATCH] #1 not found handling improved Signed-off-by: Benny Samir Hierl --- src/api/config/errors/index.ts | 4 ++++ src/api/routes/index.ts | 4 ++++ src/api/routes/wishlist/read.ts | 5 +---- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/api/config/errors/index.ts b/src/api/config/errors/index.ts index 99bacf3..992af1b 100644 --- a/src/api/config/errors/index.ts +++ b/src/api/config/errors/index.ts @@ -10,6 +10,10 @@ class httpError extends Error { } } +export const notFoundError = () => { + return new httpError('Not Found', 404, '404') +} + export const uniqueKeyError = (msg: string, code = '4001') => { return new httpError(msg, 422, code) } diff --git a/src/api/routes/index.ts b/src/api/routes/index.ts index 222f45c..a4dbf16 100644 --- a/src/api/routes/index.ts +++ b/src/api/routes/index.ts @@ -1,10 +1,14 @@ import { FastifyInstance } from 'fastify' import { default as wishlistRoute } from './wishlist/' +import { notFoundError } from '../config/errors' export default { register: (app: FastifyInstance) => { return app.register( async (app) => { + app.setNotFoundHandler((request, reply) => { + reply.send(notFoundError()) + }) await app.register(wishlistRoute, { prefix: '/wishlist' }) }, { prefix: '/api' } diff --git a/src/api/routes/wishlist/read.ts b/src/api/routes/wishlist/read.ts index 389ad37..04fd5c9 100644 --- a/src/api/routes/wishlist/read.ts +++ b/src/api/routes/wishlist/read.ts @@ -68,10 +68,7 @@ export const getBySlugUrl = { if (list) { return list } else { - return reply.code(404).send({ - error: 'notFound', - http: 404, - }) + return reply.callNotFound() } }, }