File tree Expand file tree Collapse file tree 3 files changed +22
-2
lines changed
examples/python/fastapi/postgres Expand file tree Collapse file tree 3 files changed +22
-2
lines changed Original file line number Diff line number Diff line change 1- from fastapi import FastAPI
1+ from fastapi import FastAPI , Request , status
2+ from fastapi .responses import JSONResponse
23from routes import router
34
45from custom_routes import router as custom_router
56
67app = FastAPI ()
78
9+ @app .exception_handler (Exception )
10+ async def exception_handler (request : Request , ex : Exception ):
11+ return JSONResponse (
12+ status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
13+ content = {"error" : "Internal Server Error" }
14+ )
15+
816app .include_router (router )
917
1018app .include_router (custom_router )
Original file line number Diff line number Diff line change 66@router .get ('/custom/route' )
77def customRoute ():
88 return { "custom" : True }
9+
10+ @router .get ('/custom/exception' )
11+ def customException ():
12+ raise RuntimeError ('expected error' )
Original file line number Diff line number Diff line change @@ -11,14 +11,22 @@ function removeExtension(filename) {
1111}
1212
1313- %>
14- from fastapi import FastAPI
14+ from fastapi import FastAPI, Request, status
15+ from fastapi.responses import JSONResponse
1516from routes import router
1617<% customRouteFilenames .forEach (filename => { % >
1718from < %= removeExtension (filename) % > import router as <%= fileName2routerName(filename) %>
1819< % }) - %>
1920
2021app = FastAPI()
2122
23+ @app.exception_handler(Exception)
24+ async def exception_handler(request: Request, ex: Exception):
25+ return JSONResponse(
26+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
27+ content={"error": "Internal Server Error"}
28+ )
29+
2230app.include_router(router)
2331<% customRouteFilenames .forEach (filename => { % >
2432app .include_router (< %= fileName2routerName (filename) % > )
You can’t perform that action at this time.
0 commit comments