feat(error_handling): improve error payload serialization for JSON compatibility

This commit is contained in:
Abhimanyu Saharan
2026-02-12 20:10:30 +05:30
parent 032b77afb8
commit 8e72b7e0bc
2 changed files with 41 additions and 1 deletions

View File

@@ -38,6 +38,31 @@ def test_request_validation_error_includes_request_id():
assert resp.headers.get(REQUEST_ID_HEADER) == body["request_id"]
def test_request_validation_error_handles_bytes_input_without_500():
class Payload(BaseModel):
content: str
app = FastAPI()
install_error_handling(app)
@app.put("/needs-object")
def needs_object(payload: Payload) -> dict[str, str]:
return {"content": payload.content}
client = TestClient(app, raise_server_exceptions=False)
resp = client.put(
"/needs-object",
content=b"plain-text-body",
headers={"content-type": "text/plain"},
)
assert resp.status_code == 422
body = resp.json()
assert isinstance(body.get("detail"), list)
assert isinstance(body.get("request_id"), str) and body["request_id"]
assert resp.headers.get(REQUEST_ID_HEADER) == body["request_id"]
def test_http_exception_includes_request_id():
app = FastAPI()
install_error_handling(app)