Skip to content

Commit a62a661

Browse files
authored
Update main.py
1 parent c96f95c commit a62a661

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

‎app/main.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
from contextlib import asynccontextmanager
1111
from pathlib import Path
1212

13-
from fastapi import FastAPI
13+
from fastapi import FastAPI, Request
1414
from fastapi.middleware.cors import CORSMiddleware
15+
from fastapi.responses import JSONResponse
1516
from fastapi.staticfiles import StaticFiles
1617

18+
from .controllers.errors import DomainError
1719
from .controllers.manager import get_controller
1820
from .infra.config import get_settings
1921
from .models.db import get_engine
@@ -36,6 +38,17 @@ def create_app() -> FastAPI:
3638
stacklevel=1,
3739
)
3840
app = FastAPI(title="python-agent-web", version="0.1.0", lifespan=lifespan)
41+
42+
@app.exception_handler(DomainError)
43+
def _domain_error(_request: Request, exc: DomainError) -> JSONResponse:
44+
"""Render a controller's refusal in FastAPI's own error shape.
45+
46+
With this, routes never translate business rules into status
47+
codes -- the controller names the failure, the status follows
48+
from its type.
49+
"""
50+
return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)
51+
3952
app.include_router(auth.router)
4053
app.include_router(conversations.router)
4154
app.include_router(billing.router)

0 commit comments

Comments
 (0)