From b9657b39f4061e4cf870fa253c5665ab5ad60cc6 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 25 Mar 2020 13:28:31 -0700 Subject: catch ApiValueError in some generic API calls The motivation for this change is to handle bogus revision IDs in URLs, which were causing 500 errors not 400 errors. Eg: https://qa.fatcat.wiki/file/rev/5d5d5162-b676-4f0a-968f-e19dadeaf96e%2B2019-11-27%2B13:49:51%2B0%2B6 I have no idea where these URLs are actually coming from, but they should be 4xx not 5xx. Investigating made me realize there is a whole category of ApiValueError exceptions we were not catching and should have been. --- python/fatcat_web/entity_helpers.py | 14 ++++++++++++-- python/tests/web_entity_views.py | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/python/fatcat_web/entity_helpers.py b/python/fatcat_web/entity_helpers.py index 591dda80..30dd0f6d 100644 --- a/python/fatcat_web/entity_helpers.py +++ b/python/fatcat_web/entity_helpers.py @@ -1,6 +1,6 @@ from flask import abort -from fatcat_openapi_client.rest import ApiException +from fatcat_openapi_client.rest import ApiException, ApiValueError from fatcat_tools.transforms import * from fatcat_web import app, api from fatcat_web.search import get_elastic_container_stats, get_elastic_container_random_releases @@ -118,6 +118,8 @@ def generic_get_entity(entity_type, ident): raise NotImplementedError except ApiException as ae: abort(ae.status) + except ApiValueError: + abort(400) def generic_get_entity_revision(entity_type, revision_id): try: @@ -139,6 +141,8 @@ def generic_get_entity_revision(entity_type, revision_id): raise NotImplementedError except ApiException as ae: abort(ae.status) + except ApiValueError: + abort(400) def generic_get_editgroup_entity(editgroup, entity_type, ident): if entity_type == 'container': @@ -167,6 +171,12 @@ def generic_get_editgroup_entity(editgroup, entity_type, ident): # couldn't find relevent edit in this editgroup abort(404) - entity = generic_get_entity_revision(entity_type, revision_id) + try: + entity = generic_get_entity_revision(entity_type, revision_id) + except ApiException as ae: + abort(ae.status) + except ApiValueError: + abort(400) + entity.ident = ident return entity, edit diff --git a/python/tests/web_entity_views.py b/python/tests/web_entity_views.py index 23a2b33b..e25c0296 100644 --- a/python/tests/web_entity_views.py +++ b/python/tests/web_entity_views.py @@ -42,6 +42,8 @@ def test_entity_basics(app): assert rv.status_code == 200 rv = app.get('/{}/rev/{}'.format(entity_type, revision)) assert rv.status_code == 200 + rv = app.get('/{}/rev/{}_something'.format(entity_type, revision)) + assert rv.status_code == 400 rv = app.get('/{}/rev/{}/metadata'.format(entity_type, revision)) assert rv.status_code == 200 print('/editgroup/aaaaaaaaaaaabo53aaaaaaaaaq/{}/{}'.format(entity_type, ident)) -- cgit v1.2.3