Gracefully handle errors related to response codes

This commit is contained in:
Nexus 2024-02-22 18:46:18 +00:00
parent 688f0d17d8
commit 771cfadc23
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -261,8 +261,13 @@ def preview_url(
response = client.send(response.next_request) response = client.send(response.next_request)
if response.status_code not in range(200, 400): if response.status_code not in range(200, 400):
response.raise_for_status() response.raise_for_status()
except httpx.HTTPError as e: except httpx.HTTPStatusError as e:
raise HTTPException(500, f"Failed to fetch URL: {e!r}") if e.response.status_code in (204, 400, 401, 403, 405, 429, 410):
return JSONResponse({}, e.response.status_code, {"Cache-Control": "no-store"})
raise HTTPException(
e.response.status_code,
f"Failed to fetch {e.response.url} - HTTP {e.response.status_code}: {e.response.text}"
)
if "text/html" not in response.headers.get("content-type", ""): if "text/html" not in response.headers.get("content-type", ""):
return {} return {}