Help

Controls

PermLinkWikiLink

Built with Seam

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.

Forum: Seam Users Forum ListTopic List
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.

3 Replies:
19. Aug 2008, 17:31 CET | Link

It also works fine when you call

try{
  Exceptions.handle(e);
}catch(Exception e){
   log.info("swallow exception")
}
throw e

Which is exactly what is called in the ExceptionFilter.

05. Oct 2008, 16:09 CET | Link

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.

06. Oct 2008, 19:35 CET | Link

I can't reproduce it anymore so it most likely was fixed since I first posted this.