Working on an api call which alters some data in DB.I am trying to provide a meaningful message from api in case there is an exception in JPA Layer. On any sql query run failure I get DataIntegrityViolationException from JPA which does not contain the exact reason of failure. Hence I am extracting the SQLException from it and then responding with the error message.
private String getSQLExceptionMessage(DataIntegrityViolationException e) {
Throwable nextException = e.getCause();
while (!nextException.getClass().equals(SQLException.class) && nextException.getCause() != null
&& !nextException.getClass().equals(nextException.getCause().getClass())) {
nextException = nextException.getCause();
}
return nextException.getMessage();
}
Is there any better way to do this?