Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show message from API backend when checking token fails #559

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions logfire/_internal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,14 +1102,17 @@ def from_token(cls, token: str, session: requests.Session, base_url: str) -> Sel
warnings.warn(f'Logfire API is unreachable, you may have trouble sending data. Error: {e}')
return None

if response.status_code == 401:
warnings.warn('Invalid Logfire token.')
return None
elif response.status_code != 200:
# any other status code is considered unhealthy
warnings.warn(
f'Logfire API is unhealthy, you may have trouble sending data. Status code: {response.status_code}'
)
if response.status_code != 200:
try:
detail = response.json()['detail']
except Exception:
warnings.warn(
f'Logfire API returned status code {response.status_code}, you may have trouble sending data.',
)
else:
warnings.warn(
f'Logfire API returned status code {response.status_code}. Detail: {detail}',
)
return None

data = response.json()
Expand Down
4 changes: 3 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def test_whoami(tmp_dir_cwd: Path, logfire_credentials: LogfireCredentials, caps

logfire_credentials.write_creds_file(tmp_dir_cwd)

with pytest.warns(UserWarning, match='unhealthy'): # because of the 500 above
with pytest.warns(
UserWarning, match='Logfire API returned status code 500, you may have trouble sending data.'
):
main(shlex.split(f'--logfire-url=http://localhost:0 whoami --data-dir {tmp_dir_cwd}'))

assert len(request_mocker.request_history) == 1
Expand Down
8 changes: 5 additions & 3 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1351,9 +1351,11 @@ def test_initialize_credentials_from_token_invalid_token():
with ExitStack() as stack:
request_mocker = requests_mock.Mocker()
stack.enter_context(request_mocker)
request_mocker.get('https://logfire-api.pydantic.dev/v1/info', text='Error', status_code=401)
request_mocker.get(
'https://logfire-api.pydantic.dev/v1/info', text='{"detail": "Invalid token"}', status_code=401
)

with pytest.warns(match='Invalid Logfire token.'):
with pytest.warns(match='Logfire API returned status code 401. Detail: Invalid token'):
LogfireConfig()._initialize_credentials_from_token('some-token') # type: ignore


Expand All @@ -1364,7 +1366,7 @@ def test_initialize_credentials_from_token_unhealthy():
request_mocker.get('https://logfire-api.pydantic.dev/v1/info', text='Error', status_code=500)

with pytest.warns(
UserWarning, match='Logfire API is unhealthy, you may have trouble sending data. Status code: 500'
UserWarning, match='Logfire API returned status code 500, you may have trouble sending data.'
):
LogfireConfig()._initialize_credentials_from_token('some-token') # type: ignore

Expand Down