File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010from contextlib import asynccontextmanager
1111from pathlib import Path
1212
13- from fastapi import FastAPI
13+ from fastapi import FastAPI , Request
1414from fastapi .middleware .cors import CORSMiddleware
15+ from fastapi .responses import JSONResponse
1516from fastapi .staticfiles import StaticFiles
1617
18+ from .controllers .errors import DomainError
1719from .controllers .manager import get_controller
1820from .infra .config import get_settings
1921from .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 )
You can’t perform that action at this time.
0 commit comments