2014-11-11 20:57:15 +08:00
|
|
|
var pgErrorCodes = require('./error_codes');
|
|
|
|
|
2018-03-28 16:56:06 +08:00
|
|
|
class ErrorHandler extends Error {
|
2018-03-28 18:35:01 +08:00
|
|
|
constructor(message, context, detail, hint, http_status = 400, name = null) {
|
2018-03-28 17:29:38 +08:00
|
|
|
super(message);
|
2018-03-28 18:35:01 +08:00
|
|
|
|
|
|
|
this.http_status = this.getHttpStatus(http_status);
|
2018-03-28 17:29:38 +08:00
|
|
|
this.context = context;
|
|
|
|
this.detail = detail;
|
|
|
|
this.hint = hint;
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
this.name = name;
|
2018-03-28 16:48:25 +08:00
|
|
|
}
|
2018-03-24 00:48:21 +08:00
|
|
|
}
|
2014-11-11 20:57:15 +08:00
|
|
|
|
2018-03-28 17:29:38 +08:00
|
|
|
getResponse () {
|
2018-03-28 16:48:25 +08:00
|
|
|
return {
|
2018-03-28 17:29:38 +08:00
|
|
|
error: [this.message],
|
|
|
|
context: this.context,
|
|
|
|
detail: this.detail,
|
|
|
|
hint: this.hint
|
2018-03-28 18:17:42 +08:00
|
|
|
};
|
2018-03-28 17:29:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static getName (err) {
|
|
|
|
return pgErrorCodes.codeToCondition[err.code] || err.name;
|
2018-03-28 16:49:50 +08:00
|
|
|
}
|
2018-03-28 16:48:25 +08:00
|
|
|
|
2018-03-28 18:35:01 +08:00
|
|
|
getHttpStatus (http_status) {
|
|
|
|
if (this.message.includes('permission denied')) {
|
|
|
|
return 403;
|
2018-03-28 16:48:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-28 18:35:01 +08:00
|
|
|
return http_status;
|
2018-03-28 16:49:50 +08:00
|
|
|
}
|
2018-03-28 16:48:25 +08:00
|
|
|
}
|
2014-11-11 20:57:15 +08:00
|
|
|
|
2018-03-28 16:48:25 +08:00
|
|
|
module.exports = ErrorHandler;
|