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);
}
};
2019-12-24 01:19:08 +08:00
function isTimeoutError (err) {
2018-03-28 19:06:39 +08:00
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
);
}
2019-12-24 01:19:08 +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',
2019-12-27 00:46:27 +08:00
httpStatus: 429
2018-04-03 19:43:17 +08:00
});
2018-03-28 19:06:39 +08:00
}
2019-12-24 01:19:08 +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,
2019-12-27 00:46:27 +08:00
httpStatus: err.http_status,
2018-04-03 19:43:17 +08:00
name: codeToCondition[err.code] || err.name
});
2018-03-28 19:06:39 +08:00
}