| 1 |
from fastapi import FastAPI, Request, WebSocket |
| 2 |
from fastapi.exceptions import ( |
| 3 |
RequestValidationError, |
| 4 |
ResponseValidationError, |
| 5 |
WebSocketRequestValidationError, |
| 6 |
) |
| 7 |
from fastapi.testclient import TestClient |
| 8 |
from pydantic import BaseModel |
| 9 |
|
| 10 |
|
| 11 |
class Item(BaseModel): |
| 12 |
id: int |
| 13 |
name: str |
| 14 |
|
| 15 |
|
| 16 |
class ExceptionCapture: |
| 17 |
def __init__(self): |
| 18 |
self.exception = None |
| 19 |
|
| 20 |
def capture(self, exc): |
| 21 |
self.exception = exc |
| 22 |
return exc |
| 23 |
|
| 24 |
|
| 25 |
app = FastAPI() |
| 26 |
sub_app = FastAPI() |
| 27 |
captured_exception = ExceptionCapture() |
| 28 |
|
| 29 |
app.mount(path="/sub", app=sub_app) |
| 30 |
|
| 31 |
|
| 32 |
@app.exception_handler(RequestValidationError) |
| 33 |
@sub_app.exception_handler(RequestValidationError) |
| 34 |
async def request_validation_handler(request: Request, exc: RequestValidationError): |
| 35 |
captured_exception.capture(exc) |
| 36 |
raise exc |
| 37 |
|
| 38 |
|
| 39 |
@app.exception_handler(ResponseValidationError) |
| 40 |
@sub_app.exception_handler(ResponseValidationError) |
| 41 |
async def response_validation_handler(_: Request, exc: ResponseValidationError): |
| 42 |
captured_exception.capture(exc) |
| 43 |
raise exc |
| 44 |
|
| 45 |
|
| 46 |
@app.exception_handler(WebSocketRequestValidationError) |
| 47 |
@sub_app.exception_handler(WebSocketRequestValidationError) |
| 48 |
async def websocket_validation_handler( |
| 49 |
websocket: WebSocket, exc: WebSocketRequestValidationError |
| 50 |
): |
| 51 |
captured_exception.capture(exc) |
| 52 |
raise exc |
| 53 |
|
| 54 |
|
| 55 |
@app.get("/users/{user_id}") |
| 56 |
def get_user(user_id: int): |
| 57 |
return {"user_id": user_id} # pragma: no cover |
| 58 |
|
| 59 |
|
| 60 |
@app.get("/items/", response_model=Item) |
| 61 |
def get_item(): |
| 62 |
return {"name": "Widget"} |
| 63 |
|
| 64 |
|
| 65 |
@sub_app.get("/items/", response_model=Item) |
| 66 |
def get_sub_item(): |
| 67 |
return {"name": "Widget"} # pragma: no cover |
| 68 |
|
| 69 |
|
| 70 |
@app.websocket("/ws/{item_id}") |
| 71 |
async def websocket_endpoint(websocket: WebSocket, item_id: int): |
| 72 |
await websocket.accept() # pragma: no cover |
| 73 |
await websocket.send_text(f"Item: {item_id}") # pragma: no cover |
| 74 |
await websocket.close() # pragma: no cover |
| 75 |
|
| 76 |
|
| 77 |
@sub_app.websocket("/ws/{item_id}") |
| 78 |
async def subapp_websocket_endpoint(websocket: WebSocket, item_id: int): |
| 79 |
await websocket.accept() # pragma: no cover |
| 80 |
await websocket.send_text(f"Item: {item_id}") # pragma: no cover |
| 81 |
await websocket.close() # pragma: no cover |
| 82 |
|
| 83 |
|
| 84 |
client = TestClient(app) |
| 85 |
|
| 86 |
|
| 87 |
def test_request_validation_error_includes_endpoint_context(): |
| 88 |
captured_exception.exception = None |
| 89 |
try: |
| 90 |
client.get("/users/invalid") |
| 91 |
except Exception: |
| 92 |
pass |
| 93 |
|
| 94 |
assert captured_exception.exception is not None |
| 95 |
error_str = str(captured_exception.exception) |
| 96 |
assert "get_user" in error_str |
| 97 |
assert "/users/" in error_str |
| 98 |
|
| 99 |
|
| 100 |
def test_response_validation_error_includes_endpoint_context(): |
| 101 |
captured_exception.exception = None |
| 102 |
try: |
| 103 |
client.get("/items/") |
| 104 |
except Exception: |
| 105 |
pass |
| 106 |
|
| 107 |
assert captured_exception.exception is not None |
| 108 |
error_str = str(captured_exception.exception) |
| 109 |
assert "get_item" in error_str |
| 110 |
assert "/items/" in error_str |
| 111 |
|
| 112 |
|
| 113 |
def test_websocket_validation_error_includes_endpoint_context(): |
| 114 |
captured_exception.exception = None |
| 115 |
try: |
| 116 |
with client.websocket_connect("/ws/invalid"): |
| 117 |
pass # pragma: no cover |
| 118 |
except Exception: |
| 119 |
pass |
| 120 |
|
| 121 |
assert captured_exception.exception is not None |
| 122 |
error_str = str(captured_exception.exception) |
| 123 |
assert "websocket_endpoint" in error_str |
| 124 |
assert "/ws/" in error_str |
| 125 |
|
| 126 |
|
| 127 |
def test_subapp_request_validation_error_includes_endpoint_context(): |
| 128 |
captured_exception.exception = None |
| 129 |
try: |
| 130 |
client.get("/sub/items/") |
| 131 |
except Exception: |
| 132 |
pass |
| 133 |
|
| 134 |
assert captured_exception.exception is not None |
| 135 |
error_str = str(captured_exception.exception) |
| 136 |
assert "get_sub_item" in error_str |
| 137 |
assert "/sub/items/" in error_str |
| 138 |
|
| 139 |
|
| 140 |
def test_subapp_websocket_validation_error_includes_endpoint_context(): |
| 141 |
captured_exception.exception = None |
| 142 |
try: |
| 143 |
with client.websocket_connect("/sub/ws/invalid"): |
| 144 |
pass # pragma: no cover |
| 145 |
except Exception: |
| 146 |
pass |
| 147 |
|
| 148 |
assert captured_exception.exception is not None |
| 149 |
error_str = str(captured_exception.exception) |
| 150 |
assert "subapp_websocket_endpoint" in error_str |
| 151 |
assert "/sub/ws/" in error_str |
| 152 |
|
| 153 |
|
| 154 |
def test_validation_error_with_only_path(): |
| 155 |
errors = [{"type": "missing", "loc": ("body", "name"), "msg": "Field required"}] |
| 156 |
exc = RequestValidationError(errors, endpoint_ctx={"path": "GET /api/test"}) |
| 157 |
error_str = str(exc) |
| 158 |
assert "Endpoint: GET /api/test" in error_str |
| 159 |
assert 'File "' not in error_str |
| 160 |
|
| 161 |
|
| 162 |
def test_validation_error_with_no_context(): |
| 163 |
errors = [{"type": "missing", "loc": ("body", "name"), "msg": "Field required"}] |
| 164 |
exc = RequestValidationError(errors, endpoint_ctx={}) |
| 165 |
error_str = str(exc) |
| 166 |
assert "1 validation error:" in error_str |
| 167 |
assert "Endpoint" not in error_str |
| 168 |
assert 'File "' not in error_str |