From 0eadfe6ee9c0aceb9ea7fe3ca61a603ea29b3a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 3 Jun 2020 15:51:36 +0200 Subject: [PATCH] Simpligy error middleware --- lib/api/middlewares/error-middleware.js | 31 +++++++------------------ 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/lib/api/middlewares/error-middleware.js b/lib/api/middlewares/error-middleware.js index 583b54f5..f4258383 100644 --- a/lib/api/middlewares/error-middleware.js +++ b/lib/api/middlewares/error-middleware.js @@ -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);