Gracefully handle errors related to response codes
This commit is contained in:
parent
688f0d17d8
commit
771cfadc23
1 changed files with 7 additions and 2 deletions
|
@ -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 {}
|
||||||
|
|
Loading…
Reference in a new issue