|
const isValidParam = (endpoint, val, res) => { |
|
let err422 = 'invalid query parameter' |
|
let err416 = 'product does not exist' |
|
if (endpoint === 'productId' || endpoint === 'styles' || endpoint === 'related') { |
|
try { |
|
if (Number.isNaN(Number(val))) { |
|
throw(err422) |
|
} else if (Number(val) < 1 || Number(val) > productsCount) { |
|
throw(err416) |
|
} |
|
} |
|
catch (error) { |
|
if (error === err422) { |
|
res.status(422).send('422 Error: Unprocessable Entity. Your query parameter for /products must be an integer') |
|
} else if (error === err416) { |
|
res.status(416).send('The product ID you chose does not exist') |
|
} |
|
return false; |
|
} |
|
} else { |
|
if (val < 1 || val > 100002) { |
|
res.sendStatus(416); |
|
return false; |
|
} |
|
} |
|
return true; |
|
} |
This is a cool function that allows the determination of errors. I would again encourage separating this into its own module. Right now it's at the bottom of the server.js file and is a little lost.
Products-API/server.js
Lines 95 to 121 in 0b24ac9
This is a cool function that allows the determination of errors. I would again encourage separating this into its own module. Right now it's at the bottom of the server.js file and is a little lost.