fix(api_client): raise HTTPError instead of returning None on API errors#1089
Open
AbdelhamidKhald wants to merge 1 commit intomlco2:masterfrom
Open
fix(api_client): raise HTTPError instead of returning None on API errors#1089AbdelhamidKhald wants to merge 1 commit intomlco2:masterfrom
AbdelhamidKhald wants to merge 1 commit intomlco2:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1089 +/- ##
==========================================
+ Coverage 78.19% 79.07% +0.88%
==========================================
Files 38 38
Lines 3637 3623 -14
==========================================
+ Hits 2844 2865 +21
+ Misses 793 758 -35 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
ApiClient methods now call response.raise_for_status() via _log_error instead of silently returning None/False/[] on HTTP errors. This allows callers to properly handle API failures through exception handling. Existing callers (CodeCarbonAPIOutput, _create_run, add_emission) already have try/except blocks that catch these exceptions gracefully. Relates to mlco2#820
f925406 to
90a2913
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Relates to #820
Problem
ApiClientmethods silently returnNone,False, or[]when API calls fail. This makes it impossible for callers to distinguish between "API returned an error" and "no data exists", leading to confusingTypeErrorexceptions downstream (e.g., iterating overNone).Solution
Added
response.raise_for_status()at the end of_log_error(), so all API methods now raiserequests.exceptions.HTTPErrorafter logging the error details. Removed the now-unreachablereturn None/False/[]statements.Safety
All existing callers already handle exceptions properly:
CodeCarbonAPIOutput.live_out()/.out()— wrapped intry/except Exception_create_run()— hasexcept requests.exceptions.ConnectionErrorandexcept Exceptionadd_emission()— hasexcept Exceptionmain()which has a globaltry/except ExceptionChanges
codecarbon/core/api_client.py: Addedresponse.raise_for_status()to_log_error(); removed 13 unreachable return statements and 1 deadNoneguardtests/test_api_call.py: Addedtest_call_api_errorverifyingHTTPErroris raised on 500/400 responsesTesting
blackformatting: cleanrufflinting: cleanChecklist