2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
const _ = require('underscore');
|
|
|
|
const debug = require('debug')('windshaft:cartodb:error-middleware');
|
|
|
|
|
|
|
|
module.exports = function errorMiddleware (/* options */) {
|
2017-09-22 06:31:16 +08:00
|
|
|
return function error (err, req, res, next) {
|
2017-09-21 17:46:31 +08:00
|
|
|
// jshint unused:false
|
|
|
|
// jshint maxcomplexity:9
|
|
|
|
var allErrors = Array.isArray(err) ? err : [err];
|
|
|
|
|
2019-01-30 17:27:00 +08:00
|
|
|
allErrors = populateLimitErrors(allErrors);
|
2017-09-21 17:46:31 +08:00
|
|
|
|
|
|
|
const label = err.label || 'UNKNOWN';
|
|
|
|
err = allErrors[0] || new Error(label);
|
|
|
|
allErrors[0] = err;
|
|
|
|
|
|
|
|
var statusCode = findStatusCode(err);
|
|
|
|
|
2017-11-30 22:04:07 +08:00
|
|
|
setErrorHeader(allErrors, statusCode, res);
|
2017-09-21 17:46:31 +08:00
|
|
|
debug('[%s ERROR] -- %d: %s, %s', label, statusCode, err, err.stack);
|
|
|
|
|
|
|
|
// If a callback was requested, force status to 200
|
|
|
|
if (req.query && req.query.callback) {
|
|
|
|
statusCode = 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
var errorResponseBody = {
|
|
|
|
errors: allErrors.map(errorMessage),
|
|
|
|
errors_with_context: allErrors.map(errorMessageWithContext)
|
|
|
|
};
|
|
|
|
|
|
|
|
res.status(statusCode);
|
|
|
|
|
|
|
|
if (req.query && req.query.callback) {
|
|
|
|
res.jsonp(errorResponseBody);
|
|
|
|
} else {
|
|
|
|
res.json(errorResponseBody);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function isRenderTimeoutError (err) {
|
|
|
|
return err.message === 'Render timed out';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDatasourceTimeoutError (err) {
|
|
|
|
return err.message && err.message.match(/canceling statement due to statement timeout/i);
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:32:10 +08:00
|
|
|
function isTimeoutError (errorTypes) {
|
|
|
|
return errorTypes.renderTimeoutError || errorTypes.datasourceTimeoutError;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getErrorTypes (error) {
|
2018-07-03 20:32:10 +08:00
|
|
|
return {
|
|
|
|
renderTimeoutError: isRenderTimeoutError(error),
|
2019-10-22 01:07:24 +08:00
|
|
|
datasourceTimeoutError: isDatasourceTimeoutError(error)
|
2018-07-03 20:32:10 +08:00
|
|
|
};
|
2017-09-21 17:46:31 +08:00
|
|
|
}
|
|
|
|
|
2019-01-30 17:27:00 +08:00
|
|
|
function isMaxWaitingClientsError (err) {
|
|
|
|
return err.message === 'max waitingClients count exceeded';
|
|
|
|
}
|
|
|
|
|
|
|
|
function populateLimitErrors (errors) {
|
2017-09-21 17:46:31 +08:00
|
|
|
return errors.map(function (error) {
|
2019-01-30 17:27:00 +08:00
|
|
|
if (isMaxWaitingClientsError(error)) {
|
2019-01-30 23:36:08 +08:00
|
|
|
error.message = 'You are over platform\'s limits: Max render capacity exceeded.' +
|
2019-01-30 17:27:00 +08:00
|
|
|
' Contact CARTO support for more details.';
|
|
|
|
error.type = 'limit';
|
2019-01-30 23:36:08 +08:00
|
|
|
error.subtype = 'render-capacity';
|
2019-01-30 17:27:00 +08:00
|
|
|
error.http_status = 429;
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:32:10 +08:00
|
|
|
const errorTypes = getErrorTypes(error);
|
2017-09-21 17:46:31 +08:00
|
|
|
|
2018-07-03 20:32:10 +08:00
|
|
|
if (isTimeoutError(errorTypes)) {
|
2017-09-21 17:46:31 +08:00
|
|
|
error.message = 'You are over platform\'s limits. Please contact us to know more details';
|
|
|
|
error.type = 'limit';
|
|
|
|
error.http_status = 429;
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:32:10 +08:00
|
|
|
if (errorTypes.datasourceTimeoutError) {
|
2018-06-29 21:01:12 +08:00
|
|
|
error.subtype = 'datasource';
|
|
|
|
error.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-07-03 20:32:10 +08:00
|
|
|
if (errorTypes.renderTimeoutError) {
|
|
|
|
error.subtype = 'render';
|
|
|
|
error.message = 'You are over platform\'s limits: Render timeout error.' +
|
|
|
|
' Contact CARTO support for more details.';
|
|
|
|
}
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
return error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function findStatusCode (err) {
|
2017-09-21 17:46:31 +08:00
|
|
|
var statusCode;
|
2019-10-22 01:07:24 +08:00
|
|
|
if (err.http_status) {
|
2017-09-21 17:46:31 +08:00
|
|
|
statusCode = err.http_status;
|
|
|
|
} else {
|
|
|
|
statusCode = statusFromErrorMessage('' + err);
|
|
|
|
}
|
|
|
|
return statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.findStatusCode = findStatusCode;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function statusFromErrorMessage (errMsg) {
|
2017-09-21 17:46:31 +08:00
|
|
|
// Find an appropriate statusCode based on message
|
|
|
|
// jshint maxcomplexity:7
|
|
|
|
var statusCode = 400;
|
2019-10-22 01:07:24 +08:00
|
|
|
if (errMsg.indexOf('permission denied') !== -1) {
|
2017-09-21 17:46:31 +08:00
|
|
|
statusCode = 403;
|
2019-10-22 01:07:24 +08:00
|
|
|
} else if (errMsg.indexOf('authentication failed') !== -1) {
|
2017-09-21 17:46:31 +08:00
|
|
|
statusCode = 403;
|
2019-10-22 01:07:24 +08:00
|
|
|
} else if (errMsg.match(/Postgis Plugin.*[\s|\n].*column.*does not exist/)) {
|
2017-09-21 17:46:31 +08:00
|
|
|
statusCode = 400;
|
2019-10-22 01:07:24 +08:00
|
|
|
} else if (errMsg.indexOf('does not exist') !== -1) {
|
|
|
|
if (errMsg.indexOf(' role ') !== -1) {
|
2017-09-21 17:46:31 +08:00
|
|
|
statusCode = 403; // role 'xxx' does not exist
|
2019-10-22 01:07:24 +08:00
|
|
|
} else if (errMsg.match(/function .* does not exist/)) {
|
2017-09-21 17:46:31 +08:00
|
|
|
statusCode = 400; // invalid SQL (SQL function does not exist)
|
|
|
|
} else {
|
|
|
|
statusCode = 404;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusCode;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function errorMessage (err) {
|
2017-09-21 17:46:31 +08:00
|
|
|
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/68
|
|
|
|
var message = (_.isString(err) ? err : err.message) || 'Unknown error';
|
|
|
|
|
|
|
|
return stripConnectionInfo(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.errorMessage = errorMessage;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function stripConnectionInfo (message) {
|
2017-09-21 17:46:31 +08:00
|
|
|
// Strip connection info, if any
|
|
|
|
return message
|
|
|
|
// See https://github.com/CartoDB/Windshaft/issues/173
|
|
|
|
.replace(/Connection string: '[^']*'\n\s/im, '')
|
|
|
|
// See https://travis-ci.org/CartoDB/Windshaft/jobs/20703062#L1644
|
|
|
|
.replace(/is the server.*encountered/im, 'encountered');
|
|
|
|
}
|
|
|
|
|
|
|
|
var ERROR_INFO_TO_EXPOSE = {
|
|
|
|
message: true,
|
|
|
|
layer: true,
|
|
|
|
type: true,
|
|
|
|
analysis: true,
|
|
|
|
subtype: true
|
|
|
|
};
|
|
|
|
|
|
|
|
function shouldBeExposed (prop) {
|
|
|
|
return !!ERROR_INFO_TO_EXPOSE[prop];
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function errorMessageWithContext (err) {
|
2017-09-21 17:46:31 +08:00
|
|
|
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/68
|
|
|
|
var message = (_.isString(err) ? err : err.message) || 'Unknown error';
|
|
|
|
|
|
|
|
var error = {
|
|
|
|
type: err.type || 'unknown',
|
2019-10-22 01:07:24 +08:00
|
|
|
message: stripConnectionInfo(message)
|
2017-09-21 17:46:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
for (var prop in err) {
|
|
|
|
// type & message are properties from Error's prototype and will be skipped
|
|
|
|
if (err.hasOwnProperty(prop) && shouldBeExposed(prop)) {
|
|
|
|
error[prop] = err[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
2017-11-25 00:53:07 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function setErrorHeader (errors, statusCode, res) {
|
|
|
|
const errorsCopy = errors.slice(0);
|
2017-11-27 23:43:04 +08:00
|
|
|
const mainError = errorsCopy.shift();
|
2017-11-25 00:53:07 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
const errorsLog = {
|
2017-11-29 01:22:55 +08:00
|
|
|
mainError: {
|
|
|
|
statusCode: statusCode || 200,
|
2019-10-22 01:07:24 +08:00
|
|
|
message: mainError.message,
|
|
|
|
name: mainError.name,
|
|
|
|
label: mainError.label,
|
|
|
|
type: mainError.type,
|
|
|
|
subtype: mainError.subtype
|
2017-11-29 01:22:55 +08:00
|
|
|
}
|
2017-11-25 01:06:17 +08:00
|
|
|
};
|
2017-11-25 00:53:07 +08:00
|
|
|
|
2017-11-27 23:43:04 +08:00
|
|
|
errorsLog.moreErrors = errorsCopy.map(error => {
|
2017-11-25 00:53:07 +08:00
|
|
|
return {
|
|
|
|
message: error.message,
|
2019-10-22 01:07:24 +08:00
|
|
|
name: error.name,
|
|
|
|
label: error.label,
|
|
|
|
type: error.type,
|
2017-11-25 00:53:07 +08:00
|
|
|
subtype: error.subtype
|
|
|
|
};
|
|
|
|
});
|
2018-03-23 21:13:27 +08:00
|
|
|
|
2017-12-18 19:34:56 +08:00
|
|
|
res.set('X-Tiler-Errors', stringifyForLogs(errorsLog));
|
2017-11-27 23:47:45 +08:00
|
|
|
}
|
2017-12-18 18:14:27 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-23 21:13:27 +08:00
|
|
|
* Remove problematic nested characters
|
2017-12-18 18:14:27 +08:00
|
|
|
* from object for logs RegEx
|
2018-03-23 21:13:27 +08:00
|
|
|
*
|
|
|
|
* @param {Object} object
|
2017-12-18 18:14:27 +08:00
|
|
|
*/
|
2019-10-22 01:07:24 +08:00
|
|
|
function stringifyForLogs (object) {
|
2017-12-18 18:14:27 +08:00
|
|
|
Object.keys(object).map(key => {
|
2019-10-22 01:07:24 +08:00
|
|
|
if (typeof object[key] === 'string') {
|
2017-12-18 21:54:36 +08:00
|
|
|
object[key] = object[key].replace(/[^a-zA-Z0-9]/g, ' ');
|
|
|
|
} else if (typeof object[key] === 'object') {
|
2017-12-18 19:34:56 +08:00
|
|
|
stringifyForLogs(object[key]);
|
|
|
|
} else if (object[key] instanceof Array) {
|
2019-10-22 01:07:24 +08:00
|
|
|
for (const element of object[key]) {
|
2017-12-18 19:34:56 +08:00
|
|
|
stringifyForLogs(element);
|
|
|
|
}
|
2017-12-18 18:14:27 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return JSON.stringify(object);
|
2017-12-18 19:59:44 +08:00
|
|
|
}
|