CartoDB-SQL-API/app/services/error_handler_factory.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

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-29 19:19:32 +08:00
message: 'You are over platform\'s limits: SQL query timeout error.' +
' Refactor your query before running again or contact CARTO support for more details.',
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
}