fix: expose HTTP status code in JSONRPCTransport errors#798
fix: expose HTTP status code in JSONRPCTransport errors#798pratik3558 wants to merge 4 commits intoa2aproject:mainfrom
Conversation
4ee1f77 to
701ab66
Compare
There was a problem hiding this comment.
Code Review
This pull request enhances HTTP error handling by introducing A2AClientHTTPError as a structured cause within A2AClientException, allowing callers to retrieve the HTTP status code and response body. Feedback highlights a missing throws declaration for A2AClientException in the sendPostRequest method, which would lead to a compilation error, and suggests refactoring duplicated error message strings for better maintainability.
701ab66 to
ce324f7
Compare
Previously, when the JSONRPC transport received a non-2xx HTTP response,
it threw a plain IOException with the status code embedded as a string
(e.g. "Request failed 503"). This made it impossible for callers to
programmatically distinguish 4xx from 5xx errors without fragile string
parsing.
This change makes the structured HTTP status code available while
remaining fully backward compatible:
- JSONRPCTransport.sendPostRequest now throws A2AClientException with an
A2AClientHTTPError as the cause, instead of a plain IOException.
All transport methods already declared 'throws A2AClientException' and
already had 'catch (A2AClientException e) { throw e; }', so no caller
signatures change.
- A2AClientHTTPError gains a new 'responseBody' field (and getResponseBody()
accessor) so callers can also inspect the raw error payload returned by
the server (e.g. a gateway's JSON error body on a 429 or 503).
The existing 'code', 'message', and getCode() are preserved unchanged.
The old Object-typed constructor is deprecated in favour of the new
String-typed one that actually stores the body.
Callers that want the status code can now opt in with a simple instanceof
check, while existing catch blocks for A2AClientException continue to work
without modification:
} catch (A2AClientException e) {
if (e.getCause() instanceof A2AClientHTTPError httpErr) {
int status = httpErr.getCode(); // e.g. 503
String body = httpErr.getResponseBody(); // raw response body
}
}
Fixes a2aproject#799
794811d to
3fc3085
Compare
29fa5df to
efe00bc
Compare
|
@kabir Could you review this code as well when you get a chance, please? Thank you |
|
@kabir Could you please check? |
|
Hi @ehsavoie I think you had some input on this one? |
| Assert.checkNotNullParam("message", message); | ||
| this.code = code; | ||
| this.message = message; | ||
| this.responseBody = data instanceof String s ? s : null; |
There was a problem hiding this comment.
I'd rather have an empty string instead of null. That way we could drop the @nullable on the responseBodyy
Previously, when the JSONRPC transport received a non-2xx HTTP response, it threw a plain IOException with the status code embedded as a string (e.g. "Request failed 503"). This made it impossible for callers to programmatically distinguish 4xx from 5xx errors without fragile string parsing.
This change makes the structured HTTP status code available while remaining fully backward compatible:
JSONRPCTransport.sendPostRequest now throws A2AClientException with an A2AClientHTTPError as the cause, instead of a plain IOException. All transport methods already declared 'throws A2AClientException' and already had 'catch (A2AClientException e) { throw e; }', so no caller signatures change.
A2AClientHTTPError gains a new 'responseBody' field (and getResponseBody() accessor) so callers can also inspect the raw error payload returned by the server (e.g. a gateway's JSON error body on a 429 or 503). The existing 'code', 'message', and getCode() are preserved unchanged. The old Object-typed constructor is deprecated in favour of the new String-typed one that actually stores the body.
Callers that want the status code can now opt in with a simple instanceof check, while existing catch blocks for A2AClientException continue to work without modification:
Description
Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
CONTRIBUTINGGuide.fix:which represents bug fixes, and correlates to a SemVer patch.feat:represents a new feature, and correlates to a SemVer minor.feat!:, orfix!:,refactor!:, etc., which represent a breaking change (indicated by the!) and will result in a SemVer major.Fixes #799 🦕