Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
handle WyvernError in except handler
Browse files Browse the repository at this point in the history
  • Loading branch information
wintonzheng committed Oct 6, 2023
1 parent 57ad802 commit 5a9e98d
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions wyvern/web_frameworks/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +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):
json = await request.json()
logger.warning(f"wyvern_error={exc} request_payload={json}")
return JSONResponse(
status_code=400,
content={
"detail": str(exc),
"error_code": exc.error_code,
},
)

@self.app.exception_handler(ValidationError)
async def pydantic_validation_error_handler(
request: Request,
exc: ValidationError,
):
json = await request.json()
logger.exception(f"Validation error error={exc} request_payload={json}")
raise HTTPException(status_code=422, detail=exc.errors())

@self.app.get("/healthcheck")
async def healthcheck() -> Dict[str, str]:
return {"status": "OK"}
Expand Down Expand Up @@ -189,6 +168,21 @@ async def post(

# profiler.stop()
# profiler.print(show_all=True)
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))
finally:
request_context.reset()
if not output:
Expand Down

0 comments on commit 5a9e98d

Please sign in to comment.