Files
DeepTutor/tests/api/test_main_exception_handler.py
evan188199-tech a8d41bd4fa fix(api): return JSON for unhandled exceptions (#928)
A catch-all handler so 500s carry a JSON body instead of Starlette's plain-text default.
2026-08-19 23:36:18 +08:00

22 lines
695 B
Python

from __future__ import annotations
from fastapi.testclient import TestClient
def test_unhandled_exception_returns_json_error() -> None:
from deeptutor.api.main import app
@app.get("/api/v1/__test_unhandled_exception__")
def _raise_unhandled() -> None:
raise RuntimeError("intentional test failure")
client = TestClient(app, raise_server_exceptions=False)
response = client.get("/api/v1/__test_unhandled_exception__")
assert response.status_code == 500
assert response.headers["content-type"].startswith("application/json")
assert response.json() == {
"detail": "RuntimeError: intentional test failure",
"type": "RuntimeError",
}