From 8000a519187e1b406ffe24d64392a0adc63a0377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 17 Apr 2018 11:04:03 +0200 Subject: [PATCH] Improve error messages --- lib/cartodb/middleware/coordinates.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/cartodb/middleware/coordinates.js b/lib/cartodb/middleware/coordinates.js index 43fa0fb6..9a83f181 100644 --- a/lib/cartodb/middleware/coordinates.js +++ b/lib/cartodb/middleware/coordinates.js @@ -1,12 +1,21 @@ -module.exports = function coordinates (validate = { z: true, x: true, y: true }) { - const positiveIntegerNumber = /^\d+$/; - const integerNumber = /^-?\d+$/; +const positiveIntegerNumber = /^\d+$/; +const integerNumber = /^-?\d+$/; +const invalidZoomMessage = function (zoom) { + return `Invalid zoom value (${zoom}). It should be an integer number greather or equal to 0`; +}; +const invalidCoordXMessage = function (x) { + return `Invalid coodinate 'x' value (${x}). It should be an integer number`; +}; +const invalidCoordYMessage = function (y) { + return `Invalid coodinate 'y' value (${y}). It should be an integer number greather or equal to 0`; +}; +module.exports = function coordinates (validate = { z: true, x: true, y: true }) { return function coordinatesMiddleware (req, res, next) { const { z, x, y } = req.params; if (validate.z && !positiveIntegerNumber.test(z)) { - const err = new Error(`Invalid zoom value (${z}). It should be a positive number`); + const err = new Error(invalidZoomMessage(z)); err.http_status = 400; return next(err); @@ -14,14 +23,14 @@ module.exports = function coordinates (validate = { z: true, x: true, y: true }) // Negative values for x param are valid. The x param is wrapped if (validate.x && !integerNumber.test(x)) { - const err = new Error(`Invalid coodinate 'x' value (${x}). It should be a number`); + const err = new Error(invalidCoordXMessage(x)); err.http_status = 400; return next(err); } if (validate.y && !positiveIntegerNumber.test(y)) { - const err = new Error(`Invalid coodinate 'y' value (${y}). It should be a positive number`); + const err = new Error(invalidCoordYMessage(y)); err.http_status = 400; return next(err);