improve error handling
This commit is contained in:
parent
e1ec65350d
commit
bcfba8b448
5 changed files with 22 additions and 20 deletions
|
@ -1,9 +1,7 @@
|
||||||
import { ExceptionBase } from "./exceptionsBaseType";
|
import CustomRequestException from "./customRequestException";
|
||||||
|
|
||||||
export default class BadRequestException extends Error implements ExceptionBase {
|
|
||||||
statusCode: number = 400;
|
|
||||||
|
|
||||||
|
export default class BadRequestException extends CustomRequestException {
|
||||||
constructor(msg: string) {
|
constructor(msg: string) {
|
||||||
super(msg);
|
super(400, msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
import { ExceptionBase } from "./exceptionsBaseType";
|
import CustomRequestException from "./customRequestException";
|
||||||
|
|
||||||
export default class InternalException extends Error implements ExceptionBase {
|
|
||||||
statusCode: number = 500;
|
|
||||||
|
|
||||||
|
export default class InternalException extends CustomRequestException {
|
||||||
constructor(msg: string) {
|
constructor(msg: string) {
|
||||||
super(msg);
|
super(500, msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
import { ExceptionBase } from "./exceptionsBaseType";
|
import CustomRequestException from "./customRequestException";
|
||||||
|
|
||||||
export default class UnauthorizedRequestException extends Error implements ExceptionBase {
|
|
||||||
statusCode: number = 401;
|
|
||||||
|
|
||||||
|
export default class UnauthorizedRequestException extends CustomRequestException {
|
||||||
constructor(msg: string) {
|
constructor(msg: string) {
|
||||||
super(msg);
|
super(401, msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,10 @@ import InternalException from "../exceptions/internalException";
|
||||||
import { JWTHelper } from "../helpers/jwtHelper";
|
import { JWTHelper } from "../helpers/jwtHelper";
|
||||||
|
|
||||||
export default async function authenticate(req: Request, res: Response, next: Function) {
|
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) {
|
if (!bearer) {
|
||||||
throw new BadRequestException("Provide Authorization Header");
|
throw new BadRequestException("Provide valid Authorization Header");
|
||||||
}
|
}
|
||||||
|
|
||||||
let decoded: string | jwt.JwtPayload;
|
let decoded: string | jwt.JwtPayload;
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { ExceptionBase } from "../exceptions/exceptionsBaseType";
|
import { ExceptionBase } from "../exceptions/exceptionsBaseType";
|
||||||
|
import CustomRequestException from "../exceptions/customRequestException";
|
||||||
|
|
||||||
export default function errorHandler(err: ExceptionBase, req: Request, res: Response, next: Function) {
|
export default function errorHandler(err: ExceptionBase | Error, req: Request, res: Response, next: Function) {
|
||||||
let status = err.statusCode ?? 500;
|
let status = 500;
|
||||||
let msg = err.message;
|
let msg = "Internal Server Error";
|
||||||
|
|
||||||
|
if (err instanceof CustomRequestException) {
|
||||||
|
status = err.statusCode;
|
||||||
|
msg = err.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Handler", err);
|
||||||
|
|
||||||
res.status(status).send(msg);
|
res.status(status).send(msg);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue