Browse Source

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.
merge-requests/72/head
Deimos 6 years ago
parent
commit
69db47a780
  1. 7
      tildes/tildes/resources/comment.py
  2. 5
      tildes/tildes/resources/topic.py
  3. 8
      tildes/tildes/views/api/web/exceptions.py

7
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",))

5
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

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

Loading…
Cancel
Save