From 99571b8331b22f6d9bec1663c573b681f859ca29 Mon Sep 17 00:00:00 2001 From: Shuchang Zheng Date: Thu, 5 Oct 2023 17:57:21 -0700 Subject: [PATCH] shu/fix wyvern error handling (#83) * move error handling to fastapi framework * v0.0.26 * add wyvern error warning * handle WyvernError in except handler --- pyproject.toml | 2 +- wyvern/web_frameworks/fastapi.py | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 55a3f9f..4a7af71 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "wyvern-ai" -version = "0.0.25" +version = "0.0.26" description = "" authors = ["Wyvern AI "] readme = "README.md" diff --git a/wyvern/web_frameworks/fastapi.py b/wyvern/web_frameworks/fastapi.py index 07b4084..baba3d3 100644 --- a/wyvern/web_frameworks/fastapi.py +++ b/wyvern/web_frameworks/fastapi.py @@ -72,16 +72,6 @@ def __init__(self, host: str = "127.0.0.1", port: int = 8000) -> None: self.host = host self.port = port - @self.app.exception_handler(WyvernError) - async def wyvern_exception_handler(request: Request, exc: WyvernError): - return JSONResponse( - status_code=400, - content={ - "detail": str(exc), - "error_code": exc.error_code, - }, - ) - @self.app.get("/healthcheck") async def healthcheck() -> Dict[str, str]: return {"status": "OK"} @@ -181,6 +171,15 @@ async def post( except ValidationError as e: logger.exception(f"Validation error error={e} request_payload={json}") raise HTTPException(status_code=422, detail=e.errors()) + except WyvernError as e: + logger.warning(f"Wyvern error error={e} request_payload={json}") + return JSONResponse( + status_code=400, + content={ + "detail": str(e), + "error_code": e.error_code, + }, + ) except Exception as e: logger.exception(f"Unexpected error error={e} request_payload={json}") raise HTTPException(status_code=500, detail=str(e))