2018-03-28 19:06:39 +08:00
|
|
|
const ErrorHandler = require('./error_handler');
|
2018-03-28 19:31:31 +08:00
|
|
|
const { codeToCondition } = require('../postgresql/error_codes');
|
2018-03-28 19:06:39 +08:00
|
|
|
|
2018-03-28 19:31:31 +08:00
|
|
|
module.exports = function ErrorHandlerFactory (err) {
|
2018-03-28 19:06:39 +08:00
|
|
|
if (isTimeoutError(err)) {
|
|
|
|
return createTimeoutError();
|
|
|
|
} else {
|
|
|
|
return createGenericError(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function isTimeoutError(err) {
|
|
|
|
return err.message && (
|
|
|
|
err.message.indexOf('statement timeout') > -1 ||
|
|
|
|
err.message.indexOf('RuntimeError: Execution of function interrupted by signal') > -1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createTimeoutError() {
|
2018-04-03 19:43:17 +08:00
|
|
|
return new ErrorHandler({
|
2018-06-27 21:53:51 +08:00
|
|
|
message: 'You are over platform\'s limits. Please contact us to know more details. ' +
|
2018-06-27 21:43:06 +08:00
|
|
|
'SQL query timeout expired error.',
|
2018-04-03 19:43:17 +08:00
|
|
|
context: 'limit',
|
|
|
|
detail: 'datasource',
|
|
|
|
http_status: 429
|
|
|
|
});
|
2018-03-28 19:06:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createGenericError(err) {
|
2018-04-03 19:43:17 +08:00
|
|
|
return new ErrorHandler({
|
|
|
|
message: err.message,
|
|
|
|
context: err.context,
|
|
|
|
detail: err.detail,
|
|
|
|
hint: err.hint,
|
|
|
|
http_status: err.http_status,
|
|
|
|
name: codeToCondition[err.code] || err.name
|
|
|
|
});
|
2018-03-28 19:06:39 +08:00
|
|
|
}
|