Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to say Hystrix not to trigger fallback for some of the exceptions in Hystrix command

We were using the Hystrix functionality by directly extending the HystrixCommand class. But for some of the business exceptions, Hystrix's fallback method is being triggered.

I don't want to trigger the Hystrix fallback for some of the business specific exceptions. How I can achieve it without annotation based?

like image 379
LazyGuy Avatar asked May 29 '17 16:05

LazyGuy


People also ask

How do you catch exceptions in Hystrix fallback?

Simply add a Throwable parameter to the fallback method and it will receive the exception which the original command produced.

What is the fallback method used for Hystrix command?

We'll implement Hystrix fallback as a static inner class annotated with @Component. Alternatively, we could define a @Bean annotated method returning an instance of this fallback class. For more on using Spring Netflix Eureka for service discovery have a look at this article.

Can fallback throw exception?

If the fallback method throws ValidationException then the original ConnectException is thrown back to client. This behaviour is undesired because its just a ValidationException and ideally we should be sending a message to client saying there is some validation exception.

How does hystrix command work?

The @HystrixCommand is provided by a Netflix contrib library called “javanica”. Spring Cloud automatically wraps Spring beans with that annotation in a proxy that is connected to the Hystrix circuit breaker. The circuit breaker calculates when to open and close the circuit and what to do in case of a failure.


2 Answers

Use the ignoreExceptions annotation param

@HystrixCommand(ignoreExceptions = { BaseException.class, MissingServletRequestParameterException.class, TypeMismatchException.class })

See https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#error-propagation

I see you are extending the HystrixCommand instead of using the annotation, but that doesn't matter, just set that property in the command and it should have the same effect.

Unfortunately, a Hystrix Command is created by a Builder pattern, so you will have to do some hacking. The ignoreExceptions was added to DefaultProperties.java, which is used in the HystrixCommandBuilder

like image 124
sercasti Avatar answered Sep 28 '22 16:09

sercasti


If you wrap your logic in a try/catch and re-throw any exceptions in a HystrixBadRequestException then it will not trigger the fallback.

@Override
protected Object run() throws Exception {
    try {
        return //call goes here
    }
    catch (Throwable e) {
        //We wrap any exceptions in a HystrixBadRequestException because this way any other errors will not
        //trip the short circuit
        throw new HystrixBadRequestException("Exception thrown hystrix call", e);
    }
}

From the docs: http://netflix.github.io/Hystrix/javadoc/com/netflix/hystrix/exception/HystrixBadRequestException.html

An exception representing an error with provided arguments or state rather than an execution failure. Unlike all other exceptions thrown by a HystrixCommand this will not trigger fallback, not count against failure metrics and thus not trigger the circuit breaker.

NOTE: This should only be used when an error is due to user input such as IllegalArgumentException otherwise it defeats the purpose of fault-tolerance and fallback behavior.

like image 44
devo Avatar answered Sep 28 '22 15:09

devo