Skip to content

docs: Improve API error handling for toolbox-core client #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2025

Conversation

anubhav756
Copy link
Contributor

@anubhav756 anubhav756 commented Jun 9, 2025

Fixes #273

Problem

Currently, the SDK client unconditionally attempts to parse all API responses as JSON. If the remote server returns an error (e.g., 403 Forbidden due to an authentication failure, or 404 Not Found), the response body is often HTML or plain text, not JSON.

This leads to a low-level ContentTypeError, which masks the original, more informative error from the server (like the HTTP status and reason). Users are left with a confusing "response not parseable" message instead of knowing the true cause of the failure.

Solution

This PR introduces a check to validate the HTTP response status before attempting to parse its body.

If the response status is not 200 OK, the client now captures the status code, reason phrase, and the raw response body.
It then raises a RuntimeError that includes all of this valuable context.
This ensures that the actual error from the server is always surfaced to the user.

Before

aiohttp.client_exceptions.ContentTypeError: 403, message='Attempt to decode JSON with unexpected mimetype: text/html; charset=utf-8'

After

RuntimeError: API request failed with status 403 (Forbidden). Server response: Forbidden

@anubhav756 anubhav756 self-assigned this Jun 9, 2025
@anubhav756 anubhav756 requested a review from kurtisvg June 9, 2025 20:16
@anubhav756 anubhav756 marked this pull request as ready for review June 9, 2025 20:17
@anubhav756 anubhav756 requested a review from a team as a code owner June 9, 2025 20:17
@anubhav756 anubhav756 changed the title docs: Improve error handling for toolbox-core client docs: Improve API error handling for toolbox-core client Jun 9, 2025
@anubhav756 anubhav756 requested a review from twishabansal June 9, 2025 20:23
@twishabansal
Copy link
Contributor

Thanks for the change @anubhav756 ! I was wondering if we wanted to change this across other API requests. Perhaps, we could create a helper method and add it for API requests in methods like load_tool() too.

@anubhav756
Copy link
Contributor Author

Thanks for the change @anubhav756 ! I was wondering if we wanted to change this across other API requests. Perhaps, we could create a helper method and add it for API requests in methods like load_tool() too.

Thanks for pointing that out! Added.

@anubhav756 anubhav756 merged commit 65398fd into main Jun 11, 2025
19 checks passed
@anubhav756 anubhav756 deleted the anubhav-error branch June 11, 2025 05:48
@release-please release-please bot mentioned this pull request Jun 10, 2025
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.

Improve errors for unauthenticated requests
4 participants