From ef1b34294de651cef0cf3ee97462b710d0a836b3 Mon Sep 17 00:00:00 2001 From: Michael Liendo Date: Fri, 19 Jul 2024 13:12:02 -0400 Subject: [PATCH] fix: error handler (#15) * chore: reorder imports * chore: add a error handle * chore: format code * chore: add logger --- server/src/controllers/Auth/login.ts | 26 ++++++++++--------------- server/src/controllers/Auth/register.ts | 25 +++++++++--------------- server/src/controllers/User/me.ts | 9 +-------- server/src/middlewares/checkJwt.ts | 6 ++++-- server/src/routes/index.ts | 17 +++++++++++++++- server/src/routes/user/index.ts | 5 +++-- server/src/utils/errorHandler.ts | 8 ++++++-- 7 files changed, 49 insertions(+), 47 deletions(-) diff --git a/server/src/controllers/Auth/login.ts b/server/src/controllers/Auth/login.ts index 6e1f8ec..5497071 100644 --- a/server/src/controllers/Auth/login.ts +++ b/server/src/controllers/Auth/login.ts @@ -1,9 +1,10 @@ -import type { IUserForLogin } from '@linx/shared'; import validator from 'validator'; + +import Services from '../../services'; import { BadRequestError } from '../../utils/errorHandler'; +import type { IUserForLogin } from '@linx/shared'; import type { FastifyRequest } from 'fastify'; -import Services from '../../services'; import type { Reply } from '../../types'; export default async function login(request: FastifyRequest, reply: Reply) { @@ -17,19 +18,12 @@ export default async function login(request: FastifyRequest, reply: Reply) { throw new BadRequestError('Please provide a valid email address'); } - try { - const user = await Services.auth.login({ - email, - password, - }); + const user = await Services.auth.login({ + email, + password, + }); - return reply - .code(201) - .send({ message: 'User created', data: { token: user } }); - } catch (error) { - return reply.code(error).send({ - message: 'Internal server error', - error: error.message as string, - }); - } + return reply + .code(201) + .send({ message: 'User created', data: { token: user } }); } diff --git a/server/src/controllers/Auth/register.ts b/server/src/controllers/Auth/register.ts index b926aff..05e7a97 100644 --- a/server/src/controllers/Auth/register.ts +++ b/server/src/controllers/Auth/register.ts @@ -1,10 +1,10 @@ import validator from 'validator'; import Services from '../../services'; -import type { Reply, Request } from '../../types'; import { BadRequestError } from '../../utils/errorHandler'; import type { IUserForRegister } from '@linx/shared'; +import type { Reply, Request } from '../../types'; export default async function register(request: Request, reply: Reply) { const { first_name, last_name, email, password } = @@ -22,19 +22,12 @@ export default async function register(request: Request, reply: Reply) { throw new BadRequestError('Password must be at least 4 characters long'); } - try { - const user = await Services.auth.register({ - first_name, - last_name, - email, - password, - }); - - return reply.code(201).send({ message: 'User created', data: { ...user } }); - } catch (error) { - return reply.code(500).send({ - message: 'Internal server error', - error: error.message as string, - }); - } + const user = await Services.auth.register({ + first_name, + last_name, + email, + password, + }); + + return reply.code(201).send({ message: 'User created', data: { ...user } }); } diff --git a/server/src/controllers/User/me.ts b/server/src/controllers/User/me.ts index f85d6fc..2c74410 100644 --- a/server/src/controllers/User/me.ts +++ b/server/src/controllers/User/me.ts @@ -2,12 +2,5 @@ import type { Reply, Request } from '../../types'; export default async function me(request: Request, reply: Reply) { const { password, ...user } = request.user; - try { - return reply.code(200).send({ message: 'Ok', data: { user } }); - } catch (error) { - return reply.code(500).send({ - message: 'Internal server error', - error: error.message as string, - }); - } + return reply.code(200).send({ message: 'Ok', data: { user } }); } diff --git a/server/src/middlewares/checkJwt.ts b/server/src/middlewares/checkJwt.ts index e7ea5d4..f84b9e9 100644 --- a/server/src/middlewares/checkJwt.ts +++ b/server/src/middlewares/checkJwt.ts @@ -1,10 +1,12 @@ -import type { DoneFuncWithErrOrRes, FastifyInstance } from 'fastify'; import fastifyPlugin from 'fastify-plugin'; + import Services from '../services'; -import type { Request } from '../types'; import { UnauthorizedError } from '../utils/errorHandler'; import { Jwt } from '../utils/jwt'; +import type { DoneFuncWithErrOrRes, FastifyInstance } from 'fastify'; +import type { Request } from '../types'; + function getUser(fastify: FastifyInstance, _, done: DoneFuncWithErrOrRes) { fastify.decorateRequest('user', null); diff --git a/server/src/routes/index.ts b/server/src/routes/index.ts index 3e433f4..161ba13 100644 --- a/server/src/routes/index.ts +++ b/server/src/routes/index.ts @@ -1,17 +1,32 @@ import auth from './auth'; +import user from './user'; import type { DoneFuncWithErrOrRes, FastifyInstance, RegisterOptions, } from 'fastify'; -import user from './user'; +import type { ErrorWithDetails } from '../utils/errorHandler'; export default function routes( fastify: FastifyInstance, _: RegisterOptions, done: DoneFuncWithErrOrRes, ) { + fastify.setErrorHandler((error: ErrorWithDetails, _, reply) => { + if (error.statusCode >= 500) { + fastify.log.error(error); + } else if (error.statusCode >= 400) { + fastify.log.info(error); + } + + return reply.code(error.statusCode || 500).send({ + error: error.name || 'INTERNAL_SERVER_ERROR', + message: error.message, + details: error.details, + }); + }); + fastify.get('/', async () => { return { hello: 'world' }; }); diff --git a/server/src/routes/user/index.ts b/server/src/routes/user/index.ts index a5ba810..1488366 100644 --- a/server/src/routes/user/index.ts +++ b/server/src/routes/user/index.ts @@ -1,11 +1,12 @@ +import me from '../../controllers/User/me'; +import checkJwt from '../../middlewares/checkJwt'; + import type { IReply } from '@linx/shared'; import type { DoneFuncWithErrOrRes, FastifyInstance, RegisterOptions, } from 'fastify'; -import me from '../../controllers/User/me'; -import checkJwt from '../../middlewares/checkJwt'; export default function user( fastify: FastifyInstance, diff --git a/server/src/utils/errorHandler.ts b/server/src/utils/errorHandler.ts index 574669f..4576223 100644 --- a/server/src/utils/errorHandler.ts +++ b/server/src/utils/errorHandler.ts @@ -7,22 +7,26 @@ export abstract class ErrorWithDetails extends Error { ) { super(message); } - abstract statusCode: StatusCodes; + abstract statusCode; } export class BadRequestError extends ErrorWithDetails { + name = 'BAD_REQUEST'; statusCode = StatusCodes.BAD_REQUEST; - details = 'You need add'; } export class ConflictError extends ErrorWithDetails { + name = 'CONFLICT'; statusCode = StatusCodes.CONFLICT; } export class ForbiddenError extends ErrorWithDetails { + name = 'FORBIDDEN'; statusCode = StatusCodes.FORBIDDEN; } export class NotFoundError extends ErrorWithDetails { + name = 'NOT_FOUND'; statusCode = StatusCodes.NOT_FOUND; } export class UnauthorizedError extends ErrorWithDetails { + name = 'UNAUTHORIZED'; statusCode = StatusCodes.UNAUTHORIZED; }