From c07d17ba9ba79d08c9928b96914626efdfa45d52 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Tue, 26 Jul 2022 18:51:20 +0200 Subject: [PATCH] Add error handling for admin lookups --- app/activitypub.py | 6 ++++++ app/admin.py | 29 ++++++++++++++++++++--------- app/main.py | 1 - app/scss/main.scss | 4 ++++ app/templates/lookup.html | 12 ++++++++++++ app/templates/utils.html | 6 +++--- 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/app/activitypub.py b/app/activitypub.py index fcd48e7..60a6f9c 100644 --- a/app/activitypub.py +++ b/app/activitypub.py @@ -55,6 +55,12 @@ class ObjectNotFoundError(Exception): pass +class FetchErrorTypeEnum(str, enum.Enum): + TIMEOUT = "TIMEOUT" + NOT_FOUND = "NOT_FOUND" + INTERNAL_ERROR = "INTERNAL_ERROR" + + class VisibilityEnum(str, enum.Enum): PUBLIC = "public" UNLISTED = "unlisted" diff --git a/app/admin.py b/app/admin.py index 4212845..465808b 100644 --- a/app/admin.py +++ b/app/admin.py @@ -1,3 +1,4 @@ +import httpx from fastapi import APIRouter from fastapi import Cookie from fastapi import Depends @@ -78,6 +79,7 @@ async def get_lookup( query: str | None = None, db_session: AsyncSession = Depends(get_db_session), ) -> templates.TemplateResponse | RedirectResponse: + error = None ap_object = None actors_metadata = {} if query: @@ -94,16 +96,24 @@ async def get_lookup( ) # TODO(ts): redirect to admin_profile if the actor is in DB - ap_object = await lookup(db_session, query) - if ap_object.ap_type in ap.ACTOR_TYPES: - actors_metadata = await get_actors_metadata( - db_session, [ap_object] # type: ignore - ) + try: + ap_object = await lookup(db_session, query) + except httpx.TimeoutException: + error = ap.FetchErrorTypeEnum.TIMEOUT + except (ap.ObjectNotFoundError, ap.ObjectIsGoneError): + error = ap.FetchErrorTypeEnum.NOT_FOUND + except Exception: + logger.exception(f"Failed to lookup {query}") + error = ap.FetchErrorTypeEnum.INTERNAL_ERROR else: - actors_metadata = await get_actors_metadata( - db_session, [ap_object.actor] # type: ignore - ) - print(ap_object) + if ap_object.ap_type in ap.ACTOR_TYPES: + actors_metadata = await get_actors_metadata( + db_session, [ap_object] # type: ignore + ) + else: + actors_metadata = await get_actors_metadata( + db_session, [ap_object.actor] # type: ignore + ) return await templates.render_template( db_session, request, @@ -112,6 +122,7 @@ async def get_lookup( "query": query, "ap_object": ap_object, "actors_metadata": actors_metadata, + "error": error, }, ) diff --git a/app/main.py b/app/main.py index 22f43ce..e11a786 100644 --- a/app/main.py +++ b/app/main.py @@ -79,7 +79,6 @@ _RESIZED_CACHE: MutableMapping[tuple[str, int], tuple[bytes, str, Any]] = LFUCac # - prevent double accept/double follow # - UI support for updating posts # - Article support -# - Fix tests # - Fix SQL tx in the codebase # - indieauth tweaks # - API for posting notes diff --git a/app/scss/main.scss b/app/scss/main.scss index fbcbd0f..a88e1a3 100644 --- a/app/scss/main.scss +++ b/app/scss/main.scss @@ -256,6 +256,10 @@ nav.flexbox { border: 2px dashed $secondary-color; } +.error-box { + color: $secondary-color; +} + .actor-action { margin-top:20px; margin-bottom:-20px; diff --git a/app/templates/lookup.html b/app/templates/lookup.html index 9526c35..0f3d2e1 100644 --- a/app/templates/lookup.html +++ b/app/templates/lookup.html @@ -16,6 +16,18 @@ + {% if error %} +
+ {% if error.value == "NOT_FOUND" %} +

The remote object was deleted.

+ {% elif error.value == "TIMEOUT" %} +

Lookup timed out, please try refreshing the page.

+ {% else %} +

Unexpected error, please check the logs and report an issue if needed.

+ {% endif %} +
+ {% endif %} + {% if ap_object and ap_object.ap_type in actor_types %} {{ utils.display_actor(ap_object, actors_metadata) }} {% elif ap_object %} diff --git a/app/templates/utils.html b/app/templates/utils.html index 2ea1d2c..874c11e 100644 --- a/app/templates/utils.html +++ b/app/templates/utils.html @@ -297,12 +297,12 @@

{{ object.summary | clean_html(object) | safe }}

{% endif %} - {% if object.sensitive and object.summary and object.permalink_id not in request.query_params.getlist("show_more") %} + {% if object.sensitive and object.permalink_id not in request.query_params.getlist("show_more") %} {{ show_more_button(object.permalink_id) }} {% endif %} - {% if not object.sensitive or (object.sensitive and object.summary and object.permalink_id in request.query_params.getlist("show_more")) %} - {% if object.sensitive and object.summary %} + {% if not object.sensitive or (object.sensitive and object.permalink_id in request.query_params.getlist("show_more")) %} + {% if object.sensitive %} {{ show_less_button(object.permalink_id) }} {% endif %}