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 16:48:25 +08:00
|
|
|
constructor(err) {
|
2018-03-28 16:56:06 +08:00
|
|
|
super();
|
2018-03-28 16:48:25 +08:00
|
|
|
this.err = err;
|
|
|
|
|
|
|
|
if (this.isTimeoutError()) {
|
|
|
|
this.err = new Error('You are over platform\'s limits. Please contact us to know more details');
|
|
|
|
this.err.http_status = 429;
|
|
|
|
this.err.context = 'limit';
|
|
|
|
this.err.detail = 'datasource';
|
|
|
|
}
|
2018-03-24 00:48:21 +08:00
|
|
|
}
|
2014-11-11 20:57:15 +08:00
|
|
|
|
2018-03-28 16:48:25 +08:00
|
|
|
getName () {
|
|
|
|
return pgErrorCodes.codeToCondition[this.err.code] || this.err.name;
|
2018-03-28 16:49:50 +08:00
|
|
|
}
|
2018-03-28 16:48:25 +08:00
|
|
|
|
|
|
|
getMessage () {
|
|
|
|
return this.err.message;
|
2018-03-28 16:49:50 +08:00
|
|
|
}
|
2018-03-28 16:48:25 +08:00
|
|
|
|
|
|
|
getFields () {
|
|
|
|
return {
|
|
|
|
detail: this.err.detail,
|
|
|
|
hint: this.err.hint,
|
|
|
|
context: this.err.context,
|
|
|
|
};
|
2018-03-28 16:49:50 +08:00
|
|
|
}
|
2018-03-28 16:48:25 +08:00
|
|
|
|
|
|
|
getStatus () {
|
|
|
|
var statusError = this.err.http_status || 400;
|
|
|
|
|
|
|
|
var message = this.getMessage();
|
|
|
|
|
|
|
|
if (message && message.match(/permission denied/)) {
|
|
|
|
statusError = 403;
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusError;
|
2018-03-28 16:49:50 +08:00
|
|
|
}
|
2018-03-28 16:48:25 +08:00
|
|
|
|
|
|
|
isTimeoutError () {
|
|
|
|
return this.err.message && (
|
|
|
|
this.err.message.indexOf('statement timeout') > -1 ||
|
|
|
|
this.err.message.indexOf('RuntimeError: Execution of function interrupted by signal') > -1
|
|
|
|
);
|
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;
|