web app handles pydantic validation error (#461)

This commit is contained in:
Kerem Yilmaz
2024-06-12 12:39:05 -07:00
committed by GitHub
parent 8e21c8b39e
commit 5d134d58d9

View File

@@ -3,9 +3,10 @@ from datetime import datetime
from typing import Awaitable, Callable from typing import Awaitable, Callable
import structlog import structlog
from fastapi import APIRouter, FastAPI, Response from fastapi import APIRouter, FastAPI, Response, status
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from pydantic import ValidationError
from starlette.requests import HTTPConnection, Request from starlette.requests import HTTPConnection, Request
from starlette_context.middleware import RawContextMiddleware from starlette_context.middleware import RawContextMiddleware
from starlette_context.plugins.base import Plugin from starlette_context.plugins.base import Plugin
@@ -78,6 +79,10 @@ def get_agent_app(router: APIRouter = base_router) -> FastAPI:
async def handle_skyvern_http_exception(request: Request, exc: SkyvernHTTPException) -> JSONResponse: async def handle_skyvern_http_exception(request: Request, exc: SkyvernHTTPException) -> JSONResponse:
return JSONResponse(status_code=exc.status_code, content={"detail": exc.message}) return JSONResponse(status_code=exc.status_code, content={"detail": exc.message})
@app.exception_handler(ValidationError)
async def handle_pydantic_validation_error(request: Request, exc: ValidationError) -> JSONResponse:
return JSONResponse(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content={"detail": str(exc)})
@app.exception_handler(Exception) @app.exception_handler(Exception)
async def unexpected_exception(request: Request, exc: Exception) -> JSONResponse: async def unexpected_exception(request: Request, exc: Exception) -> JSONResponse:
LOG.exception("Unexpected error in agent server.", exc_info=exc) LOG.exception("Unexpected error in agent server.", exc_info=exc)