forked from Ovi/DummyJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.js
40 lines (31 loc) · 898 Bytes
/
error.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
const errorMiddleware = (err, req, res, next) => {
if (!err) {
next();
return;
}
if (err.status === 404) {
console.log('*** 404 Error ***', err.message || err);
} else {
console.log('*-*-* [start] error *-*-*');
console.log(err.message || err);
console.log('*-*-* [end] error *-*-*');
}
if (err.message === 'jwt expired') {
return res.status(401).send({ ...err, message: 'Token Expired!' });
}
if (err.message === 'jwt malformed') {
return res.status(401).send({ ...err, message: 'Invalid/Expired Token!' });
}
if (err.message === 'Invalid Token') {
return res.status(401).send({ ...err, message: 'Invalid Token!' });
}
const error = {
message: err.message,
};
res.status(err.status || 500);
if (!error.message) {
error.message = `Something went wrong.`;
}
res.json(error);
};
module.exports = errorMiddleware;