-
Notifications
You must be signed in to change notification settings - Fork 1
/
ErrorHandler.js
31 lines (26 loc) · 1023 Bytes
/
ErrorHandler.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
const expressValidation = require('express-validation');
const log = require('./config/logger');
const errors = require('./errors');
module.exports = (app) => {
const error_code = {
INVALID_PARAMETER: 9401,
SERVER_ERROR: 500
};
app.use((err, req, res, next) => {
// 에러 로그
log.error(`\n\x1b[31m[ERROR Handler] \u001b[0m \n\x1b[34m[Request PATH - ${req.path}] \u001b[0m \n`, err);
let miss_param = false;
if (err instanceof expressValidation.ValidationError) { // 잘못된 파라미터 확인
miss_param = err.errors.map(error => error.messages.join('. ')).join('\n');
console.log(`\n\x1b[36m[Miss Params] \u001b[0m \n${miss_param}`);
err = error_code.INVALID_PARAMETER;
} else if (isNaN(err)) { // 서버쪽 에러
err = error_code.SERVER_ERROR;
}
const response_error = errors[err];
response_error.miss_param = miss_param ? miss_param : undefined;
return res.status(response_error.status).json([
response_error
]);
});
};