From 8740e13006d91240c7c8d0d0c8cfde0a06c7c613 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Wed, 20 Sep 2023 10:53:12 -0300 Subject: [PATCH] Bubble up WebApplicationExceptions in RestDataResourceMethodListener implementations - Fixes #35169 --- ...tyResourceMethodListenerExceptionTest.java | 43 +++++++++++++++++++ ...tyResourceMethodListenerExceptionTest.java | 43 +++++++++++++++++++ .../methods/StandardMethodImplementor.java | 4 ++ 3 files changed, 90 insertions(+) create mode 100644 extensions/panache/hibernate-orm-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/orm/rest/data/panache/deployment/entity/PanacheEntityResourceMethodListenerExceptionTest.java create mode 100644 extensions/panache/hibernate-reactive-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/reactive/rest/data/panache/deployment/entity/PanacheEntityResourceMethodListenerExceptionTest.java diff --git a/extensions/panache/hibernate-orm-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/orm/rest/data/panache/deployment/entity/PanacheEntityResourceMethodListenerExceptionTest.java b/extensions/panache/hibernate-orm-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/orm/rest/data/panache/deployment/entity/PanacheEntityResourceMethodListenerExceptionTest.java new file mode 100644 index 00000000000000..b324ad0b24b770 --- /dev/null +++ b/extensions/panache/hibernate-orm-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/orm/rest/data/panache/deployment/entity/PanacheEntityResourceMethodListenerExceptionTest.java @@ -0,0 +1,43 @@ +package io.quarkus.hibernate.orm.rest.data.panache.deployment.entity; + +import static io.restassured.RestAssured.given; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.ws.rs.ForbiddenException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.hibernate.orm.rest.data.panache.RestDataResourceMethodListener; +import io.quarkus.test.QuarkusUnitTest; + +class PanacheEntityResourceMethodListenerExceptionTest { + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar + .addClasses(Collection.class, CollectionsResource.class, AbstractEntity.class, AbstractItem.class, + Item.class, ItemsResource.class, WebApplicationExceptionResourceMethodListener.class) + .addAsResource("application.properties") + .addAsResource("import.sql")); + + @Test + void shouldReceiveWebApplicationException() { + given().accept("application/json") + .and().contentType("application/json") + .and().body("{\"name\": \"test-simple\", \"collection\": {\"id\": \"full\"}}") + .when().post("/items") + .then() + .statusCode(403); + } + + @ApplicationScoped + public static class WebApplicationExceptionResourceMethodListener implements RestDataResourceMethodListener { + + @Override + public void onBeforeAdd(Item item) { + throw new ForbiddenException("You shall not pass"); + } + } + +} diff --git a/extensions/panache/hibernate-reactive-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/reactive/rest/data/panache/deployment/entity/PanacheEntityResourceMethodListenerExceptionTest.java b/extensions/panache/hibernate-reactive-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/reactive/rest/data/panache/deployment/entity/PanacheEntityResourceMethodListenerExceptionTest.java new file mode 100644 index 00000000000000..e726e7e0932e54 --- /dev/null +++ b/extensions/panache/hibernate-reactive-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/reactive/rest/data/panache/deployment/entity/PanacheEntityResourceMethodListenerExceptionTest.java @@ -0,0 +1,43 @@ +package io.quarkus.hibernate.reactive.rest.data.panache.deployment.entity; + +import static io.restassured.RestAssured.given; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.ws.rs.ForbiddenException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.hibernate.reactive.rest.data.panache.RestDataResourceMethodListener; +import io.quarkus.test.QuarkusUnitTest; + +class PanacheEntityResourceMethodListenerExceptionTest { + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar + .addClasses(Collection.class, CollectionsResource.class, AbstractEntity.class, AbstractItem.class, + Item.class, ItemsResource.class, WebApplicationExceptionResourceMethodListener.class) + .addAsResource("application.properties") + .addAsResource("import.sql")); + + @Test + void shouldReceiveWebApplicationException() { + given().accept("application/json") + .and().contentType("application/json") + .and().body("{\"name\": \"test-simple\", \"collection\": {\"id\": \"full\"}}") + .when().post("/items") + .then() + .statusCode(403); + } + + @ApplicationScoped + public static class WebApplicationExceptionResourceMethodListener implements RestDataResourceMethodListener { + + @Override + public void onBeforeAdd(Item item) { + throw new ForbiddenException("You shall not pass"); + } + } + +} diff --git a/extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/StandardMethodImplementor.java b/extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/StandardMethodImplementor.java index 449cfc39e42709..c4201df1feea2d 100644 --- a/extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/StandardMethodImplementor.java +++ b/extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/StandardMethodImplementor.java @@ -12,6 +12,7 @@ import jakarta.ws.rs.PathParam; import jakarta.ws.rs.Produces; import jakarta.ws.rs.QueryParam; +import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.Response; @@ -78,6 +79,9 @@ protected abstract void implementInternal(ClassCreator classCreator, ResourceMet protected TryBlock implementTryBlock(BytecodeCreator bytecodeCreator, String message) { TryBlock tryBlock = bytecodeCreator.tryBlock(); + CatchBlockCreator webApplicationExceptionCatch = tryBlock.addCatch(WebApplicationException.class); + webApplicationExceptionCatch.throwException(webApplicationExceptionCatch.getCaughtException()); + webApplicationExceptionCatch.close(); CatchBlockCreator catchBlock = tryBlock.addCatch(Throwable.class); catchBlock.throwException(RestDataPanacheException.class, message, catchBlock.getCaughtException()); catchBlock.close();