From c890d54c735b9b58481f6965f2203aefad29559e Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Wed, 30 Oct 2024 16:56:32 +0200 Subject: [PATCH] Show message from API backend when checking token fails (#559) --- logfire/_internal/config.py | 19 +++++++++++-------- tests/test_cli.py | 4 +++- tests/test_configure.py | 8 +++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/logfire/_internal/config.py b/logfire/_internal/config.py index a4a16c09a..3090de435 100644 --- a/logfire/_internal/config.py +++ b/logfire/_internal/config.py @@ -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() diff --git a/tests/test_cli.py b/tests/test_cli.py index 486812824..d5064d2a6 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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 diff --git a/tests/test_configure.py b/tests/test_configure.py index 336c3ff58..423fdd5be 100644 --- a/tests/test_configure.py +++ b/tests/test_configure.py @@ -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 @@ -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