-
Notifications
You must be signed in to change notification settings - Fork 4
/
ErrorHandler.js
34 lines (27 loc) · 1.19 KB
/
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
32
33
34
const expressValidation = require('express-validation');
const log = require('./config/logger');
const errors = require('./errors');
const moment = require('moment');
module.exports = (app) => {
const error_code = {
INVALID_PARAMETER: 9401,
SERVER_ERROR: 500
};
app.use((err, req, res, next) => {
// 에러 로그
log.error(` : ${req.path}] ` + " [ Status Code: " + err + " : " + errors[err].description + " ] [ Time : " + moment().format('YYYY.MM.DD h:mm:ss a ]') );
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
);
});
};