CartoDB-SQL-API/app/services/error_handler.js

35 lines
736 B
JavaScript
Raw Normal View History

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
}
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 16:48:25 +08:00
}
2018-03-28 16:48:25 +08:00
module.exports = ErrorHandler;