0

I have a spring application created with maven, it has the typical flow:

controller -> service -> external gateway

When querying the external gateway from service, it can return a FeingException, and I want to capture and convert into a custom one:

@RestControllerAdvice
public class GatewayErrorHandling {

   @ExceptionHandler(FeignException.class)
   public MyCustomException handleFeignException(FeignException e) {;

       return  new  MyCustomException("test");
   }
 }

Then I try to catch it into another handler but it doesnt work

@RestControllerAdvice
public class ServiceErrorHandling {

   @ExceptionHandler(MyCustomException.class)
   public void handleMyCustomException(MyCustomException e) {;

       System.out.println("exception catched");
   }
 }

I dont know what point I am missing to not be able to reach this system.out.println line

Update: after Dirk Deyne comment, the first method is:

   @ExceptionHandler(FeignException.class)
   public void handleFeignException(FeignException e) {;

       throw new  MyCustomException("test");
   }

with same result

cucuru
  • 3,456
  • 8
  • 40
  • 74
  • 2
    `throw new MyCustomException("test")` instead of returning it – Dirk Deyne Feb 23 '23 at 09:31
  • umm I'm having same result, the other exceptionHandler is not catching it – cucuru Feb 23 '23 at 09:54
  • 2
    On the other hand, if I correctly understand what you are trying to do, chained exception handlers do not work. Per request only one handler can be invoked. – Dirk Deyne Feb 23 '23 at 12:07
  • Is it because you want to return a similar response(only a few changes based on exception) for both exceptions? – Coder Feb 23 '23 at 12:08
  • Also, you can put multiple `@ExceptionHandler` methods in the same handler class, for the different Exception classes. – dbreaux Feb 23 '23 at 14:11

0 Answers0