Fix logging and surface unhandled exceptions
- basicConfig so the pops logger actually emits output - Global exception handler logs full traceback + returns detail in 500 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
18
app/main.py
18
app/main.py
@@ -1,12 +1,17 @@
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.db import check_db, run_migrations
|
||||
from app.routers import filaments, pricing, print_jobs, printers, todos
|
||||
from app.routers import ui
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)-8s %(name)s: %(message)s",
|
||||
)
|
||||
logger = logging.getLogger("pops")
|
||||
|
||||
|
||||
@@ -18,7 +23,7 @@ async def lifespan(app: FastAPI):
|
||||
try:
|
||||
run_migrations()
|
||||
except Exception:
|
||||
logger.exception("Migration failed — app will start but DB schema may be out of date")
|
||||
logger.exception("Migration failed — schema may be out of date")
|
||||
else:
|
||||
logger.warning("Database unreachable: %s", msg)
|
||||
yield
|
||||
@@ -26,6 +31,13 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
app = FastAPI(title="POPS", lifespan=lifespan)
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unhandled_exception(request: Request, exc: Exception):
|
||||
logger.exception("Unhandled error on %s %s", request.method, request.url)
|
||||
return JSONResponse(status_code=500, content={"detail": str(exc)})
|
||||
|
||||
|
||||
# JSON API
|
||||
app.include_router(printers.router, prefix="/api")
|
||||
app.include_router(filaments.router, prefix="/api")
|
||||
@@ -33,7 +45,7 @@ app.include_router(print_jobs.router, prefix="/api")
|
||||
app.include_router(todos.router, prefix="/api")
|
||||
app.include_router(pricing.router, prefix="/api")
|
||||
|
||||
# Web UI (must come after API to avoid route shadowing)
|
||||
# Web UI
|
||||
app.include_router(ui.router)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user