Improve error messages

This commit is contained in:
Daniel García Aubert
2018-04-17 11:04:03 +02:00
parent 98d15e2e34
commit 8000a51918
+15 -6
View File
@@ -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);