I have a form and when something doesn't go right after submission, I want to be able to give the user specific error messages. I am using Sequelize as my ORM, and the promises returned are getting a bit messy with all the nested code.
For instance, if we have two models to update: User
and Photo
:
models.User.find({where: {name: 'bob' }}).then(function(user) {
if(user) {
user.updateAttributes({email: email}).then(function(user) {
models.Photo.find({where: { hash: hash}}).then(function(photo) {
if(photo)
res.json({"message":"updated"});
else
res.status(404).json({"error": "Could not find Photo"});
}).catch(err) {
res.status(500).json({"error": err});
});
}).catch(function(err) {
res.status(500).json({"error": err});
});
} else {
res.status(404).json({"error": "Could not find user"});
}
}).catch(function(err) {
res.status(500).json({"error": err});
});
And if I have 10 fields in the form to update, all the nested code can become overbearing.
What recommendations can be given if I wish to have specific error descriptions, but also more readable code? Would it be possible to capture all the 404 and 500 errors in one code block instead of breaking them up like I have?
For example: app. get('/', (req, res) => { throw new Error('BROKEN') // Express will catch this on its own. }) If getUserById throws an error or rejects, next will be called with either the thrown error or the rejected value.
If we want to handle the error for asynchronous code in Node. js then we can do it in the following two manners. Handle error using callback: A callback function is to perform some operation after the function execution is completed. We can call our callback function after an asynchronous operation is completed.
You can make use of promise chaining and a helper method to reduce your code to a series of 3 .then
s and a single .catch
:
function notFoundError(model) {
var e = new Error('Could not find ' + model);
e.statusCode = 404;
throw e;
}
models.User
.find({where: {name: 'bob'}})
.then(function (user) {
if (user) {
return user.updateAttributes({email: email});
} else {
notFoundError('user');
}
})
.then(function (user) {
return models.photos.find({where: {hash: hash}});
})
.then(function (photo) {
if (photo)
res.json({"message": "updated"});
else
notFoundError('photo');
})
.catch(function (err) {
if (err.statusCode) {
res.status(err.statusCode);
} else {
res.status(500);
}
res.json({'error': err.message});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With