Coverage for tests/test_exception_handlers.py: 100%
50 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import pytest 1abcdef
2from fastapi import Depends, FastAPI, HTTPException 1abcdef
3from fastapi.exceptions import RequestValidationError 1abcdef
4from fastapi.testclient import TestClient 1abcdef
5from starlette.responses import JSONResponse 1abcdef
8def http_exception_handler(request, exception): 1abcdef
9 return JSONResponse({"exception": "http-exception"}) 1stuvwx
12def request_validation_exception_handler(request, exception): 1abcdef
13 return JSONResponse({"exception": "request-validation"}) 1yzABCD
16def server_error_exception_handler(request, exception): 1abcdef
17 return JSONResponse(status_code=500, content={"exception": "server-error"}) 1EmgFnhGoiHpjIqkJrl
20app = FastAPI( 1abcdef
21 exception_handlers={
22 HTTPException: http_exception_handler,
23 RequestValidationError: request_validation_exception_handler,
24 Exception: server_error_exception_handler,
25 }
26)
28client = TestClient(app) 1abcdef
31def raise_value_error(): 1abcdef
32 raise ValueError() 1ghijkl
35def dependency_with_yield(): 1abcdef
36 yield raise_value_error() 1ghijkl
39@app.get("/dependency-with-yield", dependencies=[Depends(dependency_with_yield)]) 1abcdef
40def with_yield(): ... 1abcdef
43@app.get("/http-exception") 1abcdef
44def route_with_http_exception(): 1abcdef
45 raise HTTPException(status_code=400) 1stuvwx
48@app.get("/request-validation/{param}/") 1abcdef
49def route_with_request_validation_exception(param: int): 1abcdef
50 pass # pragma: no cover
53@app.get("/server-error") 1abcdef
54def route_with_server_error(): 1abcdef
55 raise RuntimeError("Oops!") 1EmFnGoHpIqJr
58def test_override_http_exception(): 1abcdef
59 response = client.get("/http-exception") 1stuvwx
60 assert response.status_code == 200 1stuvwx
61 assert response.json() == {"exception": "http-exception"} 1stuvwx
64def test_override_request_validation_exception(): 1abcdef
65 response = client.get("/request-validation/invalid") 1yzABCD
66 assert response.status_code == 200 1yzABCD
67 assert response.json() == {"exception": "request-validation"} 1yzABCD
70def test_override_server_error_exception_raises(): 1abcdef
71 with pytest.raises(RuntimeError): 1EFGHIJ
72 client.get("/server-error") 1EFGHIJ
75def test_override_server_error_exception_response(): 1abcdef
76 client = TestClient(app, raise_server_exceptions=False) 1mnopqr
77 response = client.get("/server-error") 1mnopqr
78 assert response.status_code == 500 1mnopqr
79 assert response.json() == {"exception": "server-error"} 1mnopqr
82def test_traceback_for_dependency_with_yield(): 1abcdef
83 client = TestClient(app, raise_server_exceptions=True) 1ghijkl
84 with pytest.raises(ValueError) as exc_info: 1ghijkl
85 client.get("/dependency-with-yield") 1ghijkl
86 last_frame = exc_info.traceback[-1] 1ghijkl
87 assert str(last_frame.path) == __file__ 1ghijkl
88 assert last_frame.lineno == raise_value_error.__code__.co_firstlineno 1ghijkl