Coverage for tests / test_exception_handlers.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import pytest 1abcd

2from fastapi import Depends, FastAPI, HTTPException 1abcd

3from fastapi.exceptions import RequestValidationError 1abcd

4from fastapi.testclient import TestClient 1abcd

5from starlette.responses import JSONResponse 1abcd

6 

7 

8def http_exception_handler(request, exception): 1abcd

9 return JSONResponse({"exception": "http-exception"}) 1klm

10 

11 

12def request_validation_exception_handler(request, exception): 1abcd

13 return JSONResponse({"exception": "request-validation"}) 1nop

14 

15 

16def server_error_exception_handler(request, exception): 1abcd

17 return JSONResponse(status_code=500, content={"exception": "server-error"}) 1qherifsjg

18 

19 

20app = FastAPI( 1abcd

21 exception_handlers={ 

22 HTTPException: http_exception_handler, 

23 RequestValidationError: request_validation_exception_handler, 

24 Exception: server_error_exception_handler, 

25 } 

26) 

27 

28client = TestClient(app) 1abcd

29 

30 

31def raise_value_error(): 1abcd

32 raise ValueError() 1efg

33 

34 

35def dependency_with_yield(): 1abcd

36 yield raise_value_error() 1efg

37 

38 

39@app.get("/dependency-with-yield", dependencies=[Depends(dependency_with_yield)]) 1abcd

40def with_yield(): ... 1abcd

41 

42 

43@app.get("/http-exception") 1abcd

44def route_with_http_exception(): 1abcd

45 raise HTTPException(status_code=400) 1klm

46 

47 

48@app.get("/request-validation/{param}/") 1abcd

49def route_with_request_validation_exception(param: int): 1abcd

50 pass # pragma: no cover 

51 

52 

53@app.get("/server-error") 1abcd

54def route_with_server_error(): 1abcd

55 raise RuntimeError("Oops!") 1qhrisj

56 

57 

58def test_override_http_exception(): 1abcd

59 response = client.get("/http-exception") 1klm

60 assert response.status_code == 200 1klm

61 assert response.json() == {"exception": "http-exception"} 1klm

62 

63 

64def test_override_request_validation_exception(): 1abcd

65 response = client.get("/request-validation/invalid") 1nop

66 assert response.status_code == 200 1nop

67 assert response.json() == {"exception": "request-validation"} 1nop

68 

69 

70def test_override_server_error_exception_raises(): 1abcd

71 with pytest.raises(RuntimeError): 1qrs

72 client.get("/server-error") 1qrs

73 

74 

75def test_override_server_error_exception_response(): 1abcd

76 client = TestClient(app, raise_server_exceptions=False) 1hij

77 response = client.get("/server-error") 1hij

78 assert response.status_code == 500 1hij

79 assert response.json() == {"exception": "server-error"} 1hij

80 

81 

82def test_traceback_for_dependency_with_yield(): 1abcd

83 client = TestClient(app, raise_server_exceptions=True) 1efg

84 with pytest.raises(ValueError) as exc_info: 1efg

85 client.get("/dependency-with-yield") 1efg

86 last_frame = exc_info.traceback[-1] 1efg

87 assert str(last_frame.path) == __file__ 1efg

88 assert last_frame.lineno == raise_value_error.__code__.co_firstlineno 1efg