From 2fbdb458a57ad9b305c1c4088af96cfb30b6ef96 Mon Sep 17 00:00:00 2001 From: Jan Martiska Date: Mon, 25 Mar 2019 14:44:41 +0100 Subject: [PATCH] Avoid wrapping runtime exceptions between interceptors in some cases. Fixes #1668 --- .../src/main/java/io/quarkus/arc/Reflections.java | 9 +++++++-- .../java/io/quarkus/example/metrics/MetricsResource.java | 8 ++++++++ .../java/io/quarkus/example/test/MetricsTestCase.java | 9 +++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/Reflections.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/Reflections.java index 3f5fa9911506e..36431dd2a7922 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/Reflections.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/Reflections.java @@ -109,8 +109,13 @@ public static Object invokeMethod(Class clazz, String name, Class[] paramT method.setAccessible(true); } return method.invoke(instance, args); - } catch (NoSuchMethodException | SecurityException | IllegalArgumentException | IllegalAccessException - | InvocationTargetException e) { + } catch (InvocationTargetException e) { + Throwable t = e.getTargetException(); + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } + throw new RuntimeException("Cannot invoke method: " + clazz.getName() + "#" + name + " on " + instance, e); + } catch (NoSuchMethodException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException("Cannot invoke method: " + clazz.getName() + "#" + name + " on " + instance, e); } } diff --git a/integration-tests/main/src/main/java/io/quarkus/example/metrics/MetricsResource.java b/integration-tests/main/src/main/java/io/quarkus/example/metrics/MetricsResource.java index 3558f4ad9801e..1e4ce5c2a1234 100644 --- a/integration-tests/main/src/main/java/io/quarkus/example/metrics/MetricsResource.java +++ b/integration-tests/main/src/main/java/io/quarkus/example/metrics/MetricsResource.java @@ -18,6 +18,7 @@ import javax.inject.Inject; import javax.ws.rs.GET; +import javax.ws.rs.NotFoundException; import javax.ws.rs.Path; import org.eclipse.microprofile.metrics.Histogram; @@ -84,4 +85,11 @@ public String counterWithTags() { return "TEST"; } + @GET + @Path("/counter-throwing-not-found-exception") + @Counted(monotonic = true, name = "counter_404") + public String counterWithTagsThrowingNotFound() { + throw new NotFoundException(); + } + } diff --git a/integration-tests/main/src/test/java/io/quarkus/example/test/MetricsTestCase.java b/integration-tests/main/src/test/java/io/quarkus/example/test/MetricsTestCase.java index 78a234e804d0f..df9343bbba4ba 100644 --- a/integration-tests/main/src/test/java/io/quarkus/example/test/MetricsTestCase.java +++ b/integration-tests/main/src/test/java/io/quarkus/example/test/MetricsTestCase.java @@ -138,6 +138,15 @@ public void testVendorMetrics() { .body(containsString("vendor:memory_max_non_heap_bytes ")); } + /** + * A REST method with metrics is throwing a NotFoundException, so the client should receive 404. + */ + @Test + public void testEndpointWithMetricsThrowingException() { + RestAssured.when().get("/metricsresource/counter-throwing-not-found-exception").then() + .statusCode(404); + } + private void assertMetricExactValue(String name, String val) { RestAssured.when().get("/metrics").then() .body(containsString(name + " " + val));