Skip to content

Commit

Permalink
Show message from API backend when checking token fails (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki authored Oct 30, 2024
1 parent 74d1e39 commit c890d54
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
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

0 comments on commit c890d54

Please sign in to comment.