-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.middleware.ts
51 lines (49 loc) · 1.96 KB
/
error.middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Request, Response, NextFunction } from "express";
import {
HttpBadRequestError,
HttpForbiddenError,
HttpNotFoundError,
HttpUnauthorizedError,
} from "../errors/http.error.js";
import {
PrismaClientInitializationError,
PrismaClientRustPanicError,
PrismaClientUnknownRequestError,
PrismaClientValidationError,
PrismaGenericError,
} from "../errors/prisma.error.js";
import { ProductNotFoundError } from "../errors/product.error.js";
import { TesseractServiceError } from "../errors/tesseract.error.js";
const errorHandler = (
e: Error,
_: Request,
res: Response,
next: NextFunction
) => {
if (e instanceof HttpBadRequestError) {
res.status(400).json({ error: "Bad Request Error" });
} else if (e instanceof HttpUnauthorizedError) {
res.status(401).json({ error: "Unauthorized Error" });
} else if (e instanceof HttpForbiddenError) {
res.status(403).json({ error: "Forbidden Error" });
} else if (e instanceof HttpNotFoundError) {
res.status(404).json({ error: "Not Found Error" });
} else if (e instanceof PrismaClientUnknownRequestError) {
res.status(500).json({ error: "Prisma Client Unknown Request Error" });
} else if (e instanceof PrismaClientRustPanicError) {
res.status(500).json({ error: "Prisma Client Rust Panic Error" });
} else if (e instanceof PrismaClientInitializationError) {
res.status(500).json({ error: "Prisma Client Initialization Error" });
} else if (e instanceof PrismaClientValidationError) {
res.status(500).json({ error: "Prisma Client Validation Error" });
} else if (e instanceof PrismaGenericError) {
res.status(500).json({ error: "Prisma Generic Error" });
} else if (e instanceof ProductNotFoundError) {
res.status(500).json({ error: "Product Not Found Error" });
} else if (e instanceof TesseractServiceError) {
res.status(500).json({ error: "Tesseract Service Error" });
} else {
res.status(500).json({ error: "Unexpected error" });
}
};
export default errorHandler;