Add error handling for admin lookups

main
Thomas Sileo 2022-07-26 18:51:20 +02:00
parent 24f3f94056
commit c07d17ba9b
6 changed files with 45 additions and 13 deletions

View File

@ -55,6 +55,12 @@ class ObjectNotFoundError(Exception):
pass pass
class FetchErrorTypeEnum(str, enum.Enum):
TIMEOUT = "TIMEOUT"
NOT_FOUND = "NOT_FOUND"
INTERNAL_ERROR = "INTERNAL_ERROR"
class VisibilityEnum(str, enum.Enum): class VisibilityEnum(str, enum.Enum):
PUBLIC = "public" PUBLIC = "public"
UNLISTED = "unlisted" UNLISTED = "unlisted"

View File

@ -1,3 +1,4 @@
import httpx
from fastapi import APIRouter from fastapi import APIRouter
from fastapi import Cookie from fastapi import Cookie
from fastapi import Depends from fastapi import Depends
@ -78,6 +79,7 @@ async def get_lookup(
query: str | None = None, query: str | None = None,
db_session: AsyncSession = Depends(get_db_session), db_session: AsyncSession = Depends(get_db_session),
) -> templates.TemplateResponse | RedirectResponse: ) -> templates.TemplateResponse | RedirectResponse:
error = None
ap_object = None ap_object = None
actors_metadata = {} actors_metadata = {}
if query: if query:
@ -94,16 +96,24 @@ async def get_lookup(
) )
# TODO(ts): redirect to admin_profile if the actor is in DB # TODO(ts): redirect to admin_profile if the actor is in DB
ap_object = await lookup(db_session, query) try:
if ap_object.ap_type in ap.ACTOR_TYPES: ap_object = await lookup(db_session, query)
actors_metadata = await get_actors_metadata( except httpx.TimeoutException:
db_session, [ap_object] # type: ignore 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: else:
actors_metadata = await get_actors_metadata( if ap_object.ap_type in ap.ACTOR_TYPES:
db_session, [ap_object.actor] # type: ignore actors_metadata = await get_actors_metadata(
) db_session, [ap_object] # type: ignore
print(ap_object) )
else:
actors_metadata = await get_actors_metadata(
db_session, [ap_object.actor] # type: ignore
)
return await templates.render_template( return await templates.render_template(
db_session, db_session,
request, request,
@ -112,6 +122,7 @@ async def get_lookup(
"query": query, "query": query,
"ap_object": ap_object, "ap_object": ap_object,
"actors_metadata": actors_metadata, "actors_metadata": actors_metadata,
"error": error,
}, },
) )

View File

@ -79,7 +79,6 @@ _RESIZED_CACHE: MutableMapping[tuple[str, int], tuple[bytes, str, Any]] = LFUCac
# - prevent double accept/double follow # - prevent double accept/double follow
# - UI support for updating posts # - UI support for updating posts
# - Article support # - Article support
# - Fix tests
# - Fix SQL tx in the codebase # - Fix SQL tx in the codebase
# - indieauth tweaks # - indieauth tweaks
# - API for posting notes # - API for posting notes

View File

@ -256,6 +256,10 @@ nav.flexbox {
border: 2px dashed $secondary-color; border: 2px dashed $secondary-color;
} }
.error-box {
color: $secondary-color;
}
.actor-action { .actor-action {
margin-top:20px; margin-top:20px;
margin-bottom:-20px; margin-bottom:-20px;

View File

@ -16,6 +16,18 @@
</form> </form>
</div> </div>
{% if error %}
<div class="box error-box">
{% if error.value == "NOT_FOUND" %}
<p>The remote object was deleted.</p>
{% elif error.value == "TIMEOUT" %}
<p>Lookup timed out, please try refreshing the page.</p>
{% else %}
<p>Unexpected error, please check the logs and report an issue if needed.</p>
{% endif %}
</div>
{% endif %}
{% if ap_object and ap_object.ap_type in actor_types %} {% if ap_object and ap_object.ap_type in actor_types %}
{{ utils.display_actor(ap_object, actors_metadata) }} {{ utils.display_actor(ap_object, actors_metadata) }}
{% elif ap_object %} {% elif ap_object %}

View File

@ -297,12 +297,12 @@
<p class="p-summary">{{ object.summary | clean_html(object) | safe }}</p> <p class="p-summary">{{ object.summary | clean_html(object) | safe }}</p>
{% endif %} {% 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) }} {{ show_more_button(object.permalink_id) }}
{% endif %} {% endif %}
{% if not object.sensitive or (object.sensitive and object.summary and object.permalink_id in request.query_params.getlist("show_more")) %} {% if not object.sensitive or (object.sensitive and object.permalink_id in request.query_params.getlist("show_more")) %}
{% if object.sensitive and object.summary %} {% if object.sensitive %}
{{ show_less_button(object.permalink_id) }} {{ show_less_button(object.permalink_id) }}
{% endif %} {% endif %}
<div class="e-content"> <div class="e-content">