Skip to content

[Bug]: RestErrorMapper throws UnsupportedOperationException when an error body has a null or non-string message #1342

Closed

Description

@sumitvairagar

What happened?

RestErrorMapper.mapRestError assumes the message and error fields of an HTTP error body are JSON strings. When they are null or another JSON type, getAsString() throws UnsupportedOperationException. The method only catches JsonProcessingException, so the exception escapes every RestTransport call that gets a non-2xx response. The caller receives an unchecked exception instead of an A2AClientException, and the HTTP status and headers that A2AClientHTTPError would carry are lost.

A body like this can come from a proxy or gateway in front of the agent, for example:

{"error": {"code": 502, "message": null}}

The affected calls in RestErrorMapper.java on main (897835ef):

  • line 70: errorObj.get("message").getAsString() (Google-style {"error": {...}} format)
  • lines 85–86: node.get("error").getAsString() and node.get("message").getAsString() (legacy format)
  • line 116: detail.get("reason").getAsString() follows the same pattern

has("message") returns true when the value is JSON null, so the existing check does not prevent this.

To reproduce, add to RestErrorMapperTest:

@Test
public void testNullMessageInGoogleErrorFormatFallsBackToHttpError() {
    String body = "{\"error\": {\"code\": 502, \"message\": null}}";
    A2AClientException ex = RestErrorMapper.mapRestError(body, 502, Map.of());
    assertInstanceOf(A2AClientHTTPError.class, ex.getCause());
    assertEquals(502, ((A2AClientHTTPError) ex.getCause()).getCode());
}

Expected: an A2AClientException wrapping an A2AClientHTTPError with code 502, the same as for a non-JSON error body.
Actual: java.lang.UnsupportedOperationException: JsonNull. A non-string message such as {"detail": "upstream down"} gives UnsupportedOperationException: JsonObject.

#858 fixed the same kind of problem for the JSON-RPC error field. I'd be happy to open a PR that reads these fields only when they are JSON strings and falls back to the HTTP status otherwise, with regression tests for each case.

Relevant log output

RestErrorMapperTest.testNullMessageInGoogleErrorFormatFallsBackToHttpError:134 » UnsupportedOperation JsonNull
RestErrorMapperTest.testNonStringMessageInLegacyErrorFormatFallsBackToHttpError:143 » UnsupportedOperation JsonObject

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added a commit that references this issue on Nov 20, 2026
    586fdd5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions