Quarkus: ExceptionMapper doesn't work for NotFoundException

Created on 17 Jul 2019  Â·  10Comments  Â·  Source: quarkusio/quarkus

Describe the bug
ExceptionMapper works fine for Exceptions, WebApplicationExceptions and whatever exception you create FooException, BarException, etc...

But it does not work for NotFoundException. Instead the default Resource Not Found page is displayed

Expected behavior
When a NotFoundException occurs, the mapper should take precedence over the default Resource Not Found

Actual behavior
Does not matter if there are any Mapper registered for NotFoundExecption, the html page is displayed

To Reproduce
Create a Mapper for every exception

public class NotFoundMapper implements ExceptionMapper<Exception> {
    @Override
    public Response toResponse(Exception exception) {
        int code = 500;
        if (exception instanceof WebApplicationException) {
            code = ((WebApplicationException) exception).getResponse().getStatus();
        }
        return Response.status(code)
                .entity(Json.createObjectBuilder().add("error", exception.getMessage()).add("code", code).build())
                .build();
    }
}

Then, throw an NotFoundException on an endpoint

    @GET
    @Path("/{id}")
    public Transf findById(@PathParam("id") Long id) {
        Transf t = Transf.findById(id);
        if (t == null) {
            throw new NotFoundException();
        }
        return t;
    }

The result is an HTML page.
Changing the NotFoundException for any other exception (even WebApplicationException) works fine and the json with the error status is returned.

Configuration

21:40:32 INFO  [io.quarkus]] (executor-thread-1) Installed features: [agroal, cdi, hibernate-orm, jdbc-h2, narayana-jta, resteasy, resteasy-jsonb]

Environment:

Microsoft Windows [Version 10.0.17134.885]
java version "1.8.0_151"
INFO  [io.quarkus]] (executor-thread-1) Quarkus 0.19.1 started in 0.835s. Listening on: http://[::]:8080

* Response page snapshot*
As it returns a Content-Type:application/json + the html content the browser does not render the html page

image

kinbug

Most helpful comment

I wonder if we should enable this only if we don't have another mapper for
this exception.

I'm also wondering if it's a good idea to return HTML when asking for JSON
(which is a very common use case).

On Wed, Jul 17, 2019 at 10:48 PM Gwenneg Lepage notifications@github.com
wrote:

Hi @barata0 https://github.com/barata0!

The Resource Not Found page is displayed because of the
NotFoundExceptionMapper
https://github.com/quarkusio/quarkus/blob/master/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/NotFoundExceptionMapper.java
class which is part of the quarkus-resteasy extension.

This mapper should only be active in development mode according to what
I'm seeing here: ResteasyProcessor#L91
https://github.com/quarkusio/quarkus/blob/master/extensions/resteasy/deployment/src/main/java/io/quarkus/resteasy/deployment/ResteasyProcessor.java#L91
.

Does your issue also happen in production mode? Your mapper should work as
long as it is annotated with @Provider which is missing in your
reproducer code.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/quarkusio/quarkus/issues/3253?email_source=notifications&email_token=AAJYOBIHTH5QGD4WTPG2DYLP76AQPA5CNFSM4IEJHHZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2GRKXI#issuecomment-512562525,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJYOBJ5H6RUTEUJ57LTFY3P76AQPANCNFSM4IEJHHZA
.

All 10 comments

Hi @barata0!

The Resource Not Found page is displayed because of the NotFoundExceptionMapper class which is part of the quarkus-resteasy extension.

This mapper should only be active in development mode according to what I'm seeing here: ResteasyProcessor#L91.

Does your issue also happen in production mode? Your mapper should work as long as it is annotated with @Provider which is missing in your reproducer code.

I wonder if we should enable this only if we don't have another mapper for
this exception.

I'm also wondering if it's a good idea to return HTML when asking for JSON
(which is a very common use case).

On Wed, Jul 17, 2019 at 10:48 PM Gwenneg Lepage notifications@github.com
wrote:

Hi @barata0 https://github.com/barata0!

The Resource Not Found page is displayed because of the
NotFoundExceptionMapper
https://github.com/quarkusio/quarkus/blob/master/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/NotFoundExceptionMapper.java
class which is part of the quarkus-resteasy extension.

This mapper should only be active in development mode according to what
I'm seeing here: ResteasyProcessor#L91
https://github.com/quarkusio/quarkus/blob/master/extensions/resteasy/deployment/src/main/java/io/quarkus/resteasy/deployment/ResteasyProcessor.java#L91
.

Does your issue also happen in production mode? Your mapper should work as
long as it is annotated with @Provider which is missing in your
reproducer code.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/quarkusio/quarkus/issues/3253?email_source=notifications&email_token=AAJYOBIHTH5QGD4WTPG2DYLP76AQPA5CNFSM4IEJHHZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2GRKXI#issuecomment-512562525,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJYOBJ5H6RUTEUJ57LTFY3P76AQPANCNFSM4IEJHHZA
.

I think that Quarkus should implements all it's error handling at a lower level.

As I saw in the code, it's a specific handling of NotFoundException created to help the developer understand that there is no operation that can handle it's request.
This is very usefull but should be implemented at a lower level (and maybe just by launching an exception, logging it to the console and letting a "higer exception handler handle it").

It can be worked around by playing with interceptor priorities (https://docs.oracle.com/javaee/7/api/javax/ws/rs/Priorities.html) but this will means that if a user provided ExceptionMapper catch NotFoundException it will also catch the case where there is no operation that can handle the request and I don't really like this.
I'm used to use NotFoundException when a resource is not found, and love to see a different exception when I make a mistake and use the wrong URL to access a ressource (aka no operation to handle the request).

@gwenneg you´re right. It only happens on DEV mode.

@loicmathieu I tried workaround with @Priority without success (thnx for pointing the code).

@gsmet What about a setting on application.properties disabling this feature?

I'm surprised priorities doesn't work. You should be able to make your exception mapper have priorities.

I don't have the time to dig more right now but it sure would be nice to have a way to fix it.

@barata0 I use an ExceptionMapper and it has precendence on the default one from the extension without using a priority even in dev mode.

I just tested it with Quarkus 0.18.

I don't know why yours don't take precedence (did you add the @Provider annotation?), as there is no priority on both exception mapper maybe I am lucky (or maybe resteasy took the first it found and sort them in alphabetical order as mine is called CustomExceptionMapper).

Anyway, to make this more convenient, I propose to add a priority to the default NotFoundException mapper of Resteasy with a value of USER + 1 as USER is the default it should have less precendence from a provided one.

@gsmet @gwenneg if it's OK I will open a PR with the oneliner change.

I agree. And we should test it works correctly.

Also, serving HTML when requesting JSON is probably not a good idea. So the exception mapper should probably be tweaked.

@gsmet created issue #3308 for JSON error response body.

@gsmet PR openned for a priority to the NotFoundExceptionMapper tested localy but no test provided (unit test seems to be complicated for this one).

As said in the PR, it only works when someone provides an ExceptionMapper for the exact same parameter type.

Thanks guys. Working fine on 0.20.0

Was this page helpful?
0 / 5 - 0 ratings