Skip to content

Instantly share code, notes, and snippets.

@wshayes
Created October 28, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wshayes/312b956e49b58a6372bd2f51a4e641e4 to your computer and use it in GitHub Desktop.
Save wshayes/312b956e49b58a6372bd2f51a4e641e4 to your computer and use it in GitHub Desktop.
[Route dependencies] #fastapi
# 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