mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
added more detailed logging to help catch errors
This commit is contained in:
parent
05118b749a
commit
d3ce1ceed8
1 changed files with 44 additions and 1 deletions
|
|
@ -1,12 +1,55 @@
|
|||
from fastapi import FastAPI, Depends
|
||||
import logging
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi import FastAPI, Depends, Request, status
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
from mangum import Mangum
|
||||
from backend.app.portfolio import router as portfolio_router
|
||||
from backend.app.plan import router as plan_router
|
||||
from backend.app.dependencies import validate_api_key
|
||||
from backend.app.config import get_settings
|
||||
|
||||
logger = logging.getLogger("uvicorn.error")
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
app = FastAPI(dependencies=[Depends(validate_api_key)])
|
||||
|
||||
|
||||
# Handle 422 errors (validation failures)
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||
logger.error(f"422 Validation Error at {request.url}")
|
||||
logger.error(f"Body: {exc.body}")
|
||||
logger.error(f"Validation Errors: {exc.errors()}")
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
content=jsonable_encoder({
|
||||
"detail": exc.errors(),
|
||||
"body": exc.body
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
# Handle generic HTTP exceptions (optional, useful for catching 404, 403, etc.)
|
||||
@app.exception_handler(StarletteHTTPException)
|
||||
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
||||
logger.warning(f"{exc.status_code} Error at {request.url} - Detail: {exc.detail}")
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={"detail": exc.detail},
|
||||
)
|
||||
|
||||
|
||||
# Middleware to log requests
|
||||
@app.middleware("http")
|
||||
async def log_requests(request: Request, call_next):
|
||||
logger.info(f"Incoming request: {request.method} {request.url}")
|
||||
response = await call_next(request)
|
||||
logger.info(f"Response status: {response.status_code}")
|
||||
return response
|
||||
|
||||
|
||||
app.include_router(portfolio_router.router, prefix="/v1")
|
||||
app.include_router(plan_router.router, prefix="/v1")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue