From bcfba8b448ca4d192ba0ac789baeed1ab17354fc Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Fri, 23 Aug 2024 09:44:36 +0200 Subject: [PATCH] improve error handling --- src/exceptions/badRequestException.ts | 8 +++----- src/exceptions/internalException.ts | 8 +++----- src/exceptions/unauthorizedRequestException.ts | 8 +++----- src/middleware/authenticate.ts | 4 ++-- src/middleware/errorHandler.ts | 14 +++++++++++--- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/exceptions/badRequestException.ts b/src/exceptions/badRequestException.ts index bb2d1c4..7daf842 100644 --- a/src/exceptions/badRequestException.ts +++ b/src/exceptions/badRequestException.ts @@ -1,9 +1,7 @@ -import { ExceptionBase } from "./exceptionsBaseType"; - -export default class BadRequestException extends Error implements ExceptionBase { - statusCode: number = 400; +import CustomRequestException from "./customRequestException"; +export default class BadRequestException extends CustomRequestException { constructor(msg: string) { - super(msg); + super(400, msg); } } diff --git a/src/exceptions/internalException.ts b/src/exceptions/internalException.ts index f5470b1..e7a0773 100644 --- a/src/exceptions/internalException.ts +++ b/src/exceptions/internalException.ts @@ -1,9 +1,7 @@ -import { ExceptionBase } from "./exceptionsBaseType"; - -export default class InternalException extends Error implements ExceptionBase { - statusCode: number = 500; +import CustomRequestException from "./customRequestException"; +export default class InternalException extends CustomRequestException { constructor(msg: string) { - super(msg); + super(500, msg); } } diff --git a/src/exceptions/unauthorizedRequestException.ts b/src/exceptions/unauthorizedRequestException.ts index df1b26f..9313e3e 100644 --- a/src/exceptions/unauthorizedRequestException.ts +++ b/src/exceptions/unauthorizedRequestException.ts @@ -1,9 +1,7 @@ -import { ExceptionBase } from "./exceptionsBaseType"; - -export default class UnauthorizedRequestException extends Error implements ExceptionBase { - statusCode: number = 401; +import CustomRequestException from "./customRequestException"; +export default class UnauthorizedRequestException extends CustomRequestException { constructor(msg: string) { - super(msg); + super(401, msg); } } diff --git a/src/middleware/authenticate.ts b/src/middleware/authenticate.ts index 955ee76..51fff04 100644 --- a/src/middleware/authenticate.ts +++ b/src/middleware/authenticate.ts @@ -6,10 +6,10 @@ import InternalException from "../exceptions/internalException"; import { JWTHelper } from "../helpers/jwtHelper"; export default async function authenticate(req: Request, res: Response, next: Function) { - const bearer = req.headers.authorization; + const bearer = req.headers.authorization?.split(" ")?.[1] ?? undefined; if (!bearer) { - throw new BadRequestException("Provide Authorization Header"); + throw new BadRequestException("Provide valid Authorization Header"); } let decoded: string | jwt.JwtPayload; diff --git a/src/middleware/errorHandler.ts b/src/middleware/errorHandler.ts index 684f9a6..e3ce2ec 100644 --- a/src/middleware/errorHandler.ts +++ b/src/middleware/errorHandler.ts @@ -1,9 +1,17 @@ import { Request, Response } from "express"; import { ExceptionBase } from "../exceptions/exceptionsBaseType"; +import CustomRequestException from "../exceptions/customRequestException"; -export default function errorHandler(err: ExceptionBase, req: Request, res: Response, next: Function) { - let status = err.statusCode ?? 500; - let msg = err.message; +export default function errorHandler(err: ExceptionBase | Error, req: Request, res: Response, next: Function) { + let status = 500; + let msg = "Internal Server Error"; + + if (err instanceof CustomRequestException) { + status = err.statusCode; + msg = err.message; + } + + console.log("Handler", err); res.status(status).send(msg); }