users@jax-rs-spec.java.net

[jax-rs-spec users] Re: ParamConverter IllegalArgumentException handling

From: Santiago Pericas-Geertsen <Santiago.PericasGeertsen_at_oracle.com>
Date: Mon, 12 Jan 2015 08:54:15 -0500

Hello Nigel,

 I believe somebody else brought this up some time ago. This seems to be an issue in the spec; ParamConverter was somewhat of a late addition. Ideally we should unify the behavior for all cases, it would just be simpler for developers that way.

 I have a filed a JIRA to track this for JAX-RS 2.1.

— Santiago

> On Jan 11, 2015, at 12:32 PM, nigel_hannam_at_yahoo.com wrote:
>
> Hi
>
> I have a question regarding the expected behaviour of a JAX-RS
> implementation when a ParamConverter throws an
> IllegalArgumentException.
>
>
> Section 3.3.2 of the spec states that Exceptions thrown during mapping
> of parameters are treated the same as specified in 3.2
>
>
> Section 3.2 states:
>
> Valid parameter types for each of the above annotations are listed in
> the corresponding Javadoc, however in general (excluding @Context) the
> following types are supported:
> 1. Types for which a ParamConverter is available via a registered
> ParamConverterProvider. See Javadoc for these classes for more
> information.
> 2. Primitive types.
> 3. Types that have a constructor that accepts a single String argument.
> 4. Types that have a static method named valueOf or fromString with a
> single String argument that return an instance of the type. If both
> methods are present then valueOf MUST be used unless the type is an
> enum in which case fromString MUST be used.
> 5. List<T>, Set<T>, or SortedSet<T>, where T satisfies 3 or 4 above.
>
>
> Then follows this with:
>
> A WebApplicationException thrown during construction of field or
> property values using 3 or 4 above is processed directly as described
> in Section 3.3.4. Other exceptions thrown during construction of field
> or property values using 3 or 4 above are treated as client errors: if
> the field or property is annotated with @MatrixParam, @QueryParam or
> @PathParam then an implementation MUST generate an instance of
> NotFoundException (404 status) that wraps the thrown exception and no
> entity; if the field or property is annotated with @HeaderParam or
> @CookieParam then an implementation MUST generate an instance of
> BadRequestException (400 status) that wraps the thrown exception and no
> entity. Exceptions MUST be processed as described in Section 3.3.4.
>
> This statement on Exception handling only seems to specify the expected
> behaviour if the Exception is thrown from a String contstuctor (3) or
> valueOf/fromString method (4). It does not say anything regarding
> exceptions thrown from ParamConverter (1).
>
>
> Is the intended behavior that IllegalArgumentExceptions from
> ParamConverters are converted to 400/404 by the framework in the same
> way as the other conversion errors?
>
> Or is it intended that the IllegalArgumentException should be
> propagated out and ultimately trigger a 500 unless a specific
> ExceptionMapper is added?
>
> Apologies if this has been raised previously, or if I have missed
> something in the spec.
>
> Regards
> Nigel