Skip to content

Return response body in FeignException from errorReading (#2618)#3415

Open
seonwooj0810 wants to merge 1 commit into
OpenFeign:masterfrom
seonwooj0810:fix/issue-2618-errorreading-response-body
Open

Return response body in FeignException from errorReading (#2618)#3415
seonwooj0810 wants to merge 1 commit into
OpenFeign:masterfrom
seonwooj0810:fix/issue-2618-errorreading-response-body

Conversation

@seonwooj0810

Copy link
Copy Markdown
Contributor

Fixes #2618

Problem

FeignException.errorReading(...) builds the exception by passing request.body() and request.headers() into the constructor parameters named responseBody / responseHeaders:

return new FeignException(
    response.status(),
    format(...),
    request,
    cause,
    request.body(),     // <- responseBody slot
    request.headers()); // <- responseHeaders slot

As reported in #2618, this means that when an IOException is thrown while reading/decoding the response (see InvocationContext.decode and ResponseHandler.handleResponse), FeignException.responseBody() / content() / contentUTF8() / responseHeaders() return the request body and headers instead of the response. The maintainer confirmed in the thread that this looks like a bug.

Fix

Read the response body into a byte[] and pass the response headers, mirroring the existing errorStatus(...) method. Reading is wrapped in a try/catch (IOException ignored) so a streamed or already-consumed body degrades gracefully to an empty body rather than throwing (consistent with errorStatus).

Test evidence

Strengthened the existing FeignExceptionTest.canCreateWithRequestAndResponse to assert the content: the exception now carries the response body ("response") rather than the request body ("data"), and exposes the response headers. Before the fix this assertion fails (contentUTF8() returned "data").

./mvnw -pl core test -Dtest=FeignExceptionTest
Tests run: 14, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS

Verification done: (1) no in-flight PR (gh pr list --search "responseBody"/"errorReading" empty); (2) the only self-claim is a 2024-11-03 question that never produced a PR (>21 days stale); (3) code-focused change in FeignException.java + test; (4) confirmed the request.body() pattern still present on current master; (5) maintainer acknowledged the bug in-thread. Commit is DCO signed-off; google-java-format validation passes.

)

FeignException.errorReading passed request.body() and request.headers()
into the constructor's responseBody/responseHeaders parameters, so
FeignException.responseBody()/contentUTF8()/responseHeaders() exposed the
request data instead of the response when a read failure occurred while
decoding a response.

Read the response body (mirroring errorStatus, tolerating a streamed or
already-consumed body) and pass the response headers instead.

Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>
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.

FeignException::responseBody() returns request body instead of response body

1 participant