From f6088373cd679fcca5ac2809da93c89295f16b7e Mon Sep 17 00:00:00 2001 From: Deimos Date: Mon, 24 Jun 2019 18:56:07 -0600 Subject: [PATCH] Handle topic ID 0 in topic_by_id36 root factory This causes an internal server occasionally from people trying to see if there's a topic with ID 0 (often via tild.es/0), so this will just return a 404 instead. --- tildes/tildes/resources/topic.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tildes/tildes/resources/topic.py b/tildes/tildes/resources/topic.py index 3cac7f4..3819915 100644 --- a/tildes/tildes/resources/topic.py +++ b/tildes/tildes/resources/topic.py @@ -3,7 +3,7 @@ """Root factories for topics.""" -from pyramid.httpexceptions import HTTPFound +from pyramid.httpexceptions import HTTPFound, HTTPNotFound from pyramid.request import Request from webargs.pyramidparser import use_kwargs @@ -16,11 +16,16 @@ from tildes.schemas.topic import TopicSchema @use_kwargs(TopicSchema(only=("topic_id36",)), locations=("matchdict",)) def topic_by_id36(request: Request, topic_id36: str) -> Topic: """Get a topic specified by {topic_id36} in the route (or 404).""" + try: + topic_id = id36_to_id(topic_id36) + except ValueError: + raise HTTPNotFound + query = ( request.query(Topic) .include_deleted() .include_removed() - .filter_by(topic_id=id36_to_id(topic_id36)) + .filter_by(topic_id=topic_id) ) topic = get_resource(request, query)