-
Notifications
You must be signed in to change notification settings - Fork 147
fix: expose HTTP status code in JSONRPCTransport errors #798
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
Open
pratik3558
wants to merge
4
commits into
a2aproject:main
Choose a base branch
from
pratik3558:fix/jsonrpc-transport-http-status-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package org.a2aproject.sdk.spec; | ||
|
|
||
| import org.a2aproject.sdk.util.Assert; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Client exception indicating an HTTP transport error with a specific status code. | ||
|
|
@@ -15,14 +16,14 @@ | |
| * <li>5xx - Server errors (500 Internal Server Error, 503 Service Unavailable, etc.)</li> | ||
| * </ul> | ||
| * <p> | ||
| * Usage example: | ||
| * This exception is set as the cause of {@link A2AClientException} so that callers | ||
| * can inspect the HTTP status code while remaining backward compatible: | ||
| * <pre>{@code | ||
| * if (response.statusCode() >= 400) { | ||
| * throw new A2AClientHTTPError( | ||
| * response.statusCode(), | ||
| * "HTTP error: " + response.statusMessage(), | ||
| * response.body() | ||
| * ); | ||
| * } catch (A2AClientException e) { | ||
| * if (e.getCause() instanceof A2AClientHTTPError httpError) { | ||
| * int status = httpError.getCode(); // e.g. 401, 503 | ||
| * String body = httpError.getResponseBody(); // raw response body, may be null | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| * | ||
|
|
@@ -40,37 +41,69 @@ public class A2AClientHTTPError extends A2AClientError { | |
| */ | ||
| private final String message; | ||
|
|
||
| /** | ||
| * The raw HTTP response body, may be {@code null}. | ||
| */ | ||
| @Nullable | ||
| private final String responseBody; | ||
|
|
||
| /** | ||
| * Creates a new HTTP client error with the specified status code and message. | ||
| * | ||
| * @param code the HTTP status code | ||
| * @param message the error message | ||
| * @param data additional error data (may be the response body) | ||
| * @throws IllegalArgumentException if code or message is null | ||
| * @deprecated Use {@link #A2AClientHTTPError(int, String, String)} instead to preserve the response body. | ||
| */ | ||
| @Deprecated(since = "1.0.0.Beta1", forRemoval = true) | ||
| public A2AClientHTTPError(int code, String message, Object data) { | ||
| Assert.checkNotNullParam("code", code); | ||
| Assert.checkNotNullParam("message", message); | ||
| this.code = code; | ||
| this.message = message; | ||
| this.responseBody = data instanceof String s ? s : null; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have an empty string instead of null. That way we could drop the @nullable on the responseBodyy |
||
| } | ||
|
|
||
| /** | ||
| * Gets the error code | ||
| * Creates a new HTTP client error with the specified status code, message, and response body. | ||
| * | ||
| * @return the error code | ||
| * @param code the HTTP status code (e.g. 401, 503) | ||
| * @param message the error message | ||
| * @param responseBody the raw HTTP response body, may be {@code null} | ||
| */ | ||
| public A2AClientHTTPError(int code, String message, @Nullable String responseBody) { | ||
| Assert.checkNotNullParam("message", message); | ||
| this.code = code; | ||
| this.message = message; | ||
| this.responseBody = responseBody; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the HTTP status code. | ||
| * | ||
| * @return the HTTP status code (e.g. 401, 404, 500, 503) | ||
| */ | ||
| public int getCode() { | ||
| return code; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the error message | ||
| * Gets the error message. | ||
| * | ||
| * @return the error message | ||
| */ | ||
| @Override | ||
| public String getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the raw HTTP response body, if available. | ||
| * | ||
| * @return the response body, or {@code null} if not available | ||
| */ | ||
| public @Nullable String getResponseBody() { | ||
| return responseBody; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.