2014-11-11 20:57:15 +08:00
|
|
|
var pgErrorCodes = require('./error_codes');
|
|
|
|
|
|
|
|
function ErrorHandler(err) {
|
|
|
|
this.err = err;
|
2018-03-24 00:48:21 +08:00
|
|
|
|
2018-03-24 01:02:44 +08:00
|
|
|
if (this.isTimeoutError()) {
|
2018-03-24 00:48:21 +08:00
|
|
|
this.err = new Error('You are over platform\'s limits. Please contact us to know more details');
|
|
|
|
this.err.http_status = 429;
|
2018-03-27 01:10:23 +08:00
|
|
|
this.err.context = 'limit';
|
|
|
|
this.err.detail = 'datasource';
|
2018-03-24 00:48:21 +08:00
|
|
|
}
|
2014-11-11 20:57:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ErrorHandler;
|
|
|
|
|
|
|
|
ErrorHandler.prototype.getName = function() {
|
|
|
|
return pgErrorCodes.codeToCondition[this.err.code] || this.err.name;
|
|
|
|
};
|
|
|
|
|
|
|
|
ErrorHandler.prototype.getMessage = function() {
|
2018-03-24 00:48:21 +08:00
|
|
|
return this.err.message;
|
2014-11-11 20:57:15 +08:00
|
|
|
};
|
|
|
|
|
2018-03-24 01:02:44 +08:00
|
|
|
ErrorHandler.prototype.getFields = function() {
|
|
|
|
return ['detail', 'hint', 'context'].reduce(function (previousValue, current) {
|
2014-11-21 19:59:48 +08:00
|
|
|
previousValue[current] = this.err[current];
|
|
|
|
return previousValue;
|
|
|
|
}.bind(this), {});
|
|
|
|
};
|
|
|
|
|
2014-11-11 20:57:15 +08:00
|
|
|
ErrorHandler.prototype.getStatus = function() {
|
|
|
|
var statusError = this.err.http_status || 400;
|
|
|
|
|
|
|
|
var message = this.getMessage();
|
|
|
|
|
|
|
|
if (message && message.match(/permission denied/)) {
|
2018-02-23 22:50:23 +08:00
|
|
|
statusError = 403;
|
2014-11-11 20:57:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return statusError;
|
|
|
|
};
|
|
|
|
|
2018-01-10 00:14:11 +08:00
|
|
|
ErrorHandler.prototype.isTimeoutError = function() {
|
|
|
|
return this.err.message && (
|
2018-02-23 22:50:23 +08:00
|
|
|
this.err.message.indexOf('statement timeout') > -1 ||
|
2018-01-10 00:14:11 +08:00
|
|
|
this.err.message.indexOf('RuntimeError: Execution of function interrupted by signal') > -1
|
|
|
|
);
|
|
|
|
};
|