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):
|
||||
|
||||
@@ -39,7 +39,15 @@ def get_http_url(urls: list, httpc_params: dict = {}) -> list:
|
||||
responses = []
|
||||
for i in urls:
|
||||
res = requests.get(i, **httpc_params) # nosec - B113
|
||||
responses.append(res.content.decode())
|
||||
body = res.content.decode()
|
||||
if res.status_code != 200:
|
||||
raise RuntimeError(
|
||||
"HTTP GET failed for "
|
||||
f"{i}: status={res.status_code} "
|
||||
f"content_type={res.headers.get('Content-Type', '')} "
|
||||
f"body_preview={body[:256].strip()}"
|
||||
)
|
||||
responses.append(body)
|
||||
else:
|
||||
responses = asyncio.run(http_get(urls, httpc_params)) # pragma: no cover
|
||||
return responses
|
||||
|
||||
Reference in New Issue
Block a user