users@jersey.java.net

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

From: Michal Gajdos <michal.gajdos_at_oracle.com>
Date: Mon, 19 May 2014 16:22:22 +0200

The generic (custom) exception mapper is not used if there is a more
specific exception mapper registered in Jersey (for example for
WebApplicationException or RuntimeException). Isn't this your case?

Michal

On 19.05.2014, 15:39 , Miles, Eric (CONT) wrote:
> I can confirm this behavior but can't comment whether it's intended or
> not. This same behavior also transpires if you have custom
> ParamConverterProviders and those throw an Exception, it will be
> wrapped in some form of a org.glassfish.jersey.server.ParamException
> and will not go through your generic exception mapper either.
>
>
> From: Jonas Oscarsson <jonas.osc_at_rsson.nu <mailto:jonas.osc_at_rsson.nu>>
> Reply-To: Jersey Users <users_at_jersey.java.net
> <mailto:users_at_jersey.java.net>>
> Date: Monday, May 19, 2014 4:21 AM
> To: Jersey Users <users_at_jersey.java.net <mailto:users_at_jersey.java.net>>
> Subject: [Jersey] Exception thrown in custom validator not caught by
> exception mapper
>
> 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
>
> ------------------------------------------------------------------------
>
> The information contained in this e-mail is confidential and/or
> proprietary to Capital One and/or its affiliates. The information
> transmitted herewith is intended only for use by the individual or
> entity to which it is addressed. If the reader of this message is not
> the intended recipient, you are hereby notified that any review,
> retransmission, dissemination, distribution, copying or other use of,
> or taking of any action in reliance upon this information is strictly
> prohibited. If you have received this communication in error, please
> contact the sender and delete the material from your computer.
>