Skip to content

Conversation

@brandonhawi
Copy link
Contributor

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:

  • Empty responses for dates with no data
  • 500 Internal Server Error messages wrapped in plain text
  • Previously resulted in cryptic JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Solution

Enhanced NBAResponse.get_dict() in src/nba_api/library/http.py to:

  • Catch JSONDecodeError and provide informative error messages
  • Show preview of response content when invalid JSON is received
  • Include the request URL for easier debugging
  • Differentiate between empty responses and responses with error content

Example

Before:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

After:

ValueError: NBA API returned an invalid JSON response: Expecting value: line 1 column 1 (char 0). 
Response content (first 200 chars): System.Net.WebException: The remote server returned an error: (500) Internal Server Error...
URL: https://stats.nba.com/stats/playerdashptreb?...

Testing

  • All 460 existing unit tests pass
  • Tested with various date/season combinations showing different API quirks

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 <[email protected]>
@swar
Copy link
Owner

swar commented Jan 9, 2026

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.

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.

2 participants