using error handler as a Error

This commit is contained in:
Simon Martín 2018-03-28 11:29:38 +02:00
parent 60021a70dc
commit 2722302a89

View File

@ -1,52 +1,40 @@
var pgErrorCodes = require('./error_codes'); var pgErrorCodes = require('./error_codes');
class ErrorHandler extends Error { class ErrorHandler extends Error {
constructor(err) { constructor(message, http_status, context, detail, hint, name = null) {
super(); super(message);
this.err = err; this.http_status = http_status;
this.context = context;
if (this.isTimeoutError()) { this.detail = detail;
this.err = new Error('You are over platform\'s limits. Please contact us to know more details'); this.hint = hint;
this.err.http_status = 429;
this.err.context = 'limit'; if (name) {
this.err.detail = 'datasource'; this.name = name;
} }
} }
getName () { getResponse () {
return pgErrorCodes.codeToCondition[this.err.code] || this.err.name;
}
getMessage () {
return this.err.message;
}
getFields () {
return { return {
detail: this.err.detail, error: [this.message],
hint: this.err.hint, context: this.context,
context: this.err.context, detail: this.detail,
}; hint: this.hint
}
}
static getName (err) {
return pgErrorCodes.codeToCondition[err.code] || err.name;
} }
getStatus () { static getStatus (err) {
var statusError = this.err.http_status || 400; var statusError = err.http_status || 400;
var message = this.getMessage(); if (err.message && err.message.match(/permission denied/)) {
if (message && message.match(/permission denied/)) {
statusError = 403; statusError = 403;
} }
return statusError; return statusError;
} }
isTimeoutError () {
return this.err.message && (
this.err.message.indexOf('statement timeout') > -1 ||
this.err.message.indexOf('RuntimeError: Execution of function interrupted by signal') > -1
);
}
} }
module.exports = ErrorHandler; module.exports = ErrorHandler;