feat: improve error handling in HTTP requests and database fixture loading
This commit is contained in:
@@ -2,12 +2,26 @@ import aiohttp
|
||||
import asyncio
|
||||
|
||||
|
||||
def _body_preview(body: str, limit: int = 256) -> str:
|
||||
if not body:
|
||||
return ""
|
||||
preview = body[:limit].strip()
|
||||
if len(body) > limit:
|
||||
return f"{preview}..."
|
||||
return preview
|
||||
|
||||
|
||||
async def fetch(session, url, httpc_params: dict = {}):
|
||||
async with session.get(url, **httpc_params.get("connection", {})) as response:
|
||||
body = await response.text()
|
||||
if response.status != 200: # pragma: no cover
|
||||
# response.raise_for_status()
|
||||
return ""
|
||||
return await response.text()
|
||||
raise RuntimeError(
|
||||
"HTTP GET failed for "
|
||||
f"{url}: status={response.status} "
|
||||
f"content_type={response.headers.get('Content-Type', '')} "
|
||||
f"body_preview={_body_preview(body)}"
|
||||
)
|
||||
return body
|
||||
|
||||
|
||||
async def fetch_all(session, urls, httpc_params):
|
||||
|
||||
Reference in New Issue
Block a user