I created multiple custom Exceptions for various Errors that could occur during runtime. For this I'm using the @ControllerAdvice annotation and a global error handler (as described here:
You can use below code to loop through to get the root cause of exception.
public static Throwable getRootException(Throwable exception){
Throwable rootException=exception;
while(rootException.getCause()!=null){
rootException = rootException.getCause();
}
return rootException;
}
So in essence you need to get the root cause of your exception in the exception handler and build appropriate response for your clients.