Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get failure exception in @HystrixCommand fallback method

Is there a way to get the reason a HystrixCommand failed when using the @HystrixCommand annotation within a Spring Boot application? It looks like if you implement your own HystrixCommand, you have access to the getFailedExecutionException but how can you get access to this when using the annotation? I would like to be able to do different things in the fallback method based on the type of exception that occurred. Is this possible?

I saw a note about HystrixRequestContext.initializeContext() but the HystrixRequestContext doesn't give you access to anything, is there a different way to use that context to get access to the exceptions?

like image 238
Andrew Serff Avatar asked Sep 28 '15 19:09

Andrew Serff


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.

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.

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.

Which of the following error types does not throw HystrixRuntimeException?

Hystrix does not throw HystrixRuntimeException, and instead empty message error. Bookmark this question.


3 Answers

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

From https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica

    @HystrixCommand(fallbackMethod = "fallback1")
    User getUserById(String id) {
        throw new RuntimeException("getUserById command failed");
    }

    @HystrixCommand(fallbackMethod = "fallback2")
    User fallback1(String id, Throwable e) {
        assert "getUserById command failed".equals(e.getMessage());
        throw new RuntimeException("fallback1 failed");
    }
like image 62
MattJ Avatar answered Oct 27 '22 13:10

MattJ


I haven't found a way to get the exception with Annotations either, but creating my own Command worked for me like so:

public static class DemoCommand extends HystrixCommand<String> {

    protected DemoCommand() {
        super(HystrixCommandGroupKey.Factory.asKey("Demo"));
    }

    @Override
    protected String run() throws Exception {
        throw new RuntimeException("failed!");
    }

    @Override
    protected String getFallback() {
        System.out.println("Events (so far) in Fallback: " + getExecutionEvents());
        return getFailedExecutionException().getMessage();
    }

}

Hopefully this helps someone else as well.

like image 38
Andrew Serff Avatar answered Oct 27 '22 13:10

Andrew Serff


As said in the documentation Hystrix-documentation getFallback() method will be thrown when:

  1. Whenever a command execution fails: when an exception is thrown by construct() or run()
  2. When the command is short-circuited because the circuit is open
  3. When the command’s thread pool and queue or semaphore are at capacity
  4. When the command has exceeded its timeout length.

So you can easily get what raised your fallback method called by assigning the the execution exception to a Throwable object.

Assuming your HystrixCommand returns a String

public class ExampleTask extends HystrixCommand<String> {
   //Your class body
}

do as follows:

@Override
    protected ErrorCodes getFallback() {
        Throwable t = getExecutionException();
        if (circuitBreaker.isOpen()) {
            // Log or something
        } else if (t instanceof RejectedExecutionException) {
            // Log and get the threadpool name, could be useful
        } else {
            // Maybe something else happened
        }
        return "A default String"; // Avoid using any HTTP request or ypu will need to wrap it also in HystrixCommand
    }

More info here

like image 35
vicco Avatar answered Oct 27 '22 14:10

vicco