-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorHandlers.js
34 lines (30 loc) · 921 Bytes
/
errorHandlers.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
// POSTGRES ERROR
const postgresErr = (err, req, res, next) => {
if (err.code === "22P02") {
res.status(400).send({ msg: "Bad Request" }); // INVALID TEXT REPRESENTATION
} else if (err.code === "23502") {
res.status(400).send({ msg: "Bad Request" }); // NOT NULL VIOLATION
} else if (err.code === "23503") {
res.status(404).send({ msg: "Not Found" }); // FORIEGN KEY VIOLATION
} else {
next(err);
}
};
//CUSTOM ERROR HANDLER
const customErr = (err, req, res, next) => {
if (err.status) {
res.status(err.status).send({ msg: err.msg });
} else {
next(err);
}
};
// INTERNAL SEVER ERROR
const internalErr = (err, req, res, next) => {
console.log(err);
res.status(500).send({ msg: "Internal Server Error" });
};
// CATCH ALL
const catchAll = (req, res) => {
res.status(404).send({ msg: "Bad Request" });
};
module.exports = { postgresErr, customErr, internalErr, catchAll };