fix: raise HTTP errors from ApiClient instead of returning None - #1277
Conversation
|
Cross-linked on #1089. Prefer maintainers pick one of the two rather than both landing. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1277 +/- ##
==========================================
+ Coverage 90.39% 91.43% +1.03%
==========================================
Files 49 49
Lines 5103 5056 -47
==========================================
+ Hits 4613 4623 +10
+ Misses 490 433 -57 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
594bb34 to
2b43f3a
Compare
|
Thanks for you contribution. It seems it need some improvements. Here is the review of Opus 5: OverviewTurns I verified the propagation paths: Issues1.
|
|
Rebased onto master. The 2 codecov/patch misses are the defensive |
|
Thanks @benoit-cty — addressed the Opus review:
Skipped the tracker |
Call raise_for_status after logging API failures so callers get meaningful errors, catch failures in EmissionsTracker so tracking continues offline, and update tests that assumed silent None returns. Fixes mlco2#820 Co-authored-by: Cursor <cursoragent@cursor.com>
_raise_api_error now raises for any unexpected status (including 2xx/3xx), CLI commands print friendly errors, and tests cover the unexpected-2xx path.
Following the review on mlco2#1277: - add a `_api_call` helper in the CLI that turns the exceptions ApiClient now raises into a readable message and `typer.Exit(1)` instead of a traceback, and use it for every API call of the `config` wizard (create_organization, list_projects_from_organization, create_project, list_experiments_from_project, add_experiment, get_api_key), which were left unguarded. - `get_api_key` calls `raise_for_status()` so a failed token request reports the HTTP error instead of a KeyError on the JSON body. - document why `except HTTPError: raise` is needed in `add_emission` and `_create_run` (avoid logging the error a second time). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
8131e5b to
8fc4a1c
Compare
benoit-cty
left a comment
There was a problem hiding this comment.
Thanks for fixing all this.
Every method repeated the same four lines: build the headers, call requests, compare the status code, log and raise. They now all delegate to a single `_request(method, url, payload, expected_status)`, which removes 44 lines from the module. Behaviour is unchanged: `_request` calls `_raise_api_error` on an unexpected status, so the errors raised, the logs and the re-raise clauses of `add_emission` and `_create_run` all stay as they were. The helper comes from the alternative fix proposed in mlco2#1288, which targets the same issue. Co-authored-by: PSR94 <88868390+PSR94@users.noreply.github.com> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Thanks for the review and for the credit. Glad the helper approach was useful. |
Description
ApiClientnow callsresponse.raise_for_status()after logging HTTP failures instead of returningNone/False/[]. Callers get realrequests.exceptions.HTTPErrors (and connection errors are re-raised from_create_run/add_emission).EmissionsTrackerwraps Code Carbon API output initialization in try/except so tracking continues without API reporting if setup fails. Removed the CLIorganization is Noneguard that assumed silent failure, and dropped theorganizations is Nonecheck incheck_organization_exists.Related Issue
Fixes #820
Motivation and Context
Silent
Nonereturns made API failures hard to debug and could confuse callers (e.g. iterating overNone). The issue asks forraise_for_statusin the client while keeping the tracker resilient.Note: #1089 is an earlier approved attempt that is stale with failing CI and does not wrap tracker API init. Happy to close this in favor of a refreshed #1089 if maintainers prefer.
How Has This Been Tested?
Results: all targeted tests passed (26 for api/http/tracker slice; 24 CLI).
Types of changes
Direct
ApiClientcallers that previously checked forNoneon HTTP errors will now see raised exceptions (intended). Tracker/live emission paths catch and log.AI Usage Disclosure
Please refer to docs/how-to/ai-policy.md for detailed guidelines on how to disclose AI usage in your PR. Accurately completing this section is mandatory.
AI coding tools were used to help draft the change and this PR description. I reviewed the complete change, understand the reasoning, and ran the reported local tests before submitting.
Checklist:
Made with Cursor