From 170415ec99bb1be654eb3af4fa8c1460bc7f064f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Tue, 12 Jun 2018 15:17:25 +0200 Subject: [PATCH] Make Comments API errors more meaningful --- comments-api/app.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/comments-api/app.py b/comments-api/app.py index 23479ff..b6a3312 100644 --- a/comments-api/app.py +++ b/comments-api/app.py @@ -123,12 +123,16 @@ class VideosResource(flask_restful.Resource): class CommentResource(flask_restful.Resource): @flask_restful.marshal_with(comment_fields) def get(self, video_id, comment_id): + q = db.session.query(Video).filter(Video.id == video_id) + video = q.first() + if not video: + flask_restful.abort(404, message='Video {0} does not exist'.format(video_id)) q = db.session.query(Association).join(Comment).filter( Association.video_id == video_id, Comment.id == comment_id) assoc = q.first() if not assoc: flask_restful.abort(404, - message='Video {0} or comment {1} does not exist'.format(video_id, comment_id)) + message='Comment {0} does not exist'.format(comment_id)) return assoc, 200 @@ -136,9 +140,11 @@ class CommentsResource(flask_restful.Resource): @flask_restful.marshal_with(comment_fields) def get(self, id): args = filter_parser.parse_args() - q = db.session.query(Association).join(Comment).filter(Association.video_id == id) - if q.count() == 0: + q = db.session.query(Video).filter(Video.id == id) + video = q.first() + if not video: flask_restful.abort(404, message='Video {0} does not exist'.format(id)) + q = db.session.query(Association).join(Comment).filter(Association.video_id == id) if args['filter']: q = q.filter(Comment.message_body.ilike('%{}%'.format(args['filter']))) count = q.count()