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):