Simpligy error middleware

This commit is contained in:
Daniel García Aubert 2020-06-03 15:51:36 +02:00
parent c37e3f173d
commit 0eadfe6ee9

View File

@ -1,35 +1,20 @@
'use strict';
const debug = require('debug')('windshaft:cartodb:error-middleware');
module.exports = function errorMiddleware (/* options */) {
return function error (err, req, res, next) {
const { logger } = res.locals;
var allErrors = Array.isArray(err) ? err : [err];
const errors = populateLimitErrors(Array.isArray(err) ? err : [err]);
let statusCode = findStatusCode(errors[0]);
allErrors = populateLimitErrors(allErrors);
logger.error({ errors });
// TODO REMOVE THIS THREE LINES PLEASE
const label = err.label || 'UNKNOWN';
err = allErrors[0] || new Error(label);
allErrors[0] = err;
var statusCode = findStatusCode(err);
// If a callback was requested, force status to 200
if (req.query && req.query.callback) {
statusCode = 200;
}
allErrors.forEach((err) => debug('[%s ERROR] -- %d: %s, %s', err.label || 'UNKNOWN', statusCode, err, err.stack));
logger.error({ errors: allErrors });
var errorResponseBody = {
errors: allErrors.map(errorMessage),
errors_with_context: allErrors.map(errorMessageWithContext)
const errorResponseBody = {
errors: errors.map(errorMessage),
errors_with_context: errors.map(errorMessageWithContext)
};
res.status(statusCode);
// If a callback was requested, force status to 200
res.status(req.query.callback ? 200 : statusCode);
if (req.query && req.query.callback) {
res.jsonp(errorResponseBody);