From 69db47a78007ab1fa9246169c9d23a685c9e73a9 Mon Sep 17 00:00:00 2001 From: Deimos Date: Sat, 29 Jun 2019 15:03:26 -0600 Subject: [PATCH] Move "not found" errors into root factories Previously the "web API" exceptions were being based on the matched route, but that would cause issues such as trying to move a topic to a non-existent group. That's a PATCH request on a topic, so it would display an error of "Topic not found" instead of "Group not found". This moves the logic into the root factories and displays the message attached to the HTTPNotFound exception when one is returned, which should work more properly. --- tildes/tildes/resources/comment.py | 7 +++++-- tildes/tildes/resources/topic.py | 5 ++++- tildes/tildes/views/api/web/exceptions.py | 8 ++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/tildes/tildes/resources/comment.py b/tildes/tildes/resources/comment.py index 7650788..a89fccc 100644 --- a/tildes/tildes/resources/comment.py +++ b/tildes/tildes/resources/comment.py @@ -3,7 +3,7 @@ """Root factories for comments.""" -from pyramid.httpexceptions import HTTPForbidden +from pyramid.httpexceptions import HTTPForbidden, HTTPNotFound from pyramid.request import Request from webargs.pyramidparser import use_kwargs @@ -22,7 +22,10 @@ def comment_by_id36(request: Request, comment_id36: str) -> Comment: .filter_by(comment_id=id36_to_id(comment_id36)) ) - return get_resource(request, query) + try: + return get_resource(request, query) + except HTTPNotFound: + raise HTTPNotFound("Comment not found (or it was deleted)") @use_kwargs(CommentSchema(only=("comment_id36",)), locations=("matchdict",)) diff --git a/tildes/tildes/resources/topic.py b/tildes/tildes/resources/topic.py index 3819915..e53f5df 100644 --- a/tildes/tildes/resources/topic.py +++ b/tildes/tildes/resources/topic.py @@ -28,7 +28,10 @@ def topic_by_id36(request: Request, topic_id36: str) -> Topic: .filter_by(topic_id=topic_id) ) - topic = get_resource(request, query) + try: + topic = get_resource(request, query) + except HTTPNotFound: + raise HTTPNotFound("Topic not found (or it was deleted)") # if there's also a group specified in the route, check that it's the same group as # the topic was posted in, otherwise redirect to correct group diff --git a/tildes/tildes/views/api/web/exceptions.py b/tildes/tildes/views/api/web/exceptions.py index b6f85ef..ffa3f6b 100644 --- a/tildes/tildes/views/api/web/exceptions.py +++ b/tildes/tildes/views/api/web/exceptions.py @@ -17,8 +17,6 @@ from pyramid.httpexceptions import ( from pyramid.request import Request from pyramid.response import Response -from tildes.resources.comment import comment_by_id36 -from tildes.resources.topic import topic_by_id36 from tildes.views.decorators import ic_view_config from tildes.views.exceptions import errors_from_validationerror @@ -69,10 +67,8 @@ def error_to_text_response(request: Request) -> Response: response = request.exception if isinstance(request.exception, HTTPNotFound): - if request.matched_route.factory == comment_by_id36: - response.text = "Comment not found (or it was deleted)" - elif request.matched_route.factory == topic_by_id36: - response.text = "Topic not found (or it was deleted)" + if response.message: + response.text = response.message else: response.text = "Not found" elif isinstance(request.exception, HTTPForbidden):