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.

@velo velo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The response-body/header fix in core/src/main/java/feign/FeignException.java has focused coverage in FeignExceptionTest, and I do not see a compatibility or security concern in the patch itself. I still cannot approve this head while the required ci/circleci: pr-build check is failing. Please get that check green, then I can re-review.

)

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>
@seonwooj0810 seonwooj0810 force-pushed the fix/issue-2618-errorreading-response-body branch from 0da224c to a8e3ac0 Compare June 18, 2026 11:07
@seonwooj0810

Copy link
Copy Markdown
Contributor Author

Thanks @velo. The failing ci/circleci: pr-build was a pre-existing feign-core test-compilation error on master (JavaLoggerTest/LoggerRebufferTest calling the Request.create(..., byte[], Charset) overload that had been removed), not anything introduced by this PR — this change only touches FeignException. That error has since been fixed on master by #3406 (commit 18fe771), so I've rebased this branch onto the latest master to pick it up and retrigger CI (head now a8e3ac0d).

Verified locally on the rebased branch: feign-core test sources compile and FeignExceptionTest passes (14/14). Should be green now — ready for another look whenever you have a moment. Thanks!

@velo velo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The current FeignException.errorReading change has a focused test for preserving the response body and headers, and I do not see a compatibility or security concern in the diff itself. I still cannot approve this head because the required ci/circleci: pr-build check is failing. Please get that check green, then I can take another pass.

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

2 participants