Fix server erroring when homeserver is unavailable

This commit is contained in:
Nexus 2024-06-27 14:40:26 +01:00
parent 0af19ceb2f
commit d55ceb2df2

View file

@ -271,10 +271,13 @@ def preview_url(
else: else:
return MISSING_TOKEN return MISSING_TOKEN
try:
response = httpx.get( response = httpx.get(
domain + "/_matrix/client/r0/account/whoami", domain + "/_matrix/client/r0/account/whoami",
headers={"Authorization": f"Bearer {access_token}"} headers={"Authorization": f"Bearer {access_token}"}
) )
except httpx.NetworkError as e:
raise HTTPException(504, detail=f"Failed to contact homeserver for authentication: {e}")
if response.status_code != 200: if response.status_code != 200:
return INVALID_TOKEN return INVALID_TOKEN
@ -334,7 +337,7 @@ def preview_url(
e.response.status_code, e.response.status_code,
f"Failed to fetch {e.response.url} - HTTP {e.response.status_code}: {e.response.text}" f"Failed to fetch {e.response.url} - HTTP {e.response.status_code}: {e.response.text}"
) )
except httpx.NetworkError as e: except (httpx.NetworkError, ConnectionError) as e:
logging.debug(f"Failed to fetch {url}", exc_info=True) logging.debug(f"Failed to fetch {url}", exc_info=True)
raise HTTPException(502, f"Failed to fetch {url} - {e}") raise HTTPException(502, f"Failed to fetch {url} - {e}")