0

I have a REST controller which triggers downstream REST calls through Feign clients. When any kind of error (bad reply, timeout etc.) happens in the downstream, I would like to know the reason and generate REST reply with error data, as show below:

client --> my /auth @RestController
                          |
                          | --- downstream Feign client REST call to check auth --> |
                                                                                    |
                          | <--------------- faulty reply or time out --------------|
<-- graceful error reply--|           

What would be the best way to setup Feign Client and its calling code to receive Hystrix / Feign error state in order to produce "graceful error reply"?

I use Spring Boot 2.

onkami
  • 8,791
  • 17
  • 90
  • 176
  • IMHO you should use Global Exception Handler to catch the exception and return the response(success/error) with user defined fields. – Alexpandiyan Chokkan Jul 17 '19 at 09:54
  • you can use ErrorDecoder to handle exceptions on the caller site, on the other side, just return a ResponseEntity, and create a ResponseEntityExceptionHandler with @ControllerAdvice – zlaval Jul 17 '19 at 09:59
  • @zlaval could you please elaborate "just return a ResponseEntity"? – onkami Jul 17 '19 at 10:05
  • at the evening i'll have time to create a little poc, but until: GetMapping("/myurl") public ResponseEntity> myMethod(){} then you can create a new ResponseEntity object which contains the data and a status code. In the application, when error occured, just throw some custom exception, your controller advice will catch it, pack into the proper form and return that value instead of that you create in controller's get method. – zlaval Jul 17 '19 at 10:37

1 Answers1

0

Please take a look at this part of the documentation. By creating a FallbackFactory<> of your Feign interface you can catch the exception, handle it and return a default value. Instead of returning your object, you can return a ResponseEntity<> which gives you more control on what to return, eg. ResponseEntity.notFound().build().

Hope that helps! :)

TYsewyn
  • 562
  • 2
  • 9