fix: improve error handling for invalid API responses#605
Open
brandonhawi wants to merge 1 commit intoswar:masterfrom
Open
fix: improve error handling for invalid API responses#605brandonhawi wants to merge 1 commit intoswar:masterfrom
brandonhawi wants to merge 1 commit intoswar:masterfrom
Conversation
Enhance NBAResponse.get_dict() to provide clearer error messages when the NBA API returns invalid or empty responses. This addresses issues where certain date/parameter combinations return non-JSON responses (e.g., 500 errors or empty strings) which previously resulted in cryptic JSONDecodeError messages. Changes: - Wrap json.loads() in try-except to catch JSONDecodeError - Provide informative ValueError with response content preview for invalid JSON responses - Include the request URL in error messages for easier debugging - Handle both empty responses and responses with error content This fix helps users quickly identify API issues without having to debug low-level JSON parsing errors. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Owner
|
My thoughts on this MR is that the exception messages are too long. I think the exception messages should be a short summary. I think all the additional context is great, but I think it might need too be an exception attribute instead. I think I would be okay with making custom exceptions in this case. If we strive towards that, maybe we should think about bumping the version to 1.12.0 since it could be a breaking change for folks. |
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.
Error Handling Improvement
This PR improves error handling when the NBA API returns invalid or empty responses.
Problem
The NBA API sometimes returns non-JSON responses for certain date/parameter combinations:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)Solution
Enhanced
NBAResponse.get_dict()insrc/nba_api/library/http.pyto:JSONDecodeErrorand provide informative error messagesExample
Before:
After:
Testing