-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandleErrors.js
60 lines (52 loc) · 1.46 KB
/
handleErrors.js
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
52
53
54
55
56
57
58
59
60
/* eslint-disable no-unused-vars */
const {
PrismaClientKnownRequestError,
PrismaClientValidationError,
} = require("@prisma/client/runtime");
const handleErrors = (err, req, res, next) => {
// Log the error for debugging purposes
console.error(err);
// Handle known Prisma client request errors
if (err instanceof PrismaClientKnownRequestError) {
return res.status(400).json({
status: "error",
message: "Bad request - Prisma error",
details: err.message,
});
}
// Handle Prisma client validation errors
if (err instanceof PrismaClientValidationError) {
return res.status(400).json({
status: "error",
message: "Validation error",
details: err.message,
});
}
// Handle SyntaxError (Invalid JSON)
if (err.name === "SyntaxError") {
return res.status(400).json({
status: "error",
message: "Bad request - Invalid JSON",
});
}
// Handle MySQL database connection errors
if (err.code === "PROTOCOL_CONNECTION_LOST") {
return res.status(500).json({
status: "error",
message: "Database connection lost",
});
}
// Handle MySQL query execution errors
if (err.code === "ER_PARSE_ERROR") {
return res.status(500).json({
status: "error",
message: "Database query parse error",
});
}
// Handle other unhandled errors
return res.status(500).json({
status: "error",
message: "Internal server error",
});
};
module.exports = handleErrors;