Skip to content

Commit

Permalink
feat: exception and error middleware (langflow-ai#2590)
Browse files Browse the repository at this point in the history
exception and error middleware
  • Loading branch information
zzzming authored Jul 9, 2024
1 parent 4c43b4c commit ce9b4b0
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/backend/base/langflow/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from urllib.parse import urlencode

import nest_asyncio # type: ignore
from fastapi import FastAPI, Request, Response
from fastapi import FastAPI, Request, Response, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from http import HTTPStatus
from loguru import logger
from pydantic import PydanticDeprecatedSince20
from rich import print as rprint
Expand Down Expand Up @@ -158,6 +159,21 @@ async def flatten_query_string_lists(request: Request, call_next):
app.include_router(router)
app.include_router(health_check_router)

@app.exception_handler(Exception)
async def exception_handler(request: Request, exc: Exception):
if isinstance(exc, HTTPException):
logger.error(f"HTTPException: {exc.detail}")
return JSONResponse(
status_code=exc.status_code,
content={"message": str(exc.detail)},
)
else:
logger.error(f"unhandled error: {exc}")
return JSONResponse(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
content={"message": str(exc)},
)

FastAPIInstrumentor.instrument_app(app)

return app
Expand Down

0 comments on commit ce9b4b0

Please sign in to comment.