spring-cloud / spring-cloud/spring-cloud-openfeign

Implement Feign ErrorDecoder that is aware of spring HttpClientErrorException and HttpServerErrorException

Open
#118 33 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted
Dominant language
Java
Stars
1.3k
Forks
838
Avg merge
2d 11h
Merged PRs (30d)
13

Description

In our consumption of feign we ran into an issue where we need access to the response status code and response body associated to a client error. We have implemented the following ErrorDecoder implementation in our code base and wanted to share it with the community. It seems like others that are familiar with spring RestOperations are accustom to HttpClientErrorException and HttpServerErrorException exception trees versus the stripped down FeignException. The only concern with the implementation below is how to handle the response body if an exception where to happen during the copying process from feign response to a given instance of spring HttpStatusCodeException:

public class SpringWebClientErrorDecoder implements ErrorDecoder {

    private ErrorDecoder delegate = new ErrorDecoder.Default();

    @Override
    public Exception decode(String methodKey, Response response) {
        HttpHeaders responseHeaders = new HttpHeaders();
        response.headers().entrySet().stream()
                .forEach(entry -> responseHeaders.put(entry.getKey(), new ArrayList<>(entry.getValue())));

        HttpStatus statusCode = HttpStatus.valueOf(response.status());
        String statusText = response.reason();

        byte[] responseBody;
        try {
            responseBody = IOUtils.toByteArray(response.body().asInputStream());
        } catch (IOException e) {
            throw new RuntimeException("Failed to process response body.", e);
        }

        if (response.status() >= 400 && response.status() <= 499) {
            return new HttpClientErrorException(statusCode, statusText, responseHeaders, responseBody, null);
        }

        if (response.status() >= 500 && response.status() <= 599) {
            return new HttpServerErrorException(statusCode, statusText, responseHeaders, responseBody, null);
        }
        return delegate.decode(methodKey, response);
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the ErrorDecoder.decode entry point and the proposed SpringWebClientErrorDecoder implementation in the issue. Review how Feign responses and Spring HttpClientErrorException and HttpServerErrorException are represented, especially response-body copying and failure handling. Done means the decoder exposes the intended status and body while preserving delegated behavior for other responses.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.