lecoffre-back/src/app/middlewares/ErrorHandler.ts
Arnaud D. Natali 3dc043e6c4
Feature/services test (#17)
controllers, repositories, services and associated tests for:

Deed types, Deed, Document types, Users and Customers

---------

Co-authored-by: Vincent Alamelle <vincent.alamelle@smart-chain.fr>
Co-authored-by: OxSaitama <arnaud.daubernatali@smart-chain.fr>
2023-04-06 18:43:13 +02:00

25 lines
750 B
TypeScript

import HttpException from "@Common/system/controller-pattern/exceptions/HttpException";
import HttpCodes from "@Common/system/controller-pattern/HttpCodes";
import { NextFunction, Request, Response } from "express";
export default function errorHandler(error: any, req: Request, response: Response, next: NextFunction) {
const errorStatus: number = error["status"];
/**
* Used on when try to parse json on request
*/
if (error instanceof SyntaxError && errorStatus === 400 && "body" in error) {
response.status(HttpCodes.BAD_REQUEST).send({
body: error["body"],
type: error as any["type"],
});
return;
}
if (error instanceof HttpException) {
response.status(error.httpCode).send(error.message);
return;
}
next(error);
}