| 1 |
import pytest |
| 2 |
from fastapi import Depends, FastAPI, HTTPException |
| 3 |
from fastapi.exceptions import RequestValidationError |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from starlette.responses import JSONResponse |
| 6 |
|
| 7 |
|
| 8 |
def http_exception_handler(request, exception): |
| 9 |
return JSONResponse({"exception": "http-exception"}) |
| 10 |
|
| 11 |
|
| 12 |
def request_validation_exception_handler(request, exception): |
| 13 |
return JSONResponse({"exception": "request-validation"}) |
| 14 |
|
| 15 |
|
| 16 |
def server_error_exception_handler(request, exception): |
| 17 |
return JSONResponse(status_code=500, content={"exception": "server-error"}) |
| 18 |
|
| 19 |
|
| 20 |
app = FastAPI( |
| 21 |
exception_handlers={ |
| 22 |
HTTPException: http_exception_handler, |
| 23 |
RequestValidationError: request_validation_exception_handler, |
| 24 |
Exception: server_error_exception_handler, |
| 25 |
} |
| 26 |
) |
| 27 |
|
| 28 |
client = TestClient(app) |
| 29 |
|
| 30 |
|
| 31 |
def raise_value_error(): |
| 32 |
raise ValueError() |
| 33 |
|
| 34 |
|
| 35 |
def dependency_with_yield(): |
| 36 |
yield raise_value_error() |
| 37 |
|
| 38 |
|
| 39 |
@app.get("/dependency-with-yield", dependencies=[Depends(dependency_with_yield)]) |
| 40 |
def with_yield(): ... |
| 41 |
|
| 42 |
|
| 43 |
@app.get("/http-exception") |
| 44 |
def route_with_http_exception(): |
| 45 |
raise HTTPException(status_code=400) |
| 46 |
|
| 47 |
|
| 48 |
@app.get("/request-validation/{param}/") |
| 49 |
def route_with_request_validation_exception(param: int): |
| 50 |
pass # pragma: no cover |
| 51 |
|
| 52 |
|
| 53 |
@app.get("/server-error") |
| 54 |
def route_with_server_error(): |
| 55 |
raise RuntimeError("Oops!") |
| 56 |
|
| 57 |
|
| 58 |
def test_override_http_exception(): |
| 59 |
response = client.get("/http-exception") |
| 60 |
assert response.status_code == 200 |
| 61 |
assert response.json() == {"exception": "http-exception"} |
| 62 |
|
| 63 |
|
| 64 |
def test_override_request_validation_exception(): |
| 65 |
response = client.get("/request-validation/invalid") |
| 66 |
assert response.status_code == 200 |
| 67 |
assert response.json() == {"exception": "request-validation"} |
| 68 |
|
| 69 |
|
| 70 |
def test_override_server_error_exception_raises(): |
| 71 |
with pytest.raises(RuntimeError): |
| 72 |
client.get("/server-error") |
| 73 |
|
| 74 |
|
| 75 |
def test_override_server_error_exception_response(): |
| 76 |
client = TestClient(app, raise_server_exceptions=False) |
| 77 |
response = client.get("/server-error") |
| 78 |
assert response.status_code == 500 |
| 79 |
assert response.json() == {"exception": "server-error"} |
| 80 |
|
| 81 |
|
| 82 |
def test_traceback_for_dependency_with_yield(): |
| 83 |
client = TestClient(app, raise_server_exceptions=True) |
| 84 |
with pytest.raises(ValueError) as exc_info: |
| 85 |
client.get("/dependency-with-yield") |
| 86 |
last_frame = exc_info.traceback[-1] |
| 87 |
assert str(last_frame.path) == __file__ |
| 88 |
assert last_frame.lineno == raise_value_error.__code__.co_firstlineno |