# 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!