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

Commit 5bccbdd

Browse files
committed
adding proper handling for invalid model ObjectIds passed on to article routes
1 parent 56aff70 commit 5bccbdd

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

app/controllers/articles.server.controller.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,20 @@ exports.list = function(req, res) {
8888
* Article middleware
8989
*/
9090
exports.articleByID = function(req, res, next, id) {
91+
92+
if (!mongoose.Types.ObjectId.isValid(id)) {
93+
return res.status(500).send({
94+
message: 'Article is invalid'
95+
});
96+
}
97+
9198
Article.findById(id).populate('user', 'displayName').exec(function(err, article) {
92-
if (err) {
93-
return res.status(404).send({
99+
if (err) return next(err);
100+
if (!article) {
101+
res.status(404).send({
94102
message: 'Article not found'
95-
});
103+
});
96104
}
97-
if (!article) return next(new Error('Failed to load article ' + id));
98105
req.article = article;
99106
next();
100107
});

0 commit comments

Comments
 (0)