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

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

6 

7 

8def http_exception_handler(request, exception): 1abcdef

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

10 

11 

12def request_validation_exception_handler(request, exception): 1abcdef

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

14 

15 

16def server_error_exception_handler(request, exception): 1abcdef

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

18 

19 

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) 

27 

28client = TestClient(app) 1abcdef

29 

30 

31def raise_value_error(): 1abcdef

32 raise ValueError() 1ghijkl

33 

34 

35def dependency_with_yield(): 1abcdef

36 yield raise_value_error() 1ghijkl

37 

38 

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

40def with_yield(): ... 1abcdef

41 

42 

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

44def route_with_http_exception(): 1abcdef

45 raise HTTPException(status_code=400) 1stuvwx

46 

47 

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

49def route_with_request_validation_exception(param: int): 1abcdef

50 pass # pragma: no cover 

51 

52 

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

54def route_with_server_error(): 1abcdef

55 raise RuntimeError("Oops!") 1EmFnGoHpIqJr

56 

57 

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

62 

63 

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

68 

69 

70def test_override_server_error_exception_raises(): 1abcdef

71 with pytest.raises(RuntimeError): 1EFGHIJ

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

73 

74 

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

80 

81 

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