Handling exceptions thrown during commit.

Hi everyone,

I am currently working on large number of forms, the problem I’m facing is in the way Vaadin handles exceptions thrown by set methods on commit().

For example:

class Bean {
    String name;
    (...)

    public void setName(String name) {
       if(this.name.equals("final") {
           throw new RuntimeException("Human readable message.");
        }
    }
   (...)
}

class Form extends FormLayout {
    TextField name;
    Button save;
    BeanFieldGroup<Bean> binder;

    public FormLayout() {
       (... create text field and save button ...)
        binder.setItemDataSource(new Action("final"));
        binder.bindMemberFields();
       
        save.addClickListener(event -> {
            try {
                binder.commit();
            } catch(RuntimeException e) {
                Notification.show(e.getMessage());  
            }
        });
    }
}

Whenever I try to change name I am getting full stack trace attached to the TextField component (I have to mouse-over it) rather than Notification.

How can I force the Exception to be propagated from the TextField so that I can catch it in the form?

Regards,
Oktawiusz

commit() throws a CommitException, which is not a runtime exception. Replace “RuntimeException” in your catch block with “CommitException” and you should be all good to go.

Hey Kim,

I have changed RuntimeException to CommitException but the error is still not propagated correctly.

Regards,
Oktawiusz

Hard to say what you are doing, because your code example is incomplete. I took your code and made it compile (note that I’m using Java 7) and it worked just as expected. There’s some piece that you are now leaving out.

public class PlaygroundUI extends UI {

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = PlaygroundUI.class)
public static class Servlet extends VaadinServlet {
}

public static class Bean {
   String name;

   public void setName(String name) {
      if (name.equals("final")) {
         throw new RuntimeException("Human readable message.");
      }
   }

   public String getName() {
      return name;
   }
}

class Form extends FormLayout {
   TextField name;
   Button save;
   BeanFieldGroup<Bean> binder;

   public Form() {
      name = new TextField();
      save = new Button("Save");
      binder = new BeanFieldGroup<PlaygroundUI.Bean>(Bean.class);
      addComponent(name);
      addComponent(save);

      binder.setItemDataSource(new Bean());
      binder.bindMemberFields(this);

      save.addClickListener(new ClickListener() {
         @Override
         public void buttonClick(ClickEvent event) {
            try {
               binder.commit();
            } catch (CommitException e) {
               Notification.show(e.getMessage());
            }
         }
      });
   }
}

   @Override
   protected void init(VaadinRequest request) {
      final VerticalLayout layout = new VerticalLayout();
      layout.setMargin(true);
      setContent(layout);
      layout.addComponent(new Form());
   }
}