Skip to content

Commit 04928a9

Browse files
committed
fix: improve error handling with proper HTTPException type checking
Replaces fragile hasattr() checks with explicit HTTPException type checking for 401 errors. Adds separate handling for non-HTTP exceptions with proper logging for better debugging.
1 parent bfe8871 commit 04928a9

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/mxcp/sdk/auth/middleware.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Any
1010

1111
from mcp.server.auth.middleware.auth_context import get_access_token
12+
from starlette.exceptions import HTTPException
1213

1314
from mxcp.sdk.telemetry import record_counter, traced_operation
1415

@@ -281,9 +282,9 @@ async def check_authentication(self) -> UserContext | None:
281282
user_context = await self.oauth_handler.get_user_context(
282283
external_token
283284
)
284-
except Exception as e:
285+
except HTTPException as e:
285286
# Check if this is a 401/token expired error
286-
if hasattr(e, "status_code") and e.status_code == 401:
287+
if e.status_code == 401:
287288
logger.info("🔄 Access token expired, attempting refresh...")
288289

289290
# Attempt to refresh the token
@@ -307,6 +308,10 @@ async def check_authentication(self) -> UserContext | None:
307308
else:
308309
# Not a token expiry error, re-raise
309310
raise
311+
except Exception as e:
312+
# Handle non-HTTP exceptions (network errors, etc.)
313+
logger.error(f"Non-HTTP error during get_user_context: {e}")
314+
raise
310315

311316
# Cache the result for future requests
312317
await self._cache_user_context(access_token.token, user_context)

0 commit comments

Comments
 (0)