-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.js
36 lines (32 loc) · 825 Bytes
/
errors.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
const customErrorHandler = (err, req, res, next) => {
if (err.status !== undefined) {
res.status(err.status).send({ msg: err.msg });
} else {
next(err);
}
};
const mongoErrorHandling = (err, req, res, next) => {
if (err.code !== undefined) {
const mongoErrors = {
11000: {
status: 400,
msg: "Username already exists!",
},
};
res
.status(mongoErrors[err.code].status)
.send({ msg: mongoErrors[err.code].msg });
} else next(err);
};
const serverErrorHandler = (err, req, res, next) => {
res.status(500).send({ msg: "Internal server error" });
};
const send405Error = (req, res, next) => {
res.status(405).send({ msg: "Method not allowed" });
};
module.exports = {
customErrorHandler,
mongoErrorHandling,
serverErrorHandler,
send405Error,
};