Skip to content

Commit 76bfa3b

Browse files
committed
[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.
1 parent 286a6e6 commit 76bfa3b

File tree

4 files changed

+15
-28
lines changed

4 files changed

+15
-28
lines changed

launchable/commands/verify.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from launchable.utils.env_keys import TOKEN_KEY
1212
from launchable.utils.tracking import Tracking, TrackingClient
1313

14-
from ..utils.authentication import get_org_workspace
14+
from ..utils.authentication import get_org_workspace, ensure_org_workspace
1515
from ..utils.click import emoji
1616
from ..utils.commands import Command
1717
from ..utils.http_client import DEFAULT_BASE_URL
@@ -88,18 +88,15 @@ def verify(context: click.core.Context):
8888
click.echo("Java command: " + repr(java))
8989
click.echo("launchable version: " + repr(version))
9090

91-
if org is None or workspace is None:
92-
msg = (
93-
"Could not identify Launchable organization/workspace. "
94-
"Please confirm if you set LAUNCHABLE_TOKEN or LAUNCHABLE_ORGANIZATION and LAUNCHABLE_WORKSPACE "
95-
"environment variables"
96-
)
91+
# raise an error here after we print out the basic diagnostics if LAUNCHABLE_TOKEN is not set.
92+
try:
93+
ensure_org_workspace()
94+
except click.UsageError as e:
9795
tracking_client.send_error_event(
9896
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
99-
stack_trace=msg
97+
stack_trace=e.message
10098
)
101-
raise click.UsageError(
102-
click.style(msg, fg="red"))
99+
raise e
103100

104101
try:
105102
res = client.request("get", "verification")

launchable/utils/authentication.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88

99

1010
def get_org_workspace():
11+
'''
12+
Returns (org,ws) tuple from LAUNCHABLE_TOKEN, or (None,None) if not found.
13+
Use ensure_org_workspace() if this is supposed to be an error condition
14+
'''
1115
token = os.getenv(TOKEN_KEY)
1216
if token:
1317
try:
1418
_, user, _ = token.split(":", 2)
1519
org, workspace = user.split("/", 1)
1620
return org, workspace
1721
except ValueError:
18-
return None, None
22+
raise click.UsageError(click.style("Invalid value in LAUNCHABLE_TOKEN environment variable.", fg="red"))
1923

2024
return os.getenv(ORGANIZATION_KEY), os.getenv(WORKSPACE_KEY)
2125

@@ -25,9 +29,7 @@ def ensure_org_workspace() -> Tuple[str, str]:
2529
if org is None or workspace is None:
2630
raise click.UsageError(
2731
click.style(
28-
"Could not identify Launchable organization/workspace. "
29-
"Please confirm if you set LAUNCHABLE_TOKEN or LAUNCHABLE_ORGANIZATION and "
30-
"LAUNCHABLE_WORKSPACE environment variables",
32+
"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
3133
fg="red"))
3234
return org, workspace
3335

launchable/utils/launchable_client.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from launchable.utils.tracking import Tracking, TrackingClient # type: ignore
1010

1111
from ..app import Application
12-
from .authentication import get_org_workspace
12+
from .authentication import get_org_workspace, ensure_org_workspace
1313
from .env_keys import REPORT_ERROR_KEY
1414

1515

@@ -23,13 +23,7 @@ def __init__(self, tracking_client: Optional[TrackingClient] = None, base_url: s
2323
app=app
2424
)
2525
self.tracking_client = tracking_client
26-
self.organization, self.workspace = get_org_workspace()
27-
if self.organization is None or self.workspace is None:
28-
raise ValueError(
29-
"Could not identify a Launchable organization/workspace. "
30-
"Confirm that you set LAUNCHABLE_TOKEN "
31-
"(or LAUNCHABLE_ORGANIZATION and LAUNCHABLE_WORKSPACE) environment variable(s)\n"
32-
"See https://help.launchableinc.com/getting-started#setting-your-api-key")
26+
self.organization, self.workspace = ensure_org_workspace()
3327
self._workspace_state_cache: Optional[Dict[str, Union[str, bool]]] = None
3428

3529
def request(

tests/utils/test_authentication.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ def test_get_org_workspace_no_environment_variables(self):
1111
self.assertIsNone(org)
1212
self.assertIsNone(workspace)
1313

14-
@mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": "invalid"})
15-
def test_get_org_workspace_invalid_LAUNCHABLE_TOKEN(self):
16-
org, workspace = get_org_workspace()
17-
self.assertIsNone(org)
18-
self.assertIsNone(workspace)
19-
2014
@mock.patch.dict(os.environ,
2115
{"LAUNCHABLE_TOKEN": "v1:launchableinc/test:token"})
2216
def test_get_org_workspace_valid_LAUNCHABLE_TOKEN(self):

0 commit comments

Comments
 (0)