@fedect1 It's funny that it comes up with your code review. I've noticed on several occasions such as the catch all errors in posts.js as well other places, that a refactoring might be in order.
You currently have boiler plate code spread across some services= code that is necessary and also repetitive. E.g. getters/setters. The idea with interceptors is quite simple. You intercept in this case each REST call of users.js and on error send a 404. In this case you could even go so far as to include the 204 as well.
users.js
/* Update name of a user */
router.put('/profile/:userId/name', async function (req, res, next) {
try {
const user = await User.findById(req.params.userId)
if (!user) {
return res.status(404).send({ message: 'User not found' })
}
user.name = req.body.name
await user.save()
res.status(204).send(user)
} catch (error) {
res.status(404).send(error.message)
}
})
/* Update description of a user */
router.put('/profile/:userId/description', async function (req, res, next) {
try {
const user = await User.findById(req.params.userId)
if (!user) {
return res.status(404).send({ message: 'User not found' })
}
user.description = req.body.description
await user.save()
res.status(204).send(user)
} catch (error) {
res.status(404).send(error.message)
}
})
@fedect1 It's funny that it comes up with your code review. I've noticed on several occasions such as the catch all errors in
posts.js
as well other places, that a refactoring might be in order.Here's a good article on the topic: How to implement Interceptors in JS
You currently have boiler plate code spread across some services= code that is necessary and also repetitive. E.g. getters/setters. The idea with interceptors is quite simple. You intercept in this case each REST call of
users.js
and on error send a 404. In this case you could even go so far as to include the 204 as well.users.js