fix: include Zenodo API error response body in error messages (#169)#181
fix: include Zenodo API error response body in error messages (#169)#181rtmalikian wants to merge 2 commits into
Conversation
When a non-200 status code is returned from the Zenodo API, the error message now includes the response body (JSON or text) so users can diagnose the root cause (e.g., rate limiting, access denied, invalid record). Previously, only the status code was shown, making it impossible to debug intermittent 403/429 errors without using a browser. Fixes BrainLesion#169
There was a problem hiding this comment.
Pull request overview
This PR improves diagnosability of Zenodo download failures by surfacing Zenodo’s error response body in the raised ZenodoException, and extends test coverage to validate both JSON and plain-text error bodies.
Changes:
- Include Zenodo error response content (JSON or text fallback) in
_get_metadata_and_archive_url()exception messages. - Update the existing failure test to assert that JSON error details appear in the exception message.
- Add a new test covering the non-JSON (text body) fallback path.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| brainles_preprocessing/utils/zenodo.py | Adds parsing of Zenodo error bodies and includes them in the raised exception message. |
| tests/test_zenodo.py | Expands assertions for error messages and adds a text-body fallback test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| try: | ||
| error_detail = response.json() | ||
| except (ValueError, requests.exceptions.JSONDecodeError): | ||
| error_detail = response.text.strip() |
| error_msg = ( | ||
| f"Cannot find record '{self.record_id}' on Zenodo " | ||
| f"({response.status_code=})." | ||
| f"(status_code={response.status_code}). " | ||
| f"Response: {error_detail}" | ||
| ) |
1. Catch ValueError only (requests.exceptions.JSONDecodeError is a subclass and may not exist in older requests versions) 2. Use status-code-aware error messages: 'Cannot find record' for 404, 'Failed to fetch record' for other status codes (403, 429, etc.)
|
Hi, I've addressed both Copilot review comments:
All 13 tests pass with the updated code. PR is ready for re-review. 🙏 |
Summary
When the Zenodo API returns a non-200 status code, the error response body (which contains the actual explanation from Zenodo) was being discarded. The error message only showed the status code, making it impossible to diagnose issues like rate limiting (429), access denied (403), or invalid records (404).
What was broken
In
_get_metadata_and_archive_url(), the error message was:This provided no information about why the request failed. Users had no way to distinguish between rate limiting, access restrictions, or invalid record IDs without manually visiting the URL in a browser.
What the fix does
Fixes #169
Verification
All 13 tests pass:
About the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.
📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a
Disclosure: This code was developed with assistance from mimo-2.5-pro (Xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.