Skip to content

Commit 9724b21

Browse files
committed
fix: treat LinkedEvents 404/410 as ObjectDoesNotExistError
Event/place resolvers now use shared LinkedEvents fetch helper. 404 and 410 are converted to ObjectDoesNotExistError so they are not reported to Sentry as HTTPError. Refs: PT-1990
1 parent 957bda3 commit 9724b21

4 files changed

Lines changed: 102 additions & 14 deletions

File tree

graphene_linked_events/schema.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
get_keyword_set_by_id,
4040
json2obj,
4141
json_object_hook,
42+
retrieve_linked_events_data,
4243
)
4344
from occurrences.event_api_services import prepare_published_event_data
4445
from occurrences.models import PalvelutarjotinEvent, VenueCustomData
@@ -491,15 +492,11 @@ def _test_events_p_event_relations(events_json):
491492

492493
@staticmethod
493494
def resolve_event(parent, info, **kwargs):
494-
response = api_client.retrieve(
495-
"event",
496-
kwargs.pop("id"),
497-
params=kwargs,
498-
is_event_staff=getattr(info.context.user, "is_event_staff", False),
495+
event_id = kwargs.pop("id")
496+
is_event_staff = getattr(info.context.user, "is_event_staff", False)
497+
return retrieve_linked_events_data(
498+
"event", event_id, params=kwargs, is_event_staff=is_event_staff
499499
)
500-
response.raise_for_status()
501-
obj = json2obj(format_response(response))
502-
return obj
503500

504501
@staticmethod
505502
def resolve_events(parent, info, **kwargs): # noqa: C901
@@ -620,9 +617,7 @@ def resolve_place(parent, info, **kwargs):
620617
place_id = kwargs["id"]
621618
if not place_id:
622619
return None
623-
response = api_client.retrieve("place", place_id)
624-
response.raise_for_status()
625-
return json2obj(format_response(response))
620+
return retrieve_linked_events_data("place", place_id)
626621

627622
@staticmethod
628623
def resolve_places(parent, info, **kwargs):

graphene_linked_events/tests/__snapshots__/test_api.ambr

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,30 @@
394394
}),
395395
})
396396
# ---
397+
# name: test_get_event_gone
398+
dict({
399+
'data': dict({
400+
'event': None,
401+
}),
402+
'errors': list([
403+
dict({
404+
'extensions': dict({
405+
'code': 'OBJECT_DOES_NOT_EXIST_ERROR',
406+
}),
407+
'locations': list([
408+
dict({
409+
'column': 3,
410+
'line': 3,
411+
}),
412+
]),
413+
'message': 'The resource is no longer available in the API (gone).',
414+
'path': list([
415+
'event',
416+
]),
417+
}),
418+
]),
419+
})
420+
# ---
397421
# name: test_get_event_not_found
398422
dict({
399423
'data': dict({
@@ -402,15 +426,15 @@
402426
'errors': list([
403427
dict({
404428
'extensions': dict({
405-
'code': 'GENERAL_ERROR',
429+
'code': 'OBJECT_DOES_NOT_EXIST_ERROR',
406430
}),
407431
'locations': list([
408432
dict({
409433
'column': 3,
410434
'line': 3,
411435
}),
412436
]),
413-
'message': 'A mocked generic HTTP error',
437+
'message': 'Could not find the event from the API.',
414438
'path': list([
415439
'event',
416440
]),
@@ -915,6 +939,30 @@
915939
}),
916940
})
917941
# ---
942+
# name: test_get_place_gone
943+
dict({
944+
'data': dict({
945+
'place': None,
946+
}),
947+
'errors': list([
948+
dict({
949+
'extensions': dict({
950+
'code': 'OBJECT_DOES_NOT_EXIST_ERROR',
951+
}),
952+
'locations': list([
953+
dict({
954+
'column': 3,
955+
'line': 3,
956+
}),
957+
]),
958+
'message': 'The resource is no longer available in the API (gone).',
959+
'path': list([
960+
'place',
961+
]),
962+
}),
963+
]),
964+
})
965+
# ---
918966
# name: test_get_places
919967
dict({
920968
'data': dict({

graphene_linked_events/tests/test_api.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,24 @@ def mock_data(*args, **kwargs):
740740
snapshot.assert_match(executed)
741741

742742

743+
def test_get_event_gone(api_client, snapshot, monkeypatch):
744+
"""Test that 410 (Gone) status code is handled as ObjectDoesNotExistError."""
745+
746+
def _get_mock_function(event_data, keyword_data, status_code=410):
747+
def mock_data(*args, **kwargs):
748+
return MockResponse(status_code=status_code, json_data={})
749+
750+
return mock_data
751+
752+
monkeypatch.setattr(
753+
graphene_linked_events.rest_client.LinkedEventsApiClient,
754+
"retrieve",
755+
_get_mock_function(EVENT_DATA, KEYWORD_SET_DATA),
756+
)
757+
executed = api_client.execute(GET_EVENT_QUERY)
758+
snapshot.assert_match(executed)
759+
760+
743761
def test_get_places(api_client, snapshot, mock_get_places_data):
744762
executed = api_client.execute(GET_PLACES_QUERY)
745763
snapshot.assert_match(executed)
@@ -750,6 +768,24 @@ def test_get_place(api_client, snapshot, mock_get_place_data):
750768
snapshot.assert_match(executed)
751769

752770

771+
def test_get_place_gone(api_client, snapshot, monkeypatch):
772+
"""Test that 410 (Gone) status code is handled as ObjectDoesNotExistError for places."""
773+
774+
def _get_mock_function(place_data, status_code=410):
775+
def mock_data(*args, **kwargs):
776+
return MockResponse(status_code=status_code, json_data={})
777+
778+
return mock_data
779+
780+
monkeypatch.setattr(
781+
graphene_linked_events.rest_client.LinkedEventsApiClient,
782+
"retrieve",
783+
_get_mock_function({}),
784+
)
785+
executed = api_client.execute(GET_PLACE_QUERY)
786+
snapshot.assert_match(executed)
787+
788+
753789
def test_get_keywords(api_client, snapshot, mock_get_keywords_data):
754790
executed = api_client.execute(GET_KEYWORDS_QUERY)
755791
snapshot.assert_match(executed)
@@ -2243,7 +2279,11 @@ def mock_retrieve_with_mixed_results(
22432279

22442280
@pytest.mark.parametrize(
22452281
"status_code,error_cls",
2246-
[(400, ApiBadRequestError), (404, ObjectDoesNotExistError)],
2282+
[
2283+
(400, ApiBadRequestError),
2284+
(404, ObjectDoesNotExistError),
2285+
(410, ObjectDoesNotExistError),
2286+
],
22472287
)
22482288
def test_linkedevents_api_retrieve_errors(status_code, error_cls):
22492289
with patch.object(

graphene_linked_events/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def retrieve_linked_events_data(
5757
if response.status_code == 404:
5858
raise ObjectDoesNotExistError("Could not find the event from the API.")
5959

60+
if response.status_code == 410:
61+
raise ObjectDoesNotExistError(
62+
"The resource is no longer available in the API (gone)."
63+
)
64+
6065
# Raise any other errors
6166
response.raise_for_status()
6267

0 commit comments

Comments
 (0)