Error is not being caught by FastAPI or Gunicorn

31 views Asked by At

I'm currently getting a 502 error from my FastAPI app after making concurrent requests to the endpoints, which are calling OpenAI endpoints. The problem is, that I can't see the errors in the logs, both at the route level and server level.

My team is currently using Apigee as a proxy. When I hit the endpoint directly without going through Apigee, it works fine, but I was told that the requests themselves are reaching the server.

I've enabled logging with Gunicorn, yet I can't see any of the errors. I'm also trying to catch any errors occurring outside the routes with the following:

async def catch_exceptions_middleware(request: Request, call_next):
    try:
        logger.error(f"{request.url}\t{request.query_params}")
        return await call_next(request)
    except Exception as e:
        logging.critical(e, exc_info=True)
        return JSONResponse(
            status_code=HTTP_500_INTERNAL_SERVER_ERROR,
            content=ErrorResponse(
                type="internal-error",
                title="Internal error",
                status=HTTP_500_INTERNAL_SERVER_ERROR,
                detail="An unexpected error has occured.",
                instance=request.url.path,
            ).model_dump(),
        )

The app layer looks like:

# app.py

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.base import BaseHTTPMiddleware

from src.routes import router

from src.exceptions import catch_exceptions_middleware


def create_app():
    app = FastAPI(title="test")
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_methods=["*"],
        allow_headers=["*"],
        expose_headers=["*"],
    )
    app.add_middleware(BaseHTTPMiddleware, dispatch=catch_exceptions_middleware)

    app.include_router(router)
    return app


app = create_app()
# routes.py

from fastapi import APIRouter, BackgroundTasks, Form
from fastapi.responses import StreamingResponse

from src.services import generator, not_so_heavy


router = APIRouter()


@router.get("/")
async def root():
    return {"message": "Hello World"}

Does anyone know any other way I might be able to trace this?

0

There are 0 answers