Coverage for tests / test_exception_handlers.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +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"}) 1lmno

10 

11 

12def request_validation_exception_handler(request, exception): 1abcd

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

14 

15 

16def server_error_exception_handler(request, exception): 1abcd

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

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() 1efgh

33 

34 

35def dependency_with_yield(): 1abcd

36 yield raise_value_error() 1efgh

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) 1lmno

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!") 1tiuvjwk

56 

57 

58def test_override_http_exception(): 1abcd

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

60 assert response.status_code == 200 1lmno

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

62 

63 

64def test_override_request_validation_exception(): 1abcd

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

66 assert response.status_code == 200 1pqrs

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

68 

69 

70def test_override_server_error_exception_raises(): 1abcd

71 with pytest.raises(RuntimeError): 1tuvw

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

73 

74 

75def test_override_server_error_exception_response(): 1abcd

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

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

78 assert response.status_code == 500 1ixjk

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

80 

81 

82def test_traceback_for_dependency_with_yield(): 1abcd

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

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

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

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

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

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