2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-03-28 16:56:06 +08:00
|
|
|
class ErrorHandler extends Error {
|
2018-04-03 19:43:17 +08:00
|
|
|
constructor({ message, context, detail, hint, http_status, name }) {
|
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-04-03 19:43:17 +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
|
|
|
}
|
2018-04-03 19:43:17 +08:00
|
|
|
|
|
|
|
getHttpStatus(http_status = 400) {
|
2018-03-28 18:35:01 +08:00
|
|
|
if (this.message.includes('permission denied')) {
|
|
|
|
return 403;
|
2018-03-28 16:48:25 +08:00
|
|
|
}
|
2018-04-03 19:43:17 +08:00
|
|
|
|
2018-03-28 18:35:01 +08:00
|
|
|
return http_status;
|
2018-03-28 16:49:50 +08:00
|
|
|
}
|
2018-03-28 18:58:10 +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;
|