prettyvoid
prettyvoid

Reputation: 3686

ControllerAdvice not triggering for specific exceptions

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.

Upvotes: 1

Views: 1273

Answers (1)

prettyvoid
prettyvoid

Reputation: 3686

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.

Upvotes: 1

Related Questions