Restore improved exception handling and update test for specific httpx.HTTPStatusError

- Use httpx.HTTPStatusError for more specific exception catching
- Provide better error message: 'GitHub token validation failed. Please check your token and try again.'
- Update test to expect improved error message and properly mock httpx exceptions
This commit is contained in:
ehhuang 2025-07-08 01:22:42 -07:00
parent 871021adc4
commit 7f4d55f5f7
2 changed files with 7 additions and 4 deletions

View file

@ -328,9 +328,9 @@ class GitHubTokenAuthProvider(AuthProvider):
"""
try:
user_info = await _get_github_user_info(token, self.config.github_api_base_url)
except Exception as e:
except httpx.HTTPStatusError as e:
logger.warning(f"GitHub token validation failed: {e}")
raise ValueError("Invalid GitHub token") from e
raise ValueError("GitHub token validation failed. Please check your token and try again.") from e
principal = user_info["user"]["login"]

View file

@ -6,6 +6,7 @@
from unittest.mock import AsyncMock, patch
import httpx
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
@ -24,7 +25,9 @@ class MockResponse:
def raise_for_status(self):
if self.status_code != 200:
raise Exception(f"HTTP error: {self.status_code}")
# Create a mock request for the HTTPStatusError
mock_request = httpx.Request("GET", "https://api.github.com/user")
raise httpx.HTTPStatusError(f"HTTP error: {self.status_code}", request=mock_request, response=self)
@pytest.fixture
@ -127,7 +130,7 @@ def test_authenticated_endpoint_with_invalid_github_token(mock_client_class, git
response = github_token_client.get("/test", headers={"Authorization": "Bearer invalid_token"})
assert response.status_code == 401
assert "Invalid GitHub token" in response.json()["error"]["message"]
assert "GitHub token validation failed. Please check your token and try again." in response.json()["error"]["message"]
@patch("llama_stack.distribution.server.auth_providers.httpx.AsyncClient")