Handle non-JSON API responses gracefully

Some boorus return empty/HTML responses for tag-limited queries.
All API clients now catch JSON parse errors and return empty
results instead of crashing.
This commit is contained in:
pax 2026-04-05 19:31:43 -05:00
parent c93cd9b97c
commit ee329519de
4 changed files with 17 additions and 4 deletions

View File

@ -29,7 +29,10 @@ class DanbooruClient(BooruClient):
if resp.status_code != 200:
log.warning(f" body: {resp.text[:500]}")
resp.raise_for_status()
try:
data = resp.json()
except Exception:
return []
# Some Danbooru forks wrap in {"posts": [...]}
if isinstance(data, dict):

View File

@ -49,7 +49,10 @@ class E621Client(BooruClient):
if resp.status_code != 200:
log.warning(f" body: {resp.text[:500]}")
resp.raise_for_status()
try:
data = resp.json()
except Exception:
return []
# e621 wraps posts in {"posts": [...]}
if isinstance(data, dict):

View File

@ -44,7 +44,11 @@ class GelbooruClient(BooruClient):
log.warning(f" body: {resp.text[:500]}")
resp.raise_for_status()
try:
data = resp.json()
except Exception:
log.warning(f" non-JSON response: {resp.text[:200]}")
return []
log.debug(f" json type: {type(data).__name__}, keys: {list(data.keys()) if isinstance(data, dict) else f'list[{len(data)}]'}")
# Gelbooru wraps posts in {"post": [...]} or returns {"post": []}
if isinstance(data, dict):

View File

@ -23,7 +23,10 @@ class MoebooruClient(BooruClient):
resp = await self.client.get(f"{self.base_url}/post.json", params=params)
resp.raise_for_status()
try:
data = resp.json()
except Exception:
return []
if isinstance(data, dict):
data = data.get("posts", data.get("post", []))
if not isinstance(data, list):