Quarkus: ExceptionMapper not handling Exceptions thrown from injected RestClient

Created on 16 Sep 2019  路  7Comments  路  Source: quarkusio/quarkus

Describe the bug
For an ExceptionMapper register with a controller, it is not being fired for exceptions which are thrown by an injected RestClient

Expected behavior
An ExceptionMapper<Thowable> should handle any exceptions which could be caught in the controller code

Actual behavior
WebApplicationExceptions which are thrown from a RestClient are not being handled by the ExceptionMapper

To Reproduce
There is a test showing this behavious in this repo

Additional context
Initially raised as an issue on StackOverflow

kinbug

Most helpful comment

I guess, I have the same issue, but with a different set of exceptions:

ExceptionMapper<Exception> (same applies to ExceptionMapper<Throwable>) won't catch ResteasyViolationException.

But with ExceptionMapper<ResteasyViolationException> (or ExceptionMapper<ValidationException>) it works.

Very weird behavior.

I'm using Quarkus 1.0.1


pom.xml (Click to expand)

  <dependencies>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-jsonb</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-jwt</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-health</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-hibernate-validator</artifactId>
    </dependency>
  </dependencies>


DefaultExceptionHandler class (Click to expand)

@Provider
public class DefaultExceptionHandler implements ExceptionMapper<Exception> {

    @Override
    public Response toResponse(Exception exception) {
        // doesn't work for ResteasyViolationException
        // won't get called


ValidationExceptionHandler class (Click to expand)

@Provider
public class ValidationExceptionHandler implements ExceptionMapper<ResteasyViolationException> {

    @Override
    public Response toResponse(ResteasyViolationException exception) {
        // works as expected


TestResource class (Click to expand)

@Path("/test")
public class TestResource {

    @POST
    public String test(@NotNull Boolean value) {
        return "" + value;
    }

}


Test Request JSON (Click to expand)

POST /test

Providing "no content" will trigger the @NotNull constraint validation error.

All 7 comments

@kenfinnigan @asoldano could you have a look at that one? Not sure if the server or the client is the culprit. There is a reproducer.

I think the issue is that the REST Client doesn't have a media type set: https://github.com/irishshagua/quakus-exception-handler-issues/blob/master/src/main/java/demo/SomeRestClient.java#L13

Meaning it can't find a writer

I think the issue is that the REST Client doesn't have a media type set: https://github.com/irishshagua/quakus-exception-handler-issues/blob/master/src/main/java/demo/SomeRestClient.java#L13

Meaning it can't find a writer

Hey @kenfinnigan ,
I tested again with a Produces and Consumes anno on the Rest client method and it didn't make any difference to the failing test case. Just to be clear, the actual REST client call in the test should fail. The unexpected behavior is that the ExceptionHandler is not being triggered.

I guess, I have the same issue, but with a different set of exceptions:

ExceptionMapper<Exception> (same applies to ExceptionMapper<Throwable>) won't catch ResteasyViolationException.

But with ExceptionMapper<ResteasyViolationException> (or ExceptionMapper<ValidationException>) it works.

Very weird behavior.

I'm using Quarkus 1.0.1


pom.xml (Click to expand)

  <dependencies>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-jsonb</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-jwt</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-health</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-hibernate-validator</artifactId>
    </dependency>
  </dependencies>


DefaultExceptionHandler class (Click to expand)

@Provider
public class DefaultExceptionHandler implements ExceptionMapper<Exception> {

    @Override
    public Response toResponse(Exception exception) {
        // doesn't work for ResteasyViolationException
        // won't get called


ValidationExceptionHandler class (Click to expand)

@Provider
public class ValidationExceptionHandler implements ExceptionMapper<ResteasyViolationException> {

    @Override
    public Response toResponse(ResteasyViolationException exception) {
        // works as expected


TestResource class (Click to expand)

@Path("/test")
public class TestResource {

    @POST
    public String test(@NotNull Boolean value) {
        return "" + value;
    }

}


Test Request JSON (Click to expand)

POST /test

Providing "no content" will trigger the @NotNull constraint validation error.

Is it possible to update to 1.8.1.Final and see if the error is still present?

Is it possible to update to 1.8.1.Final and see if the error is still present?

I bumped the reproducable to 1.8.1.Final. Test is still failing.

Taking another look at the example code and the spec, I think the issue is that the MP REST Client spec does not support exception mappers marked with @Provider unless @RegisterProvider as on the interface to explicitly register the exception mapper with the client.

Take a look at https://download.eclipse.org/microprofile/microprofile-rest-client-1.4.1/microprofile-rest-client-1.4.1.html#_responseexceptionmapper on the specific exception mapper handling for MP REST Client

Was this page helpful?
0 / 5 - 0 ratings