-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (38 loc) · 946 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
38
39
40
const Boom = require('boom');
module.exports = ({
env = 'production',
errStatusCodePropertie = ['status', 'statusCode', 'code']
} = {}) => async (ctx, next) => {
try {
await next();
if (ctx.status === 404) {
throw Boom.notFound('API Not Found');
}
const response = ctx.body;
ctx.body = {
success: true,
data: response
};
} catch (err) {
if (!Boom.isBoom(err)) {
let statusCode = 500;
for (let property of errStatusCodePropertie) {
if (Number.isInteger(err[property])) {
statusCode = ~~err[property];
}
}
err = Boom.boomify(err, {
statusCode: statusCode
});
}
const payload = err.output.payload;
ctx.status = payload.statusCode;
ctx.body = {
success: false,
error: payload.error,
message:
(env !== 'production' ? err.message : payload.message) ||
payload.message
};
}
};