Improve fetch

main
Thomas Sileo 2022-09-12 08:04:16 +02:00
parent 0c7a19749d
commit 04da8725ed
4 changed files with 12 additions and 5 deletions

View File

@ -61,6 +61,10 @@ class ObjectNotFoundError(Exception):
pass pass
class ObjectUnavailableError(Exception):
pass
class FetchErrorTypeEnum(str, enum.Enum): class FetchErrorTypeEnum(str, enum.Enum):
TIMEOUT = "TIMEOUT" TIMEOUT = "TIMEOUT"
NOT_FOUND = "NOT_FOUND" NOT_FOUND = "NOT_FOUND"
@ -167,6 +171,8 @@ async def fetch(
# Special handling for deleted object # Special handling for deleted object
if resp.status_code == 410: if resp.status_code == 410:
raise ObjectIsGoneError(f"{url} is gone") raise ObjectIsGoneError(f"{url} is gone")
elif resp.status_code in [401, 403]:
raise ObjectUnavailableError(f"not allowed to fetch {url}")
elif resp.status_code == 404: elif resp.status_code == 404:
raise ObjectNotFoundError(f"{url} not found") raise ObjectNotFoundError(f"{url} not found")

View File

@ -85,6 +85,8 @@ async def get_lookup(
error = ap.FetchErrorTypeEnum.TIMEOUT error = ap.FetchErrorTypeEnum.TIMEOUT
except (ap.ObjectNotFoundError, ap.ObjectIsGoneError): except (ap.ObjectNotFoundError, ap.ObjectIsGoneError):
error = ap.FetchErrorTypeEnum.NOT_FOUND error = ap.FetchErrorTypeEnum.NOT_FOUND
except (ap.ObjectUnavailableError):
error = ap.FetchErrorTypeEnum.UNAUHTORIZED
except Exception: except Exception:
logger.exception(f"Failed to lookup {query}") logger.exception(f"Failed to lookup {query}")
error = ap.FetchErrorTypeEnum.INTERNAL_ERROR error = ap.FetchErrorTypeEnum.INTERNAL_ERROR

View File

@ -115,11 +115,8 @@ async def _get_public_key(db_session: AsyncSession, key_id: str) -> Key:
# might race to fetch each other key # might race to fetch each other key
try: try:
actor = await ap.fetch(key_id, disable_httpsig=True) actor = await ap.fetch(key_id, disable_httpsig=True)
except httpx.HTTPStatusError as http_err: except ap.ObjectUnavailableError:
if http_err.response.status_code in [401, 403]: actor = await ap.fetch(key_id, disable_httpsig=False)
actor = await ap.fetch(key_id, disable_httpsig=False)
else:
raise
if actor["type"] == "Key": if actor["type"] == "Key":
# The Key is not embedded in the Person # The Key is not embedded in the Person

View File

@ -20,6 +20,8 @@
<div class="box error-box"> <div class="box error-box">
{% if error.value == "NOT_FOUND" %} {% if error.value == "NOT_FOUND" %}
<p>The remote object is unavailable.</p> <p>The remote object is unavailable.</p>
{% elif error.value == "UNAUTHORIZED" %}
<p>Missing permissions to fetch the remote object.</p>
{% elif error.value == "TIMEOUT" %} {% elif error.value == "TIMEOUT" %}
<p>Lookup timed out, please try refreshing the page.</p> <p>Lookup timed out, please try refreshing the page.</p>
{% else %} {% else %}