1

I have @RestControllerAdvice (spring boot 1.4.2) that looks like this

@RestControllerAdvice
public class GlobalControllerExceptionHandler {    
    @ExceptionHandler(value = { AvailabilityException.class })
    public RestResponse availabilityException(AvailabilityException ex) {
        //logic
    }

    @ExceptionHandler(value = { HrsException.class })
    public RestResponse hrsException(HrsException ex) {
        //logic
    }
}

This class catches excpetions of type HrsException but does not catch exceptions of type AvailabilityException

HrsException

public class HrsException extends RuntimeException {
   public Integer errorCode;
   public String messageKey;
}

AvailabilityException

public class AvailabilityException extends HrsException {
}

So I'm guessing AvailabilityException is not being caught by the controller advice because it's extending HrsException, what's the explanation for this and how can I continue with this a design?

Basically I want to create a bunch of exceptions that inherits from HrsException (because I don't want duplicate code) and want to catch them in the controller advice.

lospejos
  • 1,976
  • 3
  • 19
  • 35
prettyvoid
  • 3,446
  • 6
  • 36
  • 60
  • 3
    Some discussion here: http://stackoverflow.com/questions/19498378/setting-precedence-of-multiple-controlleradvice-exceptionhandlers – Alan Hay Dec 22 '16 at 18:29
  • @AlanHay That's unrelated to my problem, I only have one controller advice in my project, my issue is not precedence. – prettyvoid Dec 26 '16 at 09:12

1 Answers1

1

There was a catch somewhere in the code that was interfering with the controller advice, if someone faces the issue make sure you have no catches in your code preventing the chain from reaching the controller advice.

prettyvoid
  • 3,446
  • 6
  • 36
  • 60