Search code examples
javaspringnetflix-feign

Netflix Feign: Response Body empty in ErrorDecoder on 404


I've implemented a custom ErrorDecoder:

public class WebShopErrorDecoder implements ErrorDecoder {

  @Override
  public Exception decode(String methodKey, Response response) {

    JacksonDecoder jacksonDecoder = new JacksonDecoder();
    try {
        ErrorResource error = (ErrorResource) jacksonDecoder.decode(response, ErrorResource.class);

        return new EShopRequestException(error, HttpStatus.valueOf(response.status()));
    } catch (IOException e) {

        return new EShopRequestException(new ErrorResource("error.internal", "Internal server error"),
                HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
}

What i want to achieve with this WebShopErrorDecoder ist to forward the error response. My problem ist, that if the response status is 404 the ErrorResource error is null.

If i change the returned response status to a 400 for example the error resource contains the response.

When requesting the consumed service directly via the browser i can see the response body with a response status of 404.

Why is the response body of a feign request empty when the service returns 404? Is there a way to get the response body?

I just started to work with feign, so if my question is unclear please let me know and i try to clarify what's missing.

Thanks in advance!


Solution

  • Okay i just found out why the response is empty. It's not feigns fault it's mine.

    Since i use JacksonDecoder i alwas get an empty response if the response status is 404 due to the following code inside JacksonDecoder.

    ..
    public Object decode(Response response, Type type) throws IOException {
        if(response.status() == 404) {
            return Util.emptyValueOf(type);
        }
    ..
    

    What I'm doing instead is using ResponseEntityDecoder.

    public class WebShopErrorDecoder implements ErrorDecoder {
    
      private final ResponseEntityDecoder decoder;
    
      public WebShopErrorDecoder(ResponseEntityDecoder decoder) {
        this.decoder = decoder;
      }
    
      @Override
      public Exception decode(String methodKey, Response response) {
        try {
            ErrorResource error = (ErrorResource) decoder.decode(response, ErrorResource.class);
    
            return new EShopRequestException(error, HttpStatus.valueOf(response.status()));
        } catch (IOException e) {
    
            return new EShopRequestException(new ErrorResource("error.internal", "Internal server error"),
                    HttpStatus.INTERNAL_SERVER_ERROR);
        }
      }
    }
    

    Now everything works fine :)