Quarkus Microprofile Rest Client ResponseExceptionMapper doesn't catch errors

10.9k views Asked by At

I have a Rest Client in a Quarkus (1.8.1) service defined like this:

@RegisterRestClient
@Path("/")
@Produces("application/json")
@Consumes("application/json")
public interface MyClient {
    @POST
    @Path("/{entity}")
    Response postEntity(@HeaderParam(value = "Authorization") String auth, 
            @PathParam("entity") String entity, Object payload) throws MyException;
}

And I have implemented ResponseExceptionMapper in the same package like this:

public class MyExceptionMapper implements ResponseExceptionMapper<MyException> {
    @Override
    public MyException toThrowable(Response r) {
        return new DynamicsException(r.getStatus() + " - " + r.readEntity(String.class));
    }
}

When I call the service it is currently returning a 404 error, and I expected the code in the MyExceptionMapper class to be called. However it doesn't and instead a javax.ws.rs.WebApplicationException is thrown. The stack trace includes a call to the DefaultResponseExceptionMapper. It's seems that my mapper has not been registered.

How can I register my handler for invalid responses from calls to the service?

3

There are 3 answers

0
Haroon On BEST ANSWER

You need to register MyExceptionMapper as provider to the rest client with @RegisterProvider(MyExceptionMapper.class).

@RegisterRestClient
@RegisterProvider(MyExceptionMapper.class)
@Path("/")
@Produces("application/json")
@Consumes("application/json")
public interface MyClient {
0
Pablo Caiza On

Each implementation provides a default ResponseExceptionMapper implementation that will map and invoke a response to javax.ws.rs.WebApplicationException when the response status code is >= 400. It has a priority of Integer.MAX_VALUE, and is meant to be used as a fallback whenever an error is encountered. This mapper will be registered bydefault to all client interfaces, but this can be disabled by setting an MP Config property, microprofile.rest.client.disable.default.mapper, to true.

RestClientBuilder.newBuilder().property("microprofile.rest.client.disable.default.mapper",true)

0
stakahop On

Try this solution:

@ClientExceptionMapper
    static RuntimeException toException(Response response) {
        if (response.getStatus() > 200) {
            return new RuntimeException(response.readEntity(String.class));
        }
        return null;
    }