Custom error conditions to trigger fallback in Hystrix

1.9k Views Asked by At

From the understanding, Hystrix fallback logic is triggered when certain conditions occurred such as Request timeout, thread-pool running at a capacity of 100% or dependency throws exception. Beside these 3 factors can I add more conditions that are also considered as failures such as any specific HTTP Error code like 413 (payload too large)?

1

There are 1 best solutions below

0
On

The fallback method of Hystrix will be called under the following condition

  • circuit open
  • semaphore/thread pool rejection
  • execution fail (any exception thrown by your method excluding HystrixBadRequestException)
  • timeout of your method (hystrix timeout)

Only part that is directly related to the user code is execution fail.

In this case, fallback will be triggered by any exception thrown by run() method. It is same for pure Hystrix via HystrixCommand and Hystrix Javanica via annotation.

Only one exception that doesn't trigger HystrixBadRequestException

Therefore, if you want to trigger your fallback also for HTTP 413 status code, all you have to is just throwing any exception inside your method.

If you're using any library that has built-in Hystrix support like Spring Cloud Feign, you need to implement something that library requires. In case of Spring Cloud Feign, you can implement your own ErrorDecoder. The default error decoder will trigger fallback for all 4XX,5XX errors. If you don't want to trigger any fallback 4XX errors except 413, you can throw HystrixBadRequestException inside it.