Skip to content

Add ability to make error as a value. - #3559

Merged
velo merged 3 commits into
OpenFeign:masterfrom
ihsansfd:f/error-as-a-value-even-when-error-occurs
Oct 5, 2026
Merged

velo merged 3 commits into
OpenFeign:masterfrom
ihsansfd:f/error-as-a-value-even-when-error-occurs

Conversation

@ihsansfd

@ihsansfd ihsansfd commented Sep 5, 2026 •

Copy link
Copy Markdown

Solving issue #2944.

Unfortunately for status code 204 and 404, all decoders are implemented to return empty body. I don't know what's the best way to handle this, so I leave it as is (make them keep returning empty body to avoid backward incompatibility). Any guideline to handle this better is much appreciated.

This is my first PR in this repo, do tell me if there is anything need to be fixed. Thanks 🙏

@ihsansfd
ihsansfd force-pushed the f/error-as-a-value-even-when-error-occurs branch from 445782b to ea9a37e Compare September 5, 2026 16:50
@ihsansfd
ihsansfd force-pushed the f/error-as-a-value-even-when-error-occurs branch from ea9a37e to cb297c8 Compare September 12, 2026 09:42
@ihsansfd ihsansfd changed the title f/error-as-a-value-even-when-error-occurs: init Add ability to make error as a value. Sep 12, 2026
@ihsansfd
ihsansfd marked this pull request as ready for review September 12, 2026 09:49
@velo

velo commented Sep 21, 2026

Copy link
Copy Markdown
Member

Thanks for this — the design is sound and you clearly thought about Retryer, TypedResponse and decoder applicability. It's correctly targeted at master: everything is additive and binary-compatible, nothing here needs 14.x. A round of fixes and I'll take it.

Blocking

1. void methods silently swallow errors.

The new branch at InvocationContext.java:105-118 runs before the void guard and never re-checks it:

if (!shouldDecodeResponseBody) {
    ...
    return decodeErrorResponseBody(response, error);   // decoder called with void.class
}

if (isVoidType(returnType) && !decodeVoid) {           // :120 — success path only

With Feign.builder().decoder(new JacksonDecoder()).decodeErrorResponses(), a void method that receives a 500 with a JSON body returns normally — no exception at all. JacksonDecoder.canDecode is just Util.isJsonContentType(response), and decode(..., void.class) returns null without throwing. So every failed void POST/DELETE reads as a success. It also breaks the decodeVoid=false contract, which is meant to guarantee the decoder never sees a void return type.

Fold isVoidType(returnType) && !decodeVoid into shouldDecodeErrorResponseBody, and add a test for it.

2. The flag implicitly stops throwing on 404, 204 and bodiless errors.

With the stock builder (no .decoder(...)):

response today with the flag
404 + body, String return throws NotFound returns null
401 + body, String return throws Unauthorized returns the body
bodiless 401, any return type throws Unauthorized returns null

StringDecoder.canDecode returns true for status 404, status 204 or response.body() == null, so the gate at :149-150 passes and the exception disappears. The bodiless case is the one that worries me — an auth failure reading as an empty result is a bug report waiting to happen. Either exclude the 404/204/null-body cases from the new path, or document and test the change explicitly. The javadoc at BaseBuilder.java:283-287 notes the 404 envelope "arrives empty" but not that the exception is gone.

3. No README. New public functionality needs documenting — README.md:1244 (### Error Handling) is the place.

4. Test hygiene. DecodeErrorResponsesTest.java:37 never closes its MockWebServer — there's no @AfterEach, so it leaks a server, its thread pool and a port per test class. See FeignTest.java:1591 for the pattern. And :191 uses a fully-qualified new feign.optionals.OptionalDecoder(...); please use an import.

Strongly recommended

  • Mark decodeErrorResponses() @Experimental. Every other recent BaseBuilder addition is, and PredicatedDecoder — which this feature's semantics hang on — is too. It keeps the behavior out of japicmp's contract while 1 and 2 get settled.
  • Cap bufferBody (InvocationContext.java:178-187). It has no size limit, unlike disconnectResponseBodyIfNeeded eleven lines below, which stops at MAX_RESPONSE_BUFFER_SIZE. With the flag on you buffer the entire body on every 4xx/5xx before the ErrorDecoder even runs, and the decoded value then holds a second copy.
  • The AsyncFeign path has no tests — AsyncFeign.java:248 and the new AsyncResponseHandler constructor are untested.

Other coverage gaps

Beyond the void case: the !(decoder instanceof PredicatedDecoder) branch at :149 is never exercised (every decoder in the test file is a PredicatedDecoder), the ErrorDecoder-returns-null branch at :169-171 is dead, and nothing tests the default decoder — which is exactly where point 2 bites.

One smaller thing: decodeErrorResponseBody catches only FeignException (:168). That holds today because decode() normalizes everything, but if anything else escapes, the original error is lost with no addSuppressed.

@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.

Nothing has changed since the review on 09-21, so the blocking items are still open:

  1. void methods swallow errors. The new branch runs before the void check. For example, a void DELETE gets a 500 with a JSON body (JacksonDecoder + decodeErrorResponses()): decode(..., void.class) returns null and the call returns normally.
  2. Default decoder turns auth failures into values. StringDecoder.canDecode returns true for 404, 204, a null body, or a String return type. So with the stock builder, a 401 with no body returns null instead of throwing Unauthorized.
  3. Unbounded buffering. bufferBody reads the whole 4xx/5xx body into memory. The existing path stops at MAX_RESPONSE_BUFFER_SIZE.
  4. Missing tests for the void case, the default decoder, the async path, the decoder that isn't a PredicatedDecoder, and an ErrorDecoder that returns null.
  5. Still to do: add the README section under ### Error Handling, close the MockWebServer, import OptionalDecoder instead of writing it in full, and mark the new builder method @Experimental.

@ihsansfd

ihsansfd commented Oct 3, 2026

Copy link
Copy Markdown
Author

Hi velo, I'm planning to do it today...

velo added 2 commits October 5, 2026 09:07
Signed-off-by: Marvin Froeder <velo.br@gmail.com>
…r async, null ErrorDecoder and non-predicated decoders; document decodeErrorResponses

Signed-off-by: Marvin Froeder <velo.br@gmail.com>

@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.

Thanks for the feature! I pushed a follow-up for the open review items: void methods still throw, 404 and empty or bodiless errors still throw (dismiss404 stays the way to opt out of a 404), and the error body is read only up to the existing 8 KB limit. There are tests for the async path, a decoder that isn't predicated, and a null ErrorDecoder. decodeErrorResponses() is marked @experimental, and the README and CHANGELOG are updated.

@velo
velo enabled auto-merge October 5, 2026 12:11
@velo
velo merged commit 336b489 into OpenFeign:master Oct 5, 2026
3 checks passed
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.

2 participants