users@jersey.java.net

[Jersey] Exception thrown in custom validator not caught by exception mapper

From: Jonas Oscarsson <jonas.osc_at_rsson.nu>
Date: Mon, 19 May 2014 10:21:11 +0200

Hi,

We're using bean validation in Jersey. We never want to disclose
stacktraces or any server information, so we have added a generic exception
mapper as

public class ExceptionMapper implements
javax.ws.rs.ext.ExceptionMapper<Throwable> {
  ...
}

This works very well in general, but there seems to be an edge case where
this ExceptionMapper is not used: in custom validators. Consider the
following:

A custom validation annotation:

@Target({ FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = CustomValidationValidator.class)
public @interface CustomValidation {
  String message() default "{com.example.CustomValidation.message}";
  Class<?>[] groups() default { };
  Class<? extends Payload>[] payload() default { };
}

And corresponding validation logic:

public class CustomValidationValidator implements
ConstraintValidator<CustomValidation, String> {
  @Override
  public void initialize(CustomValidation constraintAnnotation) {
    // nothing to initialize
  }

  @Override
  public boolean isValid(String value, ConstraintValidatorContext context) {
    throw new RuntimeException("Here an exception is thrown,
unexpectedly.");
  }
}

Using this validator, a text response is sent to the client, bypassing the
ExceptionMapper: "HV000028: Unexpected exception during isValid call.".

Any ideas why this happens? Is this intended behavior?

Thanks / Jonas