Catch all possible request errors

This commit is contained in:
Andrew Godwin 2022-12-17 12:29:48 -07:00
parent 1f28361fd9
commit 24a4fbe1f8
6 changed files with 71 additions and 45 deletions

View File

@ -1,3 +1,4 @@
import httpx
from asgiref.sync import sync_to_async from asgiref.sync import sync_to_async
from django.db import models from django.db import models
@ -52,21 +53,27 @@ class FanOutStates(StateGraph):
case (FanOut.Types.post, False): case (FanOut.Types.post, False):
post = await fan_out.subject_post.afetch_full() post = await fan_out.subject_post.afetch_full()
# Sign it and send it # Sign it and send it
await post.author.signed_request( try:
method="post", await post.author.signed_request(
uri=fan_out.identity.inbox_uri, method="post",
body=canonicalise(post.to_create_ap()), uri=fan_out.identity.inbox_uri,
) body=canonicalise(post.to_create_ap()),
)
except httpx.RequestError:
return
# Handle sending remote posts update # Handle sending remote posts update
case (FanOut.Types.post_edited, False): case (FanOut.Types.post_edited, False):
post = await fan_out.subject_post.afetch_full() post = await fan_out.subject_post.afetch_full()
# Sign it and send it # Sign it and send it
await post.author.signed_request( try:
method="post", await post.author.signed_request(
uri=fan_out.identity.inbox_uri, method="post",
body=canonicalise(post.to_update_ap()), uri=fan_out.identity.inbox_uri,
) body=canonicalise(post.to_update_ap()),
)
except httpx.RequestError:
return
# Handle deleting local posts # Handle deleting local posts
case (FanOut.Types.post_deleted, True): case (FanOut.Types.post_deleted, True):
@ -82,11 +89,14 @@ class FanOutStates(StateGraph):
case (FanOut.Types.post_deleted, False): case (FanOut.Types.post_deleted, False):
post = await fan_out.subject_post.afetch_full() post = await fan_out.subject_post.afetch_full()
# Send it to the remote inbox # Send it to the remote inbox
await post.author.signed_request( try:
method="post", await post.author.signed_request(
uri=fan_out.identity.inbox_uri, method="post",
body=canonicalise(post.to_delete_ap()), uri=fan_out.identity.inbox_uri,
) body=canonicalise(post.to_delete_ap()),
)
except httpx.RequestError:
return
# Handle local boosts/likes # Handle local boosts/likes
case (FanOut.Types.interaction, True): case (FanOut.Types.interaction, True):
@ -101,11 +111,14 @@ class FanOutStates(StateGraph):
case (FanOut.Types.interaction, False): case (FanOut.Types.interaction, False):
interaction = await fan_out.subject_post_interaction.afetch_full() interaction = await fan_out.subject_post_interaction.afetch_full()
# Send it to the remote inbox # Send it to the remote inbox
await interaction.identity.signed_request( try:
method="post", await interaction.identity.signed_request(
uri=fan_out.identity.inbox_uri, method="post",
body=canonicalise(interaction.to_ap()), uri=fan_out.identity.inbox_uri,
) body=canonicalise(interaction.to_ap()),
)
except httpx.RequestError:
return
# Handle undoing local boosts/likes # Handle undoing local boosts/likes
case (FanOut.Types.undo_interaction, True): # noqa:F841 case (FanOut.Types.undo_interaction, True): # noqa:F841
@ -121,11 +134,14 @@ class FanOutStates(StateGraph):
case (FanOut.Types.undo_interaction, False): # noqa:F841 case (FanOut.Types.undo_interaction, False): # noqa:F841
interaction = await fan_out.subject_post_interaction.afetch_full() interaction = await fan_out.subject_post_interaction.afetch_full()
# Send an undo to the remote inbox # Send an undo to the remote inbox
await interaction.identity.signed_request( try:
method="post", await interaction.identity.signed_request(
uri=fan_out.identity.inbox_uri, method="post",
body=canonicalise(interaction.to_undo_ap()), uri=fan_out.identity.inbox_uri,
) body=canonicalise(interaction.to_undo_ap()),
)
except httpx.RequestError:
return
case _: case _:
raise ValueError( raise ValueError(

View File

@ -727,7 +727,7 @@ class Post(StatorModel):
response = async_to_sync(SystemActor().signed_request)( response = async_to_sync(SystemActor().signed_request)(
method="get", uri=object_uri method="get", uri=object_uri
) )
except (httpx.RequestError, httpx.ConnectError): except httpx.RequestError:
raise cls.DoesNotExist(f"Could not fetch {object_uri}") raise cls.DoesNotExist(f"Could not fetch {object_uri}")
if response.status_code in [404, 410]: if response.status_code in [404, 410]:
raise cls.DoesNotExist(f"No post at {object_uri}") raise cls.DoesNotExist(f"No post at {object_uri}")

View File

@ -74,7 +74,7 @@ class Searcher:
method="get", method="get",
uri=self.query, uri=self.query,
) )
except (httpx.RequestError, httpx.ConnectError): except httpx.RequestError:
return None return None
if response.status_code >= 400: if response.status_code >= 400:
return None return None

View File

@ -33,7 +33,7 @@ class BaseCacheView(View):
follow_redirects=True, follow_redirects=True,
timeout=settings.SETUP.REMOTE_TIMEOUT, timeout=settings.SETUP.REMOTE_TIMEOUT,
) )
except (httpx.ConnectError, httpx.RequestError): except httpx.RequestError:
return HttpResponse(status=502) return HttpResponse(status=502)
if remote_response.status_code >= 400: if remote_response.status_code >= 400:
return HttpResponse(status=502) return HttpResponse(status=502)

View File

@ -1,5 +1,6 @@
from typing import Optional from typing import Optional
import httpx
from django.db import models, transaction from django.db import models, transaction
from core.ld import canonicalise from core.ld import canonicalise
@ -37,11 +38,14 @@ class FollowStates(StateGraph):
if not follow.source.local: if not follow.source.local:
return cls.remote_requested return cls.remote_requested
# Sign it and send it # Sign it and send it
await follow.source.signed_request( try:
method="post", await follow.source.signed_request(
uri=follow.target.inbox_uri, method="post",
body=canonicalise(follow.to_ap()), uri=follow.target.inbox_uri,
) body=canonicalise(follow.to_ap()),
)
except httpx.RequestError:
return
return cls.local_requested return cls.local_requested
@classmethod @classmethod
@ -56,11 +60,14 @@ class FollowStates(StateGraph):
source server. source server.
""" """
follow = await instance.afetch_full() follow = await instance.afetch_full()
await follow.target.signed_request( try:
method="post", await follow.target.signed_request(
uri=follow.source.inbox_uri, method="post",
body=canonicalise(follow.to_accept_ap()), uri=follow.source.inbox_uri,
) body=canonicalise(follow.to_accept_ap()),
)
except httpx.RequestError:
return
return cls.accepted return cls.accepted
@classmethod @classmethod
@ -69,11 +76,14 @@ class FollowStates(StateGraph):
Delivers the Undo object to the target server Delivers the Undo object to the target server
""" """
follow = await instance.afetch_full() follow = await instance.afetch_full()
await follow.source.signed_request( try:
method="post", await follow.source.signed_request(
uri=follow.target.inbox_uri, method="post",
body=canonicalise(follow.to_undo_ap()), uri=follow.target.inbox_uri,
) body=canonicalise(follow.to_undo_ap()),
)
except httpx.RequestError:
return
return cls.undone_remotely return cls.undone_remotely

View File

@ -402,7 +402,7 @@ class Identity(StatorModel):
response = await client.get( response = await client.get(
f"https://{domain}/.well-known/webfinger?resource=acct:{handle}", f"https://{domain}/.well-known/webfinger?resource=acct:{handle}",
) )
except (httpx.RequestError, httpx.ConnectError): except httpx.RequestError:
return None, None return None, None
if response.status_code in [404, 410]: if response.status_code in [404, 410]:
return None, None return None, None
@ -438,7 +438,7 @@ class Identity(StatorModel):
method="get", method="get",
uri=self.actor_uri, uri=self.actor_uri,
) )
except (httpx.ConnectError, httpx.RequestError): except httpx.RequestError:
return False return False
if response.status_code == 410: if response.status_code == 410:
# Their account got deleted, so let's do the same. # Their account got deleted, so let's do the same.