Skip to content

Commit 521adc8

Browse files
authored
fix(cli): avoid traceback noise in config auth failures (#1080)
1 parent b370a5a commit 521adc8

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

codecarbon/cli/main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ def version(
6969

7070
def show_config(path: Path = Path("./.codecarbon.config")) -> None:
7171
d = get_config(path)
72-
api_endpoint = get_api_endpoint(path)
73-
api = ApiClient(endpoint_url=api_endpoint)
74-
api.set_access_token(get_access_token())
7572
print("Current configuration : \n")
7673
print("Config file content : ")
7774
print(d)
7875
try:
76+
api_endpoint = get_api_endpoint(path)
77+
api = ApiClient(endpoint_url=api_endpoint)
78+
api.set_access_token(get_access_token())
7979
if "organization_id" not in d:
8080
print(
8181
"No organization_id in config, follow setup instruction to complete your configuration file!",
@@ -102,8 +102,8 @@ def show_config(path: Path = Path("./.codecarbon.config")) -> None:
102102
print("\nOrganization :")
103103
print(org)
104104
except Exception as e:
105-
raise ValueError(
106-
f"Your configuration is invalid, please verify your configuration file at {path}. To start from scratch, run `codecarbon config` and overwrite your configuration file. (error: {e})"
105+
print(
106+
f"[yellow]Could not validate remote configuration details[/yellow]. You can continue with local configuration setup. (error: {e})"
107107
)
108108

109109

tests/cli/test_cli_main.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,36 @@ def test_monitor_run_and_monitor(monkeypatch):
7979
)
8080
assert result.exit_code == 0
8181
assert "Hello, World!" in result.output
82+
83+
84+
def test_show_config_handles_access_token_errors(monkeypatch, tmp_path, capsys):
85+
class FakeApiClient:
86+
def __init__(self, endpoint_url=None):
87+
self.endpoint_url = endpoint_url
88+
89+
def set_access_token(self, token):
90+
self.token = token
91+
92+
def fake_get_access_token():
93+
raise ValueError("Not able to retrieve the access token, please run login.")
94+
95+
monkeypatch.setattr(cli_main, "ApiClient", FakeApiClient)
96+
monkeypatch.setattr(
97+
cli_main,
98+
"get_config",
99+
lambda path: {
100+
"api_endpoint": "https://api.codecarbon.io",
101+
"organization_id": "org-id",
102+
"project_id": "project-id",
103+
"experiment_id": "experiment-id",
104+
},
105+
)
106+
monkeypatch.setattr(
107+
cli_main, "get_api_endpoint", lambda path: "https://api.codecarbon.io"
108+
)
109+
monkeypatch.setattr(cli_main, "get_access_token", fake_get_access_token)
110+
111+
cli_main.show_config(tmp_path / ".codecarbon.config")
112+
captured = capsys.readouterr()
113+
assert "Could not validate remote configuration details" in captured.out
114+
assert "Not able to retrieve the access token" in captured.out

0 commit comments

Comments
 (0)