Skip to content

fix: raise HTTP errors in ApiClient instead of returning None (#820)#1128

Open
agodianel wants to merge 1 commit intomlco2:masterfrom
agodianel:fix/820-api-client-throw-errors
Open

fix: raise HTTP errors in ApiClient instead of returning None (#820)#1128
agodianel wants to merge 1 commit intomlco2:masterfrom
agodianel:fix/820-api-client-throw-errors

Conversation

@agodianel
Copy link

Resolves #820 by raising requests.exceptions.HTTPError when the API returns a non-success status code, rather than failing silently and returning None. This allows the EmissionsTracker to catch the exception and fall back to offline mode without crashing the user's execution run.

Description

Fixes #820

The ApiClient previously returned None or False on HTTP errors, which failed silently for the user but occasionally crashed upstream logic (e.g. attempting to read api.run_id when it was None). As requested in the issue, this PR updates the client to throw requests.exceptions.HTTPError on bad status codes and handles the exception upstream to fall back gracefully.

Changes:

  • Replaced return None/False/[] with r.raise_for_status() in all ApiClient data methods.
  • Re-raised exceptions in generic try...except blocks within ApiClient (e.g., inside _create_run).
  • Wrapped CodeCarbonAPIOutput initialization in emissions_tracker.py with a try...except block so that if the API is unreachable, the execution falls back to local tracking instead of tearing down the user's workload.
  • Added test_call_api_error_raises to test_api_call.py to ensure that 500 error responses raise the correct HTTP error natively.

All local CodeCarbon unit tests (including the new ones) execute and pass successfully.

)

Resolves mlco2#820 by raising requests.exceptions.HTTPError when the API returns
a non-success status code, rather than failing silently and returning None.
This allows the EmissionsTracker to catch the exception and fall back to
offline mode without crashing the user's execution run.
@agodianel agodianel requested a review from a team as a code owner March 23, 2026 00:17
@codecov
Copy link

codecov bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 13.04348% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.87%. Comparing base (46ac6dd) to head (ac1dba7).

Files with missing lines Patch % Lines
codecarbon/core/api_client.py 18.75% 13 Missing ⚠️
codecarbon/emissions_tracker.py 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1128      +/-   ##
==========================================
+ Coverage   80.74%   80.87%   +0.13%     
==========================================
  Files          41       41              
  Lines        3921     3927       +6     
==========================================
+ Hits         3166     3176      +10     
+ Misses        755      751       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API client to throw error instead of None

1 participant