CartoDB-SQL-API/app/middlewares/error.js

124 lines
3.2 KiB
JavaScript
Raw Normal View History

var PgErrorHandler = require('../postgresql/error_handler');
2018-03-28 18:38:45 +08:00
var pgErrorCodes = require('../postgresql/error_codes');
2018-03-28 16:48:08 +08:00
module.exports = function errorMiddleware() {
return function error(err, req, res, next) {
2018-03-28 18:13:31 +08:00
let pgErrorHandler = getErrorHandler(err);
2018-03-28 17:30:11 +08:00
var msg = pgErrorHandler.getResponse();
if (global.settings.environment === 'development') {
msg.stack = err.stack;
}
2018-03-28 16:48:08 +08:00
if (global.settings.environment !== 'test') {
// TODO: email this Exception report
console.error("EXCEPTION REPORT: " + err.stack);
}
// Force inline content disposition
res.header("Content-Disposition", 'inline');
2018-03-28 16:48:08 +08:00
if (req && req.profiler) {
req.profiler.done('finish');
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
}
2018-03-28 17:30:11 +08:00
setErrorHeader(msg, pgErrorHandler.http_status, res);
res.header('Content-Type', 'application/json; charset=utf-8');
res.status(getStatusError(pgErrorHandler, req));
if (req.query && req.query.callback) {
res.jsonp(msg);
} else {
res.json(msg);
}
if (req && req.profiler) {
res.req.profiler.sendStats();
}
next();
};
};
function getStatusError(pgErrorHandler, req) {
2018-03-28 17:30:11 +08:00
var statusError = pgErrorHandler.http_status;
// JSONP has to return 200 status error
if (req && req.query && req.query.callback) {
statusError = 200;
}
return statusError;
}
function setErrorHeader(err, statusCode, res) {
let errorsLog = Object.assign({}, err);
errorsLog.statusCode = statusCode || 200;
errorsLog.message = errorsLog.error[0];
delete errorsLog.error;
res.set('X-SQLAPI-Errors', stringifyForLogs(errorsLog));
}
/**
* Remove problematic nested characters
* from object for logs RegEx
*
* @param {Object} object
*/
function stringifyForLogs(object) {
Object.keys(object).map(key => {
2018-03-28 16:48:08 +08:00
if (typeof object[key] === 'string') {
object[key] = object[key].replace(/[^a-zA-Z0-9]/g, ' ');
} else if (typeof object[key] === 'object') {
stringifyForLogs(object[key]);
} else if (object[key] instanceof Array) {
for (let element of object[key]) {
stringifyForLogs(element);
}
}
});
return JSON.stringify(object);
}
2018-03-28 17:30:11 +08:00
2018-03-28 18:13:31 +08:00
function getErrorHandler (err) {
if (isTimeoutError(err)) {
return createTimeoutError();
} else {
return createPgError(err);
}
}
2018-03-28 17:30:11 +08:00
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() {
return new PgErrorHandler(
'You are over platform\'s limits. Please contact us to know more details',
'limit',
2018-03-28 18:35:01 +08:00
'datasource',
undefined,
429
2018-03-28 17:30:11 +08:00
);
}
function createPgError(err) {
return new PgErrorHandler(
err.message,
err.context,
err.detail,
err.hint,
2018-03-28 18:35:01 +08:00
err.http_status,
2018-03-28 18:38:45 +08:00
pgErrorCodes.codeToCondition[err.code] || err.name
2018-03-28 17:30:11 +08:00
);
}