forked from expressjs/api-error-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (28 loc) · 848 Bytes
/
index.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
var statuses = require('statuses');
var production = process.env.NODE_ENV === 'production';
module.exports = function () {
return function apiErrorHandler(err, req, res, next) {
var status = err.status || err.statusCode || 500;
if (status < 400) status = 500;
res.statusCode = status;
var body = {
status: status
};
// show the stacktrace when not in production
// TODO: make this an option
if (!production) body.stack = err.stack;
// internal server errors
if (status >= 500) {
console.error(err.stack);
body.message = statuses[status];
res.json(body);
return;
}
// client errors
body.message = err.message;
if (err.code) body.code = err.code;
if (err.name) body.name = err.name;
if (err.type) body.type = err.type;
res.json(body);
}
}