Created
October 28, 2019 14:43
-
-
Save wshayes/312b956e49b58a6372bd2f51a4e641e4 to your computer and use it in GitHub Desktop.
[Route dependencies] #fastapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://gitter.im/tiangolo/fastapi?at=5db608eeef84ab3786aba5a4 | |
from fastapi import FastAPI, Depends | |
from starlette.testclient import TestClient | |
app = FastAPI() | |
def capture_exception(exc: Exception) -> None: | |
print(str(exc)) | |
async def log_exceptions(): | |
try: | |
yield | |
except Exception as e: | |
capture_exception(e) | |
raise | |
@app.get("/", dependencies=[Depends(log_exceptions)]) | |
def endpoint(): | |
raise ValueError("Success!") | |
client = TestClient(app, raise_server_exceptions=False) | |
client.get("/") | |
# Success! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment