Add error handling for admin lookups

This commit is contained in:
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
class FetchErrorTypeEnum(str, enum.Enum):
TIMEOUT = "TIMEOUT"
NOT_FOUND = "NOT_FOUND"
INTERNAL_ERROR = "INTERNAL_ERROR"
class VisibilityEnum(str, enum.Enum):
PUBLIC = "public"
UNLISTED = "unlisted"

View file

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

View file

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

View file

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

View file

@ -16,6 +16,18 @@
</form>
</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 %}
{{ utils.display_actor(ap_object, actors_metadata) }}
{% elif ap_object %}

View file

@ -297,12 +297,12 @@
<p class="p-summary">{{ object.summary | clean_html(object) | safe }}</p>
{% 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 %}
<div class="e-content">