Skip to content

Add opt-out for default RestClient StatusHandler #36248

@seungy0

Description

@seungy0

Use case

We need to treat some non-200 responses as valid business outcomes.
Specifically, the API returns a JSON error payload with business error codes.
For a known subset of error codes, the client should NOT throw and should return the response to the caller.

We are using RestClient via RestClientAdapter (HttpServiceProxyFactory).

What I tried

I registered a custom ResponseErrorHandler via RestClient.Builder.defaultStatusHandler(...).
The handler’s hasError(...) returns false for known business error codes so they can be handled as normal responses.

However, RestClient still throws for 4xx/5xx because a default StatusHandler is always added and runs after my handler when hasError returns false.

To fully bypass it, I had to add a handler that matches ALL status codes and conditionally runs the error handling inside it. That workaround is brittle and non-obvious.

Problem

  • The default StatusHandler is added implicitly (DefaultResponseSpec constructor).
  • It is hard to discover why 4xx/5xx still throw even after registering a custom ResponseErrorHandler.
  • There is no official way to disable the default handler (NoOpResponseErrorHandler does not remove it).

Enhancement request

Provide an opt-out for the default StatusHandler, e.g.

  • RestClient.Builder.disableDefaultStatusHandler() or
  • RestClient.Builder.defaultStatusHandlerEnabled(boolean)

Default behavior should remain unchanged.

Related

Issue: #33276

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions