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?
Most of the time just using getFailedExecutionException().getMessage() gave me null values.
this gives me better results all the time.
As said in the documentation Hystrix-documentation
getFallback()
method will be thrown when: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
do as follows:
More info here
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
I haven't found a way to get the exception with Annotations either, but creating my own Command worked for me like so:
Hopefully this helps someone else as well.
I couldn't find a way to obtain the exception with the annotations, but i found
HystrixPlugins
, with that you can register aHystrixCommandExecutionHook
and you can get the exact exception in that like this :The command instance is a
GenericCommand
.