Question

I'm using Hibernate Validator in an application with EJB and Spring MVC. I'm using JBoss 5, Hibernate 3 and Spring MVC version 3.

I'd like to try and catch validation exceptions at Spring Exception Resolver level and generate an error message based on the InvalidStateException message. I don't want to put exception handling logic in the data access layer; I want this to be driven by annotations and just handle the validation errors in one centralized place.

The domain model is being correctly verified and an exception of type InvalidStateException is thrown as expected when I try to create an invalid entity (e.g. violate a length constraint on a field). At the point of trying to catch the exception in my instance of a Spring Exception resolver, I find my original exception has disappeared and a javax.ejb.EJBTransactionRolledbackException has taken its place. The original exception is not in the caused by list.

My first guess was that org.hibernate.validator.InvalidStateException wasn't annotated with ApplicationException, so I installed a Hibernate event listener, caught the original exception and rethrew it as a new exception annotated with ApplicationException. This has no effect.

One further complexity is that the web tier is calling EJBs via a Remote interface. I annotated my exception class with WebFault, but to no avail.

What should I do to ensure the exception bubbles all the way up?

Was it helpful?

Solution

Try this?

protected InvalidStateException extractValidationException(Throwable ex) { Throwable e = ex; while (e != null) { if (e instanceof InvalidStateException) { return (ValidationException) e; } else if (e instanceof EJBException) { e = ((EJBException) e).getCausedByException(); } else { e = e.getCause(); } } return null; }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top