async def test_healthz(client): response = await client.get("/healthz") assert response.status_code == 200 assert response.headers["X-Request-Id"] async def test_unknown_route_uses_error_envelope_for_auth(client): response = await client.get("/v1/me") assert response.status_code in (401, 404) async def test_routing_errors_use_the_error_envelope(client): response = await client.get("/v1/does-not-exist") assert response.status_code == 404 assert response.json()["error"]["code"] == "NOT_FOUND" response = await client.delete("/v1/auth/register") assert response.status_code == 405 assert response.json()["error"]["code"] == "METHOD_NOT_ALLOWED" async def test_openapi_documents_the_real_error_envelope_and_report(client): spec = (await client.get("/openapi.json")).json() schemas = spec["components"]["schemas"] assert "HTTPValidationError" not in schemas and "ErrorResponse" in schemas assert all("422" not in op["responses"] for ops in spec["paths"].values() for op in ops.values()) report_op = spec["paths"]["/v1/sessions/{session_id}/swing-report"]["post"] assert report_op["requestBody"]["content"]["application/json"]["schema"]["$ref"].endswith("SwingAnalysisPayload") assert schemas["SessionOut"]["properties"]["report"]["anyOf"][0]["$ref"].endswith("SwingReport") def _raise(exc: Exception): async def endpoint(): raise exc return endpoint async def test_database_busy_and_bugs_use_the_envelope_with_request_id(app, client): from sqlalchemy.exc import DBAPIError from sqlalchemy.exc import TimeoutError as PoolTimeout busy = DBAPIError("SELECT 1", {}, Exception("(EMAXCONNSESSION) max clients reached in session mode")) app.add_api_route("/_test/db-busy", _raise(busy)) app.add_api_route("/_test/pool-timeout", _raise(PoolTimeout("QueuePool limit reached"))) app.add_api_route("/_test/bug", _raise(RuntimeError("boom"))) for path, status, code in (("/_test/db-busy", 503, "UNAVAILABLE"), ("/_test/pool-timeout", 503, "UNAVAILABLE"), ("/_test/bug", 500, "INTERNAL")): response = await client.get(path, headers={"X-Request-Id": "rid-1"}) assert response.status_code == status, path assert response.json()["error"]["code"] == code assert response.headers["X-Request-Id"] == "rid-1"