javaspring-boothystrixfeignnetflix

Custom error decoder stop working | Feign Hystrix


Error decoder doesn't work.

I have 2 microservices M1 M2. M1 use feign client with custom ErrorDecoder for communicating with M2. Then i added Hystrix for circuit breaking if M2 is unavailable.

Feign client:

@FeignClient(name = "M2", fallback = M2Client.Fallback.class)
public interface M2Client{

    @GetMapping("/api/v1/users/{id}")
    UserDTO getUserById(@PathVariable long id);
    
    //other endpoints

    @Component
    class Fallback implements M2Client{
        @Override
        public UserDTO getUserById(long id) {
            throw new MicroserviceException("Service is unavailable");
        }
}

Error decoder:

@Component
public class CustomErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String s, Response response) {
    //...
   }
}

But Custom ErrorDecoder stop working and all time M2 throw error, that calling fallback


Solution

  • Okay, as I understand there is a following way how handling works:

    1. After exception calling ErrorDecoder
    2. After ErrorDecoder calling Fallback.

    So i changed fallback to fallback factory and just throw exception farther in Fallback class.

        @Component
        class Fallback implements FallbackFactory<M2Client> {
            @Override
            public AuthenticationClient create(Throwable cause) {
                if (cause instanceof TimeoutException) {
                    throw new MicroserviceException("Service is unavailable");
                }
    
                if (cause instanceof RuntimeException) {
                    throw (RuntimeException) cause;
                } else {
                    throw new RuntimeException("Unhandled exception: " + cause.getMessage());
                }
            }
        }