Unable to catch Validation exception

Hi all,

I’m having strange problem with catching validation exception: it got escaped somehow and here is detail description of the problem.

I added custom validator that checks if input is already present in database; the code is done in Clojure but I will describe it here so non-clojure devs can follow up too.

The code looks like this (more like pseudo code):

(defn db-validator [err]

  (reify com.vaadin.data.Validator
    (validate [this value]

       (let [result (jdbc/query "SELECT * FROM table WHERE id = ?" value)
          (when result
            (throw (com.vaadin.data.Validator$InvalidValueException. err))))))))

Essentially, it will implement
Validator.validate()
and if field is found in database, it will throw
InvalidValueException
with given error.

To perform validation, I’m doing this:

(defn do-validation [field]

  (.setValidationVisible field false)
  (try
    (.validate field)
    ;;; XXX
    true
    (catch Validator$InvalidValueException e
      (.setValidationVisible field true)
      false)))

and call it with:
(do-validation )
. Above code is the same as java code:

public boolean doValidation (AbstractField field) {
  field.setValidationVisible(false);
  try {
    field.validate();
    /* XXX */
    return true;
  } catch (InvalidValueException e) {
    field.setVaidationVisible(true);
    return false;
  }

Notice part marked with

XXX

. Even after validation fails, this function will go after that point, returning true. However, exception is thrown (since it is reported in log) but not catched in
doValidation()
.

Here is stacktrace:

com.vaadin.data.Validator$InvalidValueException: This username is already present. Try different one.
        at myapp$make_db_validator$reify__6279.validate(form-init5712720220750341912.clj:83)
[color=#FF0000]
        at com.vaadin.ui.AbstractField.validate(AbstractField.java:963)
        at com.vaadin.ui.AbstractField.validate(AbstractField.java:928)
[/color]
        at myapp$do_validation.invoke(form-init5712720220750341912.clj:46)
        at myapp$show_add_or_edit_win$reify__6297$fn__6298.invoke(form-init5712720220750341912.clj:132)
        at clojure.core$map$fn__4245.invoke(core.clj:2557)
        at clojure.lang.LazySeq.sval(LazySeq.java:40)
        at clojure.lang.LazySeq.seq(LazySeq.java:49)
        at clojure.lang.RT.seq(RT.java:484)
        at clojure.core$seq.invoke(core.clj:133)
        at clojure.core$some.invoke(core.clj:2514)
        at myapp$show_add_or_edit_win$reify__6297.buttonClick(form-init5712720220750341912.clj:133)
        at sun.reflect.GeneratedMethodAccessor26.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
        at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
        at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
        at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:979)
        at com.vaadin.ui.Button.fireClick(Button.java:393)
        at com.vaadin.ui.Button$1.click(Button.java:57)

Feel free to ignore Clojure mangled names; I highlighted in red parts that I’m suspicious about.

Any idea what went wrong? If you would like more details, I will be happy to provide them.

Thanks,
Sanel

Huh, looks like the code block do not allow coloring the code. Here is the chunk I was referring about:

  ...
        at com.vaadin.ui.AbstractField.validate(AbstractField.java:963)
        at com.vaadin.ui.AbstractField.validate(AbstractField.java:928)
  ...

Vaadin version is 7.4.1.

Best,
Sanel