You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.
| Online: | 21 Members of 4546 |
| Forum: Seam Users |
18. Aug 2008, 23:19 CET | Link |
I am trying to handle some ConstraintViolationExceptions in the code so that I can provide a user friendly error message. After I add the FacesMessages I throw the exception again so that seam automatically rollsback the transaction. The problem is that the FacesMessages that I added are not displayed on the page only the messages that was provided in pages.xml for the exception. Am I doing something wrong? Have others experienced this and is this a bug in seam? I am using the latest SNAPSHOT of 2.0.3
Here is an example of what I am doing:
Here is my action
public String register() {
try{
user.setPassword(DigestUtils.shaHex(user.getPassword()));
user.setEnabled(true);
theSession.persist(user);
theSession.flush();
}catch(ConstraintViolationException e){
@SuppressWarnings("unchecked")
Map<String, String> violatedUniqueConstraints = (Map<String, String>) theSession.createQuery("select new map(username as username, email as email) from User u " +
"where u.username = #{user.username} or u.email = #{user.email}").uniqueResult();
if(violatedUniqueConstraints.containsKey("username") && violatedUniqueConstraints.get("username").equals(user.getUsername())){
facesMessages.add("User with username #{user.username} already exists");
}
if(violatedUniqueConstraints.containsKey("email") && violatedUniqueConstraints.get("email").equals(user.getEmail())){
facesMessages.add("User with email #{user.email} already exists");
}
//Rethrow the exception so the transaction is rolled back
throw e;
}
}
then in pages.xml if have the following so that it is redirected to the same page it was on
<exception class="org.hibernate.exception.ConstraintViolationException">
<redirect>
<message/>
</redirect>
</exception>
The interesting thing is that if I call
FacesManager.instance().redirect(Pages.getCurrentViewId());
before I rethrow the exception all messages are displayed correctly and the code in org.jboss.seam.web.ExceptionFilter.endWebRequestAfterException is also called which is what rolls back the transaction.
It also works fine when you call
try{ Exceptions.handle(e); }catch(Exception e){ log.info("swallow exception") } throw eWhich is exactly what is called in the ExceptionFilter.
It sounds like what you are doing should work. Can you file an issue in JIRA with an example we can use to reproduce.
Read about how to report a bug.
I can't reproduce it anymore so it most likely was fixed since I first posted this.