Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
adding proper handling for invalid model ObjectIds passed on to artic…
Browse files Browse the repository at this point in the history
…le routes
  • Loading branch information
lirantal committed Jan 12, 2015
1 parent 56aff70 commit 5bccbdd
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions app/controllers/articles.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,20 @@ exports.list = function(req, res) {
* Article middleware
*/
exports.articleByID = function(req, res, next, id) {

if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(500).send({
message: 'Article is invalid'
});
}

Article.findById(id).populate('user', 'displayName').exec(function(err, article) {
if (err) {
return res.status(404).send({
if (err) return next(err);
if (!article) {
res.status(404).send({
message: 'Article not found'
});
});
}
if (!article) return next(new Error('Failed to load article ' + id));
req.article = article;
next();
});
Expand Down

0 comments on commit 5bccbdd

Please sign in to comment.