You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Al Maw <wi...@almaw.com> on 2007/10/01 11:31:43 UTC

Re: Help - Best Practice - Mapping Database Constraint Violation to User Interface

mchack wrote:
> Could someone provide a pointer/link as to the best mechanism to map DB
> constraint violations from Hibernate (or ORM layer) back to the user
> interface layer. I'm sure this has been solved but wasn't successful in
> searching for an answer.

I'm not sure if this is the best way to do it, but I use a custom 
RequestCyleProcessor that extends WebRequestCycleProcessor.

It overrides #response(RuntimeException, RequestCycle) to check the 
RuntimeException for a ConstraintViolationException (call 
exception.getCause() recursively until you find one or it's null).

If it finds one, I send the user to a special error page.

The error will be vendor-specific, unfortunately.
I use something like this for MySQL:
ConstraintViolationException e = (ConstraintViolationException)t;
String detail = e.getSQLException().getMessage();
if (detail != null && detail.startsWith("Duplicate entry '")) {
     detail = detail.replaceAll(".*'(.*)'.*", "$1");
     detail = getString(
             "DuplicateEntry",
             new SingleStringMapModel("detail", detail)
     );
}

We do this for StaleObjectStateException too (use 
StaleObjectStateException#getEntityName() for your error message).

Regards,

Al

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Help - Best Practice - Mapping Database Constraint Violation to User Interface

Posted by Igor Vaynberg <ig...@gmail.com>.
same here, but its probably much easier to just overrie
requestcycle.onruntimexception() to achieve the exact same behavior :)

-igor


On 10/1/07, Al Maw <wi...@almaw.com> wrote:
>
> mchack wrote:
> > Could someone provide a pointer/link as to the best mechanism to map DB
> > constraint violations from Hibernate (or ORM layer) back to the user
> > interface layer. I'm sure this has been solved but wasn't successful in
> > searching for an answer.
>
> I'm not sure if this is the best way to do it, but I use a custom
> RequestCyleProcessor that extends WebRequestCycleProcessor.
>
> It overrides #response(RuntimeException, RequestCycle) to check the
> RuntimeException for a ConstraintViolationException (call
> exception.getCause() recursively until you find one or it's null).
>
> If it finds one, I send the user to a special error page.
>
> The error will be vendor-specific, unfortunately.
> I use something like this for MySQL:
> ConstraintViolationException e = (ConstraintViolationException)t;
> String detail = e.getSQLException().getMessage();
> if (detail != null && detail.startsWith("Duplicate entry '")) {
>      detail = detail.replaceAll(".*'(.*)'.*", "$1");
>      detail = getString(
>              "DuplicateEntry",
>              new SingleStringMapModel("detail", detail)
>      );
> }
>
> We do this for StaleObjectStateException too (use
> StaleObjectStateException#getEntityName() for your error message).
>
> Regards,
>
> Al
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>