From 6e78b8b1be0df504c879cb211a6768873ab4e2f8 Mon Sep 17 00:00:00 2001 From: andrea bertagnolli Date: Thu, 17 Oct 2024 09:57:54 +0200 Subject: [PATCH] refactor: avoid logging on WebApplicationException (#4555) --- .../mapper/UnexpectedExceptionMapper.java | 6 +++--- .../mapper/UnexpectedExceptionMapperTest.java | 20 +++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/mapper/UnexpectedExceptionMapper.java b/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/mapper/UnexpectedExceptionMapper.java index a5d15d54a11..fb8abb0893c 100644 --- a/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/mapper/UnexpectedExceptionMapper.java +++ b/extensions/common/http/jersey-core/src/main/java/org/eclipse/edc/web/jersey/mapper/UnexpectedExceptionMapper.java @@ -44,11 +44,11 @@ public UnexpectedExceptionMapper(Monitor monitor) { @Override public Response toResponse(Throwable exception) { - monitor.severe("JerseyExtension: Unexpected exception caught", exception); - if (exception instanceof WebApplicationException) { - return ((WebApplicationException) exception).getResponse(); + if (exception instanceof WebApplicationException webApplicationException) { + return webApplicationException.getResponse(); } + monitor.severe("JerseyExtension: Unexpected exception caught", exception); var status = exceptionMap.getOrDefault(exception.getClass(), INTERNAL_SERVER_ERROR); return Response.status(status).build(); diff --git a/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/mapper/UnexpectedExceptionMapperTest.java b/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/mapper/UnexpectedExceptionMapperTest.java index 04bce3a7fcb..dcd0a2dee33 100644 --- a/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/mapper/UnexpectedExceptionMapperTest.java +++ b/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/mapper/UnexpectedExceptionMapperTest.java @@ -28,20 +28,36 @@ import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; class UnexpectedExceptionMapperTest { + private final Monitor monitor = mock(Monitor.class); + private final UnexpectedExceptionMapper mapper = new UnexpectedExceptionMapper(monitor); + @ParameterizedTest @ArgumentsSource(JakartaApiExceptions.class) + void toResponse_jakartaExceptions(Throwable throwable, int expectedCode) { + try (var response = mapper.toResponse(throwable)) { + assertThat(response.getStatus()).isEqualTo(expectedCode); + assertThat(response.getStatusInfo().getReasonPhrase()).isNotBlank(); + assertThat(response.getEntity()).isNull(); + verifyNoInteractions(monitor); + } + } + + @ParameterizedTest @ArgumentsSource(JavaExceptions.class) void toResponse_unexpectedExceptions(Throwable throwable, int expectedCode) { - var mapper = new UnexpectedExceptionMapper(mock(Monitor.class)); - try (var response = mapper.toResponse(throwable)) { assertThat(response.getStatus()).isEqualTo(expectedCode); assertThat(response.getStatusInfo().getReasonPhrase()).isNotBlank(); assertThat(response.getEntity()).isNull(); + verify(monitor).severe(anyString(), same(throwable)); } }