From 24a4fbe1f820e6ce13a969f39dd7676894987532 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sat, 17 Dec 2022 12:29:48 -0700 Subject: [PATCH] Catch all possible request errors --- activities/models/fan_out.py | 66 ++++++++++++++++++++++-------------- activities/models/post.py | 2 +- activities/search.py | 2 +- mediaproxy/views.py | 2 +- users/models/follow.py | 40 ++++++++++++++-------- users/models/identity.py | 4 +-- 6 files changed, 71 insertions(+), 45 deletions(-) diff --git a/activities/models/fan_out.py b/activities/models/fan_out.py index fd52acd..c522938 100644 --- a/activities/models/fan_out.py +++ b/activities/models/fan_out.py @@ -1,3 +1,4 @@ +import httpx from asgiref.sync import sync_to_async from django.db import models @@ -52,21 +53,27 @@ class FanOutStates(StateGraph): case (FanOut.Types.post, False): post = await fan_out.subject_post.afetch_full() # Sign it and send it - await post.author.signed_request( - method="post", - uri=fan_out.identity.inbox_uri, - body=canonicalise(post.to_create_ap()), - ) + try: + await post.author.signed_request( + method="post", + uri=fan_out.identity.inbox_uri, + body=canonicalise(post.to_create_ap()), + ) + except httpx.RequestError: + return # Handle sending remote posts update case (FanOut.Types.post_edited, False): post = await fan_out.subject_post.afetch_full() # Sign it and send it - await post.author.signed_request( - method="post", - uri=fan_out.identity.inbox_uri, - body=canonicalise(post.to_update_ap()), - ) + try: + await post.author.signed_request( + method="post", + uri=fan_out.identity.inbox_uri, + body=canonicalise(post.to_update_ap()), + ) + except httpx.RequestError: + return # Handle deleting local posts case (FanOut.Types.post_deleted, True): @@ -82,11 +89,14 @@ class FanOutStates(StateGraph): case (FanOut.Types.post_deleted, False): post = await fan_out.subject_post.afetch_full() # Send it to the remote inbox - await post.author.signed_request( - method="post", - uri=fan_out.identity.inbox_uri, - body=canonicalise(post.to_delete_ap()), - ) + try: + await post.author.signed_request( + method="post", + uri=fan_out.identity.inbox_uri, + body=canonicalise(post.to_delete_ap()), + ) + except httpx.RequestError: + return # Handle local boosts/likes case (FanOut.Types.interaction, True): @@ -101,11 +111,14 @@ class FanOutStates(StateGraph): case (FanOut.Types.interaction, False): interaction = await fan_out.subject_post_interaction.afetch_full() # Send it to the remote inbox - await interaction.identity.signed_request( - method="post", - uri=fan_out.identity.inbox_uri, - body=canonicalise(interaction.to_ap()), - ) + try: + await interaction.identity.signed_request( + method="post", + uri=fan_out.identity.inbox_uri, + body=canonicalise(interaction.to_ap()), + ) + except httpx.RequestError: + return # Handle undoing local boosts/likes case (FanOut.Types.undo_interaction, True): # noqa:F841 @@ -121,11 +134,14 @@ class FanOutStates(StateGraph): case (FanOut.Types.undo_interaction, False): # noqa:F841 interaction = await fan_out.subject_post_interaction.afetch_full() # Send an undo to the remote inbox - await interaction.identity.signed_request( - method="post", - uri=fan_out.identity.inbox_uri, - body=canonicalise(interaction.to_undo_ap()), - ) + try: + await interaction.identity.signed_request( + method="post", + uri=fan_out.identity.inbox_uri, + body=canonicalise(interaction.to_undo_ap()), + ) + except httpx.RequestError: + return case _: raise ValueError( diff --git a/activities/models/post.py b/activities/models/post.py index 8a9aabb..d6914c9 100644 --- a/activities/models/post.py +++ b/activities/models/post.py @@ -727,7 +727,7 @@ class Post(StatorModel): response = async_to_sync(SystemActor().signed_request)( method="get", uri=object_uri ) - except (httpx.RequestError, httpx.ConnectError): + except httpx.RequestError: raise cls.DoesNotExist(f"Could not fetch {object_uri}") if response.status_code in [404, 410]: raise cls.DoesNotExist(f"No post at {object_uri}") diff --git a/activities/search.py b/activities/search.py index e192b94..5a43156 100644 --- a/activities/search.py +++ b/activities/search.py @@ -74,7 +74,7 @@ class Searcher: method="get", uri=self.query, ) - except (httpx.RequestError, httpx.ConnectError): + except httpx.RequestError: return None if response.status_code >= 400: return None diff --git a/mediaproxy/views.py b/mediaproxy/views.py index 4fc09b1..7e16e04 100644 --- a/mediaproxy/views.py +++ b/mediaproxy/views.py @@ -33,7 +33,7 @@ class BaseCacheView(View): follow_redirects=True, timeout=settings.SETUP.REMOTE_TIMEOUT, ) - except (httpx.ConnectError, httpx.RequestError): + except httpx.RequestError: return HttpResponse(status=502) if remote_response.status_code >= 400: return HttpResponse(status=502) diff --git a/users/models/follow.py b/users/models/follow.py index 5f0d6ab..e96e988 100644 --- a/users/models/follow.py +++ b/users/models/follow.py @@ -1,5 +1,6 @@ from typing import Optional +import httpx from django.db import models, transaction from core.ld import canonicalise @@ -37,11 +38,14 @@ class FollowStates(StateGraph): if not follow.source.local: return cls.remote_requested # Sign it and send it - await follow.source.signed_request( - method="post", - uri=follow.target.inbox_uri, - body=canonicalise(follow.to_ap()), - ) + try: + await follow.source.signed_request( + method="post", + uri=follow.target.inbox_uri, + body=canonicalise(follow.to_ap()), + ) + except httpx.RequestError: + return return cls.local_requested @classmethod @@ -56,11 +60,14 @@ class FollowStates(StateGraph): source server. """ follow = await instance.afetch_full() - await follow.target.signed_request( - method="post", - uri=follow.source.inbox_uri, - body=canonicalise(follow.to_accept_ap()), - ) + try: + await follow.target.signed_request( + method="post", + uri=follow.source.inbox_uri, + body=canonicalise(follow.to_accept_ap()), + ) + except httpx.RequestError: + return return cls.accepted @classmethod @@ -69,11 +76,14 @@ class FollowStates(StateGraph): Delivers the Undo object to the target server """ follow = await instance.afetch_full() - await follow.source.signed_request( - method="post", - uri=follow.target.inbox_uri, - body=canonicalise(follow.to_undo_ap()), - ) + try: + await follow.source.signed_request( + method="post", + uri=follow.target.inbox_uri, + body=canonicalise(follow.to_undo_ap()), + ) + except httpx.RequestError: + return return cls.undone_remotely diff --git a/users/models/identity.py b/users/models/identity.py index 0ea590e..7c3fa35 100644 --- a/users/models/identity.py +++ b/users/models/identity.py @@ -402,7 +402,7 @@ class Identity(StatorModel): response = await client.get( f"https://{domain}/.well-known/webfinger?resource=acct:{handle}", ) - except (httpx.RequestError, httpx.ConnectError): + except httpx.RequestError: return None, None if response.status_code in [404, 410]: return None, None @@ -438,7 +438,7 @@ class Identity(StatorModel): method="get", uri=self.actor_uri, ) - except (httpx.ConnectError, httpx.RequestError): + except httpx.RequestError: return False if response.status_code == 410: # Their account got deleted, so let's do the same.