From 76e660983db79ffe62ac9ac67f496f4062442e90 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Thu, 14 Dec 2023 06:27:38 +0100 Subject: [PATCH] Unify CatchAllExceptionMapper Signed-off-by: Jorge Bescos Gascon --- .../micrometer/cdi/HelloWorldTest.java | 1 + .../lra/LoadBalancedCoordinatorTest.java | 4 +- .../resources/CatchAllExceptionMapper.java | 51 ------------------- .../metrics/CatchAllExceptionMapper.java | 51 ------------------- .../microprofile/metrics/HelloWorldTest.java | 1 + .../server}/CatchAllExceptionMapper.java | 13 ++++- .../metrics/tck/CatchAllExceptionMapper.java | 51 ------------------- .../metrics/tck/MetricsTckCdiExtension.java | 2 + 8 files changed, 17 insertions(+), 157 deletions(-) delete mode 100644 microprofile/lra/jax-rs/src/test/java/io/helidon/microprofile/lra/resources/CatchAllExceptionMapper.java delete mode 100644 microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/CatchAllExceptionMapper.java rename {integrations/micrometer/cdi/src/test/java/io/helidon/integrations/micrometer/cdi => microprofile/server/src/main/java/io/helidon/microprofile/server}/CatchAllExceptionMapper.java (85%) delete mode 100644 microprofile/tests/tck/tck-metrics/src/test/java/io/helidon/microprofile/metrics/tck/CatchAllExceptionMapper.java diff --git a/integrations/micrometer/cdi/src/test/java/io/helidon/integrations/micrometer/cdi/HelloWorldTest.java b/integrations/micrometer/cdi/src/test/java/io/helidon/integrations/micrometer/cdi/HelloWorldTest.java index 8419f5ff7c0..8bb6a8753eb 100644 --- a/integrations/micrometer/cdi/src/test/java/io/helidon/integrations/micrometer/cdi/HelloWorldTest.java +++ b/integrations/micrometer/cdi/src/test/java/io/helidon/integrations/micrometer/cdi/HelloWorldTest.java @@ -19,6 +19,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.stream.IntStream; +import io.helidon.microprofile.server.CatchAllExceptionMapper; import io.helidon.microprofile.testing.junit5.AddBean; import io.helidon.microprofile.testing.junit5.HelidonTest; diff --git a/microprofile/lra/jax-rs/src/test/java/io/helidon/microprofile/lra/LoadBalancedCoordinatorTest.java b/microprofile/lra/jax-rs/src/test/java/io/helidon/microprofile/lra/LoadBalancedCoordinatorTest.java index 497f8fa59e1..fe31cc26999 100644 --- a/microprofile/lra/jax-rs/src/test/java/io/helidon/microprofile/lra/LoadBalancedCoordinatorTest.java +++ b/microprofile/lra/jax-rs/src/test/java/io/helidon/microprofile/lra/LoadBalancedCoordinatorTest.java @@ -33,7 +33,6 @@ import io.helidon.lra.coordinator.client.CoordinatorClient; import io.helidon.lra.coordinator.client.PropagatedHeaders; import io.helidon.microprofile.config.ConfigCdiExtension; -import io.helidon.microprofile.lra.resources.CatchAllExceptionMapper; import io.helidon.microprofile.lra.resources.CdiNestedCompleteOrCompensate; import io.helidon.microprofile.lra.resources.CommonAfter; import io.helidon.microprofile.lra.resources.DontEnd; @@ -47,6 +46,7 @@ import io.helidon.microprofile.lra.resources.StartAndAfter; import io.helidon.microprofile.lra.resources.Timeout; import io.helidon.microprofile.lra.resources.Work; +import io.helidon.microprofile.server.CatchAllExceptionMapper; import io.helidon.microprofile.server.JaxRsCdiExtension; import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.microprofile.testing.junit5.AddBean; @@ -95,7 +95,7 @@ @AddExtension(CdiComponentProvider.class) // LRA client @AddExtension(LraCdiExtension.class) -// Test resources +// resources @AddBean(CatchAllExceptionMapper.class) @AddBean(JaxRsCompleteOrCompensate.class) @AddBean(NonJaxRsCompleteOrCompensate.class) diff --git a/microprofile/lra/jax-rs/src/test/java/io/helidon/microprofile/lra/resources/CatchAllExceptionMapper.java b/microprofile/lra/jax-rs/src/test/java/io/helidon/microprofile/lra/resources/CatchAllExceptionMapper.java deleted file mode 100644 index cf01f4a938d..00000000000 --- a/microprofile/lra/jax-rs/src/test/java/io/helidon/microprofile/lra/resources/CatchAllExceptionMapper.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.microprofile.lra.resources; - -import java.lang.System.Logger.Level; - -import io.helidon.webserver.http.ServerRequest; - -import jakarta.annotation.Priority; -import jakarta.ws.rs.WebApplicationException; -import jakarta.ws.rs.core.Context; -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.ext.ExceptionMapper; -import jakarta.ws.rs.ext.Provider; - -/** - * Default mapper for exceptions that are not caught. - */ -@Provider -@Priority(5000) -public class CatchAllExceptionMapper implements ExceptionMapper { - - private static final System.Logger LOGGER = System.getLogger(CatchAllExceptionMapper.class.getName()); - @Context - private ServerRequest serverRequest; - - @Override - public Response toResponse(Exception exception) { - serverRequest.context().register("unmappedException", exception); - if (exception instanceof WebApplicationException wae) { - return wae.getResponse(); - } else { - LOGGER.log(Level.WARNING, () -> "Internal server error", exception); - return Response.serverError().build(); - } - } -} diff --git a/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/CatchAllExceptionMapper.java b/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/CatchAllExceptionMapper.java deleted file mode 100644 index ac754f70285..00000000000 --- a/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/CatchAllExceptionMapper.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.microprofile.metrics; - -import java.lang.System.Logger.Level; - -import io.helidon.webserver.http.ServerRequest; - -import jakarta.annotation.Priority; -import jakarta.ws.rs.WebApplicationException; -import jakarta.ws.rs.core.Context; -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.ext.ExceptionMapper; -import jakarta.ws.rs.ext.Provider; - -/** - * Default mapper for exceptions that are not caught. - */ -@Provider -@Priority(5000) -public class CatchAllExceptionMapper implements ExceptionMapper { - - private static final System.Logger LOGGER = System.getLogger(CatchAllExceptionMapper.class.getName()); - @Context - private ServerRequest serverRequest; - - @Override - public Response toResponse(Exception exception) { - serverRequest.context().register("unmappedException", exception); - if (exception instanceof WebApplicationException wae) { - return wae.getResponse(); - } else { - LOGGER.log(Level.WARNING, () -> "Internal server error", exception); - return Response.serverError().build(); - } - } -} diff --git a/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/HelloWorldTest.java b/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/HelloWorldTest.java index 043dff46b04..dc346892d0d 100644 --- a/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/HelloWorldTest.java +++ b/microprofile/metrics/src/test/java/io/helidon/microprofile/metrics/HelloWorldTest.java @@ -24,6 +24,7 @@ import java.util.regex.Pattern; import java.util.stream.IntStream; +import io.helidon.microprofile.server.CatchAllExceptionMapper; import io.helidon.microprofile.testing.junit5.AddBean; import io.helidon.microprofile.testing.junit5.AddConfig; import io.helidon.microprofile.testing.junit5.HelidonTest; diff --git a/integrations/micrometer/cdi/src/test/java/io/helidon/integrations/micrometer/cdi/CatchAllExceptionMapper.java b/microprofile/server/src/main/java/io/helidon/microprofile/server/CatchAllExceptionMapper.java similarity index 85% rename from integrations/micrometer/cdi/src/test/java/io/helidon/integrations/micrometer/cdi/CatchAllExceptionMapper.java rename to microprofile/server/src/main/java/io/helidon/microprofile/server/CatchAllExceptionMapper.java index 8b38fdb090d..43ff30379ed 100644 --- a/integrations/micrometer/cdi/src/test/java/io/helidon/integrations/micrometer/cdi/CatchAllExceptionMapper.java +++ b/microprofile/server/src/main/java/io/helidon/microprofile/server/CatchAllExceptionMapper.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.helidon.integrations.micrometer.cdi; +package io.helidon.microprofile.server; import java.lang.System.Logger.Level; @@ -28,16 +28,25 @@ import jakarta.ws.rs.ext.Provider; /** - * Default mapper for exceptions that are not caught. + * Mapper for exceptions that are not caught. + * + * It is not configured by default in Helidon but it is provided for explicit usage. */ @Provider @Priority(5000) public class CatchAllExceptionMapper implements ExceptionMapper { private static final System.Logger LOGGER = System.getLogger(CatchAllExceptionMapper.class.getName()); + @Context private ServerRequest serverRequest; + /** + * Default empty constructor. + */ + public CatchAllExceptionMapper() { + } + @Override public Response toResponse(Exception exception) { serverRequest.context().register("unmappedException", exception); diff --git a/microprofile/tests/tck/tck-metrics/src/test/java/io/helidon/microprofile/metrics/tck/CatchAllExceptionMapper.java b/microprofile/tests/tck/tck-metrics/src/test/java/io/helidon/microprofile/metrics/tck/CatchAllExceptionMapper.java deleted file mode 100644 index 8b98053f2ca..00000000000 --- a/microprofile/tests/tck/tck-metrics/src/test/java/io/helidon/microprofile/metrics/tck/CatchAllExceptionMapper.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.microprofile.metrics.tck; - -import java.lang.System.Logger.Level; - -import io.helidon.webserver.http.ServerRequest; - -import jakarta.annotation.Priority; -import jakarta.ws.rs.WebApplicationException; -import jakarta.ws.rs.core.Context; -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.ext.ExceptionMapper; -import jakarta.ws.rs.ext.Provider; - -/** - * Default mapper for exceptions that are not caught. - */ -@Provider -@Priority(5000) -public class CatchAllExceptionMapper implements ExceptionMapper { - - private static final System.Logger LOGGER = System.getLogger(CatchAllExceptionMapper.class.getName()); - @Context - private ServerRequest serverRequest; - - @Override - public Response toResponse(Exception exception) { - serverRequest.context().register("unmappedException", exception); - if (exception instanceof WebApplicationException wae) { - return wae.getResponse(); - } else { - LOGGER.log(Level.WARNING, () -> "Internal server error", exception); - return Response.serverError().build(); - } - } -} diff --git a/microprofile/tests/tck/tck-metrics/src/test/java/io/helidon/microprofile/metrics/tck/MetricsTckCdiExtension.java b/microprofile/tests/tck/tck-metrics/src/test/java/io/helidon/microprofile/metrics/tck/MetricsTckCdiExtension.java index 9cce6ae7083..381c02b800c 100644 --- a/microprofile/tests/tck/tck-metrics/src/test/java/io/helidon/microprofile/metrics/tck/MetricsTckCdiExtension.java +++ b/microprofile/tests/tck/tck-metrics/src/test/java/io/helidon/microprofile/metrics/tck/MetricsTckCdiExtension.java @@ -15,6 +15,8 @@ */ package io.helidon.microprofile.metrics.tck; +import io.helidon.microprofile.server.CatchAllExceptionMapper; + import jakarta.enterprise.event.Observes; import jakarta.enterprise.inject.spi.BeforeBeanDiscovery; import jakarta.enterprise.inject.spi.Extension;