Skip to content

fix(api_client): raise HTTPError instead of returning None on API errors#1089

Open
AbdelhamidKhald wants to merge 1 commit intomlco2:masterfrom
AbdelhamidKhald:fix/issue-820-api-client-raise-errors
Open

fix(api_client): raise HTTPError instead of returning None on API errors#1089
AbdelhamidKhald wants to merge 1 commit intomlco2:masterfrom
AbdelhamidKhald:fix/issue-820-api-client-raise-errors

Conversation

@AbdelhamidKhald
Copy link

Summary

Relates to #820

Problem

ApiClient methods silently return None, 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 confusing TypeError exceptions downstream (e.g., iterating over None).

Solution

Added response.raise_for_status() at the end of _log_error(), so all API methods now raise requests.exceptions.HTTPError after logging the error details. Removed the now-unreachable return None/False/[] statements.

Safety

All existing callers already handle exceptions properly:

  • CodeCarbonAPIOutput.live_out() / .out() — wrapped in try/except Exception
  • _create_run() — has except requests.exceptions.ConnectionError and except Exception
  • add_emission() — has except Exception
  • CLI commands — all go through main() which has a global try/except Exception

Changes

  • codecarbon/core/api_client.py: Added response.raise_for_status() to _log_error(); removed 13 unreachable return statements and 1 dead None guard
  • tests/test_api_call.py: Added test_call_api_error verifying HTTPError is raised on 500/400 responses

Testing

  • All 11 existing + new tests pass
  • black formatting: clean
  • ruff linting: clean
  • Verified all caller exception handling paths

Checklist

  • Tests pass locally
  • Linter passes
  • Follows contribution guidelines
  • Changes are minimal and focused

@AbdelhamidKhald AbdelhamidKhald requested a review from a team as a code owner February 27, 2026 13:30
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.07%. Comparing base (db0d749) to head (f925406).
⚠️ Report is 3 commits behind head on master.

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.
📢 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.

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
@AbdelhamidKhald AbdelhamidKhald force-pushed the fix/issue-820-api-client-raise-errors branch from f925406 to 90a2913 Compare February 27, 2026 18:37
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.

1 participant