Improve L1 fetch error handling with better diagnostics

- Handle 404 explicitly with clear "not found" message
- Log actual response body when JSON parsing fails
- Include status code and body preview in error messages

This helps diagnose issues like empty responses or HTML error pages from L1.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
gilesb
2026-01-09 02:00:27 +00:00
parent 449ed0c100
commit 91d8093a1b

View File

@@ -1787,20 +1787,33 @@ async def record_run(req: RecordRunRequest, user: User = Depends(get_required_us
# Helper to fetch from L1 without blocking event loop
def fetch_l1_run():
logger.info(f"record_run: Fetching run from L1: {l1_url}/runs/{req.run_id}")
resp = requests.get(f"{l1_url}/runs/{req.run_id}", timeout=30)
url = f"{l1_url}/runs/{req.run_id}"
logger.info(f"record_run: Fetching run from L1: {url}")
resp = requests.get(url, timeout=30)
if resp.status_code == 404:
raise ValueError(f"Run not found on L1: {req.run_id}")
resp.raise_for_status()
return resp.json()
try:
return resp.json()
except Exception:
body_preview = resp.text[:200] if resp.text else "(empty)"
logger.error(f"L1 returned non-JSON for {url}: status={resp.status_code}, body={body_preview}")
raise ValueError(f"L1 returned invalid response: {body_preview[:100]}")
def fetch_l1_cache(content_hash):
logger.debug(f"record_run: Fetching cache {content_hash[:16]}... from L1")
resp = requests.get(
f"{l1_url}/cache/{content_hash}",
headers={"Accept": "application/json"},
timeout=10
)
url = f"{l1_url}/cache/{content_hash}"
resp = requests.get(url, headers={"Accept": "application/json"}, timeout=10)
if resp.status_code == 404:
raise ValueError(f"Cache item not found on L1: {content_hash[:16]}...")
resp.raise_for_status()
return resp.json()
try:
return resp.json()
except Exception as e:
# Log what we actually got back
body_preview = resp.text[:200] if resp.text else "(empty)"
logger.error(f"L1 returned non-JSON for {url}: status={resp.status_code}, body={body_preview}")
raise ValueError(f"L1 returned invalid response (status={resp.status_code}): {body_preview[:100]}")
# Fetch run from L1
try: