Adding the list back as I dropped it by mistake.
On Tue, Jan 25, 2022 at 5:50 PM Arwin Tio <arwin.tio(a)hotmail.com> wrote:
Hi James,
Feel free to publish this back to the mailing list, if you'd like.
Thanks for the example, I have some follow-up questions:
- How does the class get hooked up to the rest of the framework? Is it
through the @Provider annotation?
Yes. The @Provider annotation is what registers it.
- Do you need to have multiple ParamConverterProviders? i.e. one for
for integers, strings, lists, enums, etc? If you want to handle multiple
types as parameters
There are some default providers for sure and I wouldn't suggest doing one
for
each type. In most cases it should just work. In this case it's
allowing Enum's to be supported by passing the Enum.name(), Enum.toString()
or the ordinal value. It's more convenience than anything.
- I see in your example that there is a clause if (!rawType.isEnum()) -
does that mean that every single ParamConverterProvider is applied to every
single parameter? If you have 100 providers, could this cause performance
issues? Is there a way to associate a converter provider to a particular
type?
To my knowledge yes, each ParamConverterProvider is invoked on each
parameter. I
would think if you have a lot that could impact performance.
There is no way that I'm aware of to associate a converter with a type.
However, returning null is the way to stop processing a
ParamConverterProvider.
Thanks for the help,
Arwin
------------------------------
*From:* James Perkins <jperkins(a)redhat.com>
*Sent:* January 25, 2022 5:13 PM
*To:* Arwin Tio <arwin.tio(a)hotmail.com>
*Subject:* Re: [resteasy] How to show helpful error messages for
malformed @QueryParam?
Hi Arwin,
I meant to reply to the list, but I guess I just replied directly instead
:) I apologize for that. I'll keep it private for now in case you don't
want any of the information public.
What you could use is a ParamConverter. Something like this may work
https://gist.github.com/jamezp/aad75e83760f565f15f4f0b54e202620.
On Tue, Jan 25, 2022 at 5:03 PM Arwin Tio <arwin.tio(a)hotmail.com> wrote:
Hi James,
Thanks for the reply. Sorry, there is a mistake in my example. In my
particular case, I have an endpoint that takes a "status" enum:
localhost:8080/api/v1/users?status=creating
But when I use the wrong status, it will throw an exception:
java.lang.IllegalArgumentException: No enum constant com.data.model.UserStatus.creating
at java.base/java.lang.Enum.valueOf(Enum.java:273)
But the client will only receive a 404, with no error message. What I was
trying to say in the original email was, if number_param received something
like a String:
localhost:8080/api/v1/users?number_param=asdf
Then it will also return a 404 as a client error, due to this exception
Caused by: java.lang.NumberFormatException: For input string: "asdf"
at
java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
What I am trying to figure out, is, instead of an opaque 404, I would like
to set it up to return helpful error messages to the client, such as:
- 'number_param' received 'asdf', expected a number/integer instead
of
a string
- 'status' received 'creating', valid values are ['created',
'pending', 'complete']
Thanks,
Arwin
------------------------------
*From:* James Perkins <jperkins(a)redhat.com>
*Sent:* January 25, 2022 4:31 PM
*To:* Arwin Tio <arwin.tio(a)hotmail.com>
*Subject:* Re: [resteasy] How to show helpful error messages for
malformed @QueryParam?
Hello Arwin,
Sorry for the late reply. What kind of argument is "123"? What I mean is
it a linked to an enum or special value of some sort?
On Fri, Jan 21, 2022 at 9:05 AM Arwin Tio <arwin.tio(a)hotmail.com> wrote:
As I understand it, a malformed @QueryParam will return a 404 with no
message. For example, localhost:8080/api/v1/users?number_param=123 will
respond with 404. Apparently, this is due to the JAX-RS spec:
```
3.2 Fields and Bean Properties
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
```
I would like the modify this behavior to:
1. Return a 400 status code, as I believe 404 causes confusion for users
(they will think the endpoint doesn't exist)
2. Show a helpful error message, for example "'123' is an invalid input
for number_param, expected number"
I have read that the way to do this is with the ExceptionMapper<E extends
Throwable> interface. Is this true? And if so, which Exception E should I
use? Is there something like a QueryParamException?
If it matters, I am using RESTEasy with Quarkus.
Thanks,
Arwin
_______________________________________________
resteasy mailing list -- resteasy(a)lists.jboss.org
To unsubscribe send an email to resteasy-leave(a)lists.jboss.org
--
James R. Perkins
JBoss by Red Hat
--
James R. Perkins
JBoss by Red Hat
--
James R. Perkins
JBoss by Red Hat