From 76bfa3bb7cff849a943a26e9aff6f1287a9bb215 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Mon, 24 Nov 2025 11:43:15 -0800 Subject: [PATCH] [chore] error reporting improvement Tell apart a bad value vs no value. Also, use the ensure method to avoid duplicating errors in multiple places. Seeing incorrect value in LAUNCHABLE_TOKEN previously was a silently recovered error, but I think this behavior is harmful. If the value is incorrectly set, we need to call an attention to that fact so that we can guide users to fix it. --- launchable/commands/verify.py | 17 +++++++---------- launchable/utils/authentication.py | 10 ++++++---- launchable/utils/launchable_client.py | 10 ++-------- tests/utils/test_authentication.py | 6 ------ 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/launchable/commands/verify.py b/launchable/commands/verify.py index 68e4596b9..a7d47eb7b 100644 --- a/launchable/commands/verify.py +++ b/launchable/commands/verify.py @@ -11,7 +11,7 @@ from launchable.utils.env_keys import TOKEN_KEY from launchable.utils.tracking import Tracking, TrackingClient -from ..utils.authentication import get_org_workspace +from ..utils.authentication import get_org_workspace, ensure_org_workspace from ..utils.click import emoji from ..utils.commands import Command from ..utils.http_client import DEFAULT_BASE_URL @@ -88,18 +88,15 @@ def verify(context: click.core.Context): click.echo("Java command: " + repr(java)) click.echo("launchable version: " + repr(version)) - if org is None or workspace is None: - msg = ( - "Could not identify Launchable organization/workspace. " - "Please confirm if you set LAUNCHABLE_TOKEN or LAUNCHABLE_ORGANIZATION and LAUNCHABLE_WORKSPACE " - "environment variables" - ) + # raise an error here after we print out the basic diagnostics if LAUNCHABLE_TOKEN is not set. + try: + ensure_org_workspace() + except click.UsageError as e: tracking_client.send_error_event( event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR, - stack_trace=msg + stack_trace=e.message ) - raise click.UsageError( - click.style(msg, fg="red")) + raise e try: res = client.request("get", "verification") diff --git a/launchable/utils/authentication.py b/launchable/utils/authentication.py index a6f01abb0..aee82dcfd 100644 --- a/launchable/utils/authentication.py +++ b/launchable/utils/authentication.py @@ -8,6 +8,10 @@ def get_org_workspace(): + ''' + Returns (org,ws) tuple from LAUNCHABLE_TOKEN, or (None,None) if not found. + Use ensure_org_workspace() if this is supposed to be an error condition + ''' token = os.getenv(TOKEN_KEY) if token: try: @@ -15,7 +19,7 @@ def get_org_workspace(): org, workspace = user.split("/", 1) return org, workspace except ValueError: - return None, None + raise click.UsageError(click.style("Invalid value in LAUNCHABLE_TOKEN environment variable.", fg="red")) return os.getenv(ORGANIZATION_KEY), os.getenv(WORKSPACE_KEY) @@ -25,9 +29,7 @@ def ensure_org_workspace() -> Tuple[str, str]: if org is None or workspace is None: raise click.UsageError( click.style( - "Could not identify Launchable organization/workspace. " - "Please confirm if you set LAUNCHABLE_TOKEN or LAUNCHABLE_ORGANIZATION and " - "LAUNCHABLE_WORKSPACE environment variables", + "LAUNCHABLE_TOKEN environment variable not set. Nor the LAUNCHABLE_ORGANIZATION and LAUNCHABLE_WORKSPACE combo if you are using https://help.launchableinc.com/sending-data-to-launchable/using-the-launchable-cli/getting-started/migration-to-github-oidc-auth/)", # noqa: E501 fg="red")) return org, workspace diff --git a/launchable/utils/launchable_client.py b/launchable/utils/launchable_client.py index 5013171eb..f2bb28153 100644 --- a/launchable/utils/launchable_client.py +++ b/launchable/utils/launchable_client.py @@ -9,7 +9,7 @@ from launchable.utils.tracking import Tracking, TrackingClient # type: ignore from ..app import Application -from .authentication import get_org_workspace +from .authentication import get_org_workspace, ensure_org_workspace from .env_keys import REPORT_ERROR_KEY @@ -23,13 +23,7 @@ def __init__(self, tracking_client: Optional[TrackingClient] = None, base_url: s app=app ) self.tracking_client = tracking_client - self.organization, self.workspace = get_org_workspace() - if self.organization is None or self.workspace is None: - raise ValueError( - "Could not identify a Launchable organization/workspace. " - "Confirm that you set LAUNCHABLE_TOKEN " - "(or LAUNCHABLE_ORGANIZATION and LAUNCHABLE_WORKSPACE) environment variable(s)\n" - "See https://help.launchableinc.com/getting-started#setting-your-api-key") + self.organization, self.workspace = ensure_org_workspace() self._workspace_state_cache: Optional[Dict[str, Union[str, bool]]] = None def request( diff --git a/tests/utils/test_authentication.py b/tests/utils/test_authentication.py index 9106b0bc5..e44874347 100644 --- a/tests/utils/test_authentication.py +++ b/tests/utils/test_authentication.py @@ -11,12 +11,6 @@ def test_get_org_workspace_no_environment_variables(self): self.assertIsNone(org) self.assertIsNone(workspace) - @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": "invalid"}) - def test_get_org_workspace_invalid_LAUNCHABLE_TOKEN(self): - org, workspace = get_org_workspace() - self.assertIsNone(org) - self.assertIsNone(workspace) - @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": "v1:launchableinc/test:token"}) def test_get_org_workspace_valid_LAUNCHABLE_TOKEN(self):