Skip to content

fix: expose HTTP status code in JSONRPCTransport errors#798

Open
pratik3558 wants to merge 4 commits intoa2aproject:mainfrom
pratik3558:fix/jsonrpc-transport-http-status-code
Open

fix: expose HTTP status code in JSONRPCTransport errors#798
pratik3558 wants to merge 4 commits intoa2aproject:mainfrom
pratik3558:fix/jsonrpc-transport-http-status-code

Conversation

@pratik3558
Copy link
Copy Markdown

@pratik3558 pratik3558 commented Apr 17, 2026

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
    }
}

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:

  • Follow the CONTRIBUTING Guide.
  • Make your Pull Request title in the https://www.conventionalcommits.org/ specification.
    • Important Prefixes for release-please:
      • fix: which represents bug fixes, and correlates to a SemVer patch.
      • feat: represents a new feature, and correlates to a SemVer minor.
      • feat!:, or fix!:, refactor!:, etc., which represent a breaking change (indicated by the !) and will result in a SemVer major.
  • Ensure the tests pass
  • Appropriate READMEs were updated (if necessary)

Fixes #799 🦕

@pratik3558 pratik3558 force-pushed the fix/jsonrpc-transport-http-status-code branch from 4ee1f77 to 701ab66 Compare April 17, 2026 04:15
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@pratik3558 pratik3558 force-pushed the fix/jsonrpc-transport-http-status-code branch from 701ab66 to ce324f7 Compare April 17, 2026 04:21
Comment thread spec/src/main/java/org/a2aproject/sdk/spec/A2AClientHTTPError.java Outdated
Comment thread spec/src/main/java/org/a2aproject/sdk/spec/A2AClientHTTPError.java Outdated
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
@pratik3558 pratik3558 force-pushed the fix/jsonrpc-transport-http-status-code branch from 794811d to 3fc3085 Compare April 17, 2026 12:41
@pratik3558 pratik3558 requested a review from akupireddy April 17, 2026 12:56
@pratik3558 pratik3558 force-pushed the fix/jsonrpc-transport-http-status-code branch from 29fa5df to efe00bc Compare April 17, 2026 17:47
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.

fix: JSONRPCTransport swallows HTTP status code in error responses

4 participants