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 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(

View File

@ -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}")

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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.