Feign - handling underling Exceptions - propagating error status

8.5k views Asked by At

This is similar question to Netflix Feign - Propagate Status and Exception through Microservices

I have microservices architecture with feign and hystrix. My problem is that when underling service returns error response - say status 404 - my FallbackFactory should propagate it, but it always returns 200.

Here is my code:

ErrorResponseDecoder:

@Component
public class ErrorResponseDecoder implements ErrorDecoder {

private ErrorDecoder delegate = new ErrorDecoder.Default();

@Override
public Exception decode(String methodKey, Response response) {
    HttpHeaders responseHeaders = new HttpHeaders();
    for(Map.Entry<String,Collection<String>> entry : response.headers().entrySet()) {
        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);
}
}

Fallback:

@Component
public class ServiceFallbackFactory implements FallbackFactory<ServiceFeign> {

@Override
public ServiceFeign create(final Throwable cause) {

    return new ServiceFeign() {

        @Override
        public Response getList(String date) {
            if(cause instanceof HttpStatusCodeException) {
                HttpStatusCodeException exc = (HttpStatusCodeException)cause;
                return Response.status(exc.getStatusCode().value()).entity(exc.getMessage()).build();
            } else {
                throw new RuntimeException(cause);
            }
        }
    }
}

Output:

{
  "statusType": "NOT_FOUND",
  "entity": "404 Not Found",
  "entityType": "java.lang.String",
  "metadata": {},
  "status": 404
}

Http status: 200 :( How can I make it also 404?

1

There are 1 answers

0
FazoM On BEST ANSWER

First of all - I didn't need fallback mechanism for that.

Most likely ResponseEntity would work, but I didn't try that with this solution.

For detailed design see: Hystrix - how to register ExceptionMapper