Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exception and error middleware #2590

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading