From 5bccbdd5fa533e3f334f42e50ca3a0a6aeef4100 Mon Sep 17 00:00:00 2001 From: Liran Tal Date: Mon, 12 Jan 2015 23:22:25 +0200 Subject: [PATCH] adding proper handling for invalid model ObjectIds passed on to article routes --- app/controllers/articles.server.controller.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/controllers/articles.server.controller.js b/app/controllers/articles.server.controller.js index e4fa5a41..7b991bf7 100644 --- a/app/controllers/articles.server.controller.js +++ b/app/controllers/articles.server.controller.js @@ -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(); });