mirror of
https://github.com/NousResearch/atropos.git
synced 2026-04-24 17:04:55 +00:00
replace await resp.json() with await parse_http_response(resp)
This commit is contained in:
parent
a8b59ccc9b
commit
e06469f8c2
5 changed files with 52 additions and 6 deletions
39
atroposlib/utils/io.py
Normal file
39
atroposlib/utils/io.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import logging
|
||||
from typing import Any, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def parse_http_response(
|
||||
resp: Any, logger: Optional[logging.Logger] = None
|
||||
) -> Any:
|
||||
"""
|
||||
Parse an HTTP response with proper error handling and logging.
|
||||
|
||||
Args:
|
||||
resp: The HTTP response object (must have raise_for_status() and json() methods)
|
||||
logger: Optional logger instance. If not provided, uses the default module logger.
|
||||
|
||||
Returns:
|
||||
The parsed JSON response
|
||||
|
||||
Raises:
|
||||
Exception: Re-raises any exceptions that occur during parsing
|
||||
"""
|
||||
if logger is None:
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
try:
|
||||
# Raise an exception for bad status codes (4xx or 5xx)
|
||||
resp.raise_for_status()
|
||||
# Attempt to parse the response as JSON
|
||||
return await resp.json()
|
||||
except Exception as e:
|
||||
# Handle HTTP errors (raised by raise_for_status)
|
||||
error_text = await resp.text() # Read the response text for logging
|
||||
logger.error(
|
||||
f"Error fetching from server. Status: {getattr(e, 'status', 'unknown')}, "
|
||||
f"Message: {getattr(e, 'message', str(e))}. Response: {error_text}"
|
||||
)
|
||||
# Re-raise the exception to allow retry decorators to handle it
|
||||
raise
|
||||
Loading…
Add table
Add a link
Reference in a new issue