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

Commit

Permalink
Merge pull request #349 from lirantal/bugfix-348-article-not-found
Browse files Browse the repository at this point in the history
fixing issue #348 - instead of returning a server error 500 on article loading which isnt found we'll throw a 404 with json message
  • Loading branch information
lirantal committed Jan 14, 2015
2 parents b2e9b3d + 9952138 commit 92517f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/controllers/articles.server.controller.js
Expand Up @@ -88,9 +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(400).send({
message: 'Article is invalid'
});
}

Article.findById(id).populate('user', 'displayName').exec(function(err, article) {
if (err) return next(err);
if (!article) return next(new Error('Failed to load article ' + id));
if (!article) {
return res.status(404).send({
message: 'Article not found'
});
}
req.article = article;
next();
});
Expand Down
11 changes: 11 additions & 0 deletions app/tests/article.server.routes.test.js
Expand Up @@ -201,6 +201,17 @@ describe('Article CRUD tests', function() {
});
});

it('should return proper error for single article which doesnt exist, if not signed in', function(done) {
request(app).get('/articles/test')
.end(function(req, res) {
// Set assertion
res.body.should.be.an.Object.with.property('message', 'Article is invalid');

// Call the assertion callback
done();
});
});

it('should be able to delete an article if signed in', function(done) {
agent.post('/auth/signin')
.send(credentials)
Expand Down

0 comments on commit 92517f7

Please sign in to comment.