CartoDB-SQL-API/lib/services/error-handler-factory.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
const ErrorHandler = require('./error-handler');
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 ||
err.message.indexOf('canceling statement due to user request') > -1
2018-03-28 19:06:39 +08:00
);
}
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
}