2014-11-11 20:57:15 +08:00
|
|
|
var pgErrorCodes = require('./error_codes');
|
|
|
|
|
|
|
|
function ErrorHandler(err) {
|
|
|
|
this.err = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ErrorHandler;
|
|
|
|
|
|
|
|
ErrorHandler.prototype.getName = function() {
|
|
|
|
return pgErrorCodes.codeToCondition[this.err.code] || this.err.name;
|
|
|
|
};
|
|
|
|
|
|
|
|
ErrorHandler.prototype.getMessage = function() {
|
|
|
|
var message = this.err.message;
|
|
|
|
|
2018-01-10 00:14:11 +08:00
|
|
|
if (this.isTimeoutError()) {
|
2014-11-11 20:57:15 +08:00
|
|
|
message = conditionToMessage[pgErrorCodes.conditionToCode.query_canceled];
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2014-11-21 19:59:48 +08:00
|
|
|
ErrorHandler.prototype.getFields = function(fields) {
|
|
|
|
fields = fields || ['detail', 'hint', 'context'];
|
|
|
|
return fields.reduce(function (previousValue, current) {
|
|
|
|
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/)) {
|
|
|
|
statusError = 401;
|
|
|
|
}
|
|
|
|
|
2017-08-03 23:19:08 +08:00
|
|
|
if (message === conditionToMessage[pgErrorCodes.conditionToCode.query_canceled]) {
|
|
|
|
statusError = 429;
|
|
|
|
}
|
|
|
|
|
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-01-10 00:16:38 +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
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-11-11 20:57:15 +08:00
|
|
|
var conditionToMessage = {};
|
|
|
|
conditionToMessage[pgErrorCodes.conditionToCode.query_canceled] = [
|
2017-08-03 23:19:08 +08:00
|
|
|
'You are over platform\'s limits. Please contact us to know more details'
|
2014-11-11 20:57:15 +08:00
|
|
|
].join(' ');
|