2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-03-28 16:56:06 +08:00
|
|
|
class ErrorHandler extends Error {
|
2019-12-27 00:46:27 +08:00
|
|
|
constructor ({ message, context, detail, hint, httpStatus, name }) {
|
2018-03-28 17:29:38 +08:00
|
|
|
super(message);
|
2018-03-28 18:35:01 +08:00
|
|
|
|
2019-12-27 00:46:27 +08:00
|
|
|
this.http_status = this.getHttpStatus(httpStatus);
|
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
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
getResponse () {
|
2020-06-30 23:42:59 +08:00
|
|
|
const serialized = {
|
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
|
|
|
};
|
2020-06-30 23:42:59 +08:00
|
|
|
|
|
|
|
if (global.settings.environment === 'development') {
|
|
|
|
serialized.stack = this.stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
return serialized;
|
2018-03-28 17:29:38 +08:00
|
|
|
}
|
2018-04-03 19:43:17 +08:00
|
|
|
|
2019-12-27 00:46:27 +08:00
|
|
|
getHttpStatus (httpStatus = 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
|
|
|
|
2019-12-27 00:46:27 +08:00
|
|
|
return httpStatus;
|
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;
|