fixes and refactor
This commit is contained in:
@@ -46,6 +46,25 @@ def _get_correlation_id(request: Request) -> str:
|
||||
return str(uuid4())
|
||||
|
||||
|
||||
def _json_safe(value: Any) -> Any:
|
||||
"""Recursively convert non-JSON-serializable objects (like Exceptions) into strings.
|
||||
|
||||
Keeps overall structure intact so tests inspecting error details (e.g. 'loc', 'msg') still work.
|
||||
"""
|
||||
# Exception -> string message
|
||||
if isinstance(value, BaseException):
|
||||
return str(value)
|
||||
# Mapping types
|
||||
if isinstance(value, dict):
|
||||
return {k: _json_safe(v) for k, v in value.items()}
|
||||
# Sequence types
|
||||
if isinstance(value, (list, tuple)):
|
||||
return [
|
||||
_json_safe(v) for v in value
|
||||
]
|
||||
return value
|
||||
|
||||
|
||||
def _build_error_response(
|
||||
request: Request,
|
||||
*,
|
||||
@@ -66,7 +85,7 @@ def _build_error_response(
|
||||
"correlation_id": correlation_id,
|
||||
}
|
||||
if details is not None:
|
||||
body["error"]["details"] = details
|
||||
body["error"]["details"] = _json_safe(details)
|
||||
|
||||
response = JSONResponse(content=body, status_code=status_code)
|
||||
response.headers[ERROR_HEADER_NAME] = correlation_id
|
||||
|
||||
Reference in New Issue
Block a user