Skip to content

Commit 89a0060

Browse files
committed
fix: separate unauthorized issue from internal server error
1 parent 099ee23 commit 89a0060

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

main.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import AsyncGenerator, Awaitable, Callable
1212

1313
import httpx
14-
from fastapi import FastAPI, Request
14+
from fastapi import FastAPI, HTTPException, Request
1515
from fastapi.middleware.cors import CORSMiddleware
1616
from starlette.responses import JSONResponse, Response
1717

@@ -69,7 +69,7 @@ async def api_authentication_middleware(
6969
# Store client_id in request state for later use
7070
request.state.client_id = client_id
7171
return await call_next(request)
72-
except Exception as e:
72+
except HTTPException as e:
7373
return JSONResponse(
7474
status_code=401,
7575
content={
@@ -78,6 +78,15 @@ async def api_authentication_middleware(
7878
"status": 401,
7979
},
8080
)
81+
except Exception as e:
82+
return JSONResponse(
83+
status_code=500,
84+
content={
85+
"error": "Internal Server Error",
86+
"message": str(e),
87+
"status": 500,
88+
},
89+
)
8190

8291

8392
# Include routers from different modules

0 commit comments

Comments
 (0)