Skip to content

Enhance error handling in tournament teams extraction from SendouQ API#68

Merged
cesaregarza merged 1 commit intomainfrom
feature/fix-teams-page
Jan 26, 2026
Merged

Enhance error handling in tournament teams extraction from SendouQ API#68
cesaregarza merged 1 commit intomainfrom
feature/fix-teams-page

Conversation

@cesaregarza
Copy link
Owner

This commit improves the error handling when extracting tournament teams from the SendouQ API response. It adds support for parsing JSON strings and logs errors related to key access and JSON decoding, ensuring better traceability and robustness in the event of unexpected response structures.

This commit improves the error handling when extracting tournament teams from the SendouQ API response. It adds support for parsing JSON strings and logs errors related to key access and JSON decoding, ensuring better traceability and robustness in the event of unexpected response structures.
@cesaregarza cesaregarza merged commit 7425a34 into main Jan 26, 2026
7 checks passed
@cesaregarza cesaregarza deleted the feature/fix-teams-page branch January 26, 2026 03:59
@claude
Copy link

claude bot commented Jan 26, 2026

Pull Request Review: Enhance error handling in tournament teams extraction

Summary

This PR improves error handling for parsing tournament teams data from the SendouQ API by adding support for cases where data["data"] is a JSON string that needs additional parsing.


Code Quality and Best Practices

Positives:

  • Clean, focused change that solves a specific problem
  • Good use of type checking with isinstance(inner_data, str)
  • Comprehensive exception handling including orjson.JSONDecodeError
  • Helpful inline comment explaining the issue
  • Consistent with existing error handling patterns in the file

Suggestions:

  1. Consider defensive coding for nested access (src/fast_api_app/routes/sendou_proxy.py:210)
    The current implementation uses direct nested dictionary access which could fail if the structure is unexpected. Consider using .get() with clear error messages for better debugging when the API structure changes.

Potential Bugs or Issues

  1. Mutation of shared reference (src/fast_api_app/routes/sendou_proxy.py:207-209)
    The code assigns inner_data = data["data"] and then potentially reassigns it. This is fine, but be aware that this doesn't modify data["data"] itself. This is likely the intended behavior.

  2. Inconsistency with other endpoints (src/fast_api_app/routes/sendou_proxy.py:170-218)
    Other endpoints in the same file (get_tournament_match, get_tournament_team, get_q_match) don't perform this secondary JSON parsing. This suggests one of two things:

    • This is a special case unique to the /teams endpoint
    • Other endpoints might have the same issue but it hasn't been encountered yet

    Recommendation: Verify whether other endpoints need similar handling or document why this is specific to the teams endpoint.


Performance Considerations

  • Double JSON parsing: The change adds an additional orjson.loads() call when data["data"] is a string. This is necessary for correctness, but worth noting.
  • Impact: Minimal - orjson is highly performant and this only affects the teams endpoint
  • Trade-off: The robustness gained outweighs the minor performance cost

Security Concerns

Overall: No major security issues

  • The use of orjson.loads() is safe - it doesn't execute code like eval() would
  • The exception handling prevents unhandled crashes that could leak information
  • The 502 error response is appropriate and doesn't expose internal details

Minor consideration:

  • The error log at line 212 includes the exception details. This is generally fine, but ensure your logging infrastructure doesn't expose these logs publicly, as they might contain API response structure details

Test Coverage

Critical Gap: No tests for this code path

The codebase has extensive test coverage for other features, but there are no tests for the sendou_proxy.py routes. This PR would benefit from:

  1. Unit tests for the new parsing logic (test when data["data"] is already parsed, is a JSON string, is malformed JSON, or when tournament/ctx/teams keys are missing)

  2. Integration tests for the endpoint (mock the sendou.ink API response and verify correct handling of different response formats)

Recommendation: Add tests to prevent regression and ensure the fix works as intended. The existing test patterns in tests/ provide good examples to follow.


Additional Observations

  1. Code consistency: This change maintains consistency with the codebase style (line length, formatting, imports)

  2. Documentation: The inline comment is helpful, but consider also documenting this behavior in the docstring

  3. Error message specificity: The error message "Unexpected response structure from sendou.ink" could be more specific about what was expected vs. what was received (though be careful not to leak sensitive data)


Verdict

Recommendation: Approve with suggestions

This is a solid bug fix that improves robustness. The code quality is good and follows existing patterns. The main gaps are:

  • Missing test coverage (should be addressed before or shortly after merge)
  • Potential inconsistency with other endpoints (should be investigated)
  • Could benefit from more defensive nested access patterns

The change is safe to merge as-is, but would be stronger with tests and verification that other endpoints don't need similar treatment.

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