From 9a5adb505e4cfec597da5247cfe7b86b68727cc2 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Mon, 2 Jan 2023 19:56:18 +0100 Subject: [PATCH] OZG-3276 fix exception handling in interceptor --- .../GrpcCallContextInterceptor.java | 10 ++++ .../GrpcCallContextInterceptorTest.java | 47 ++++++++++++++++--- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/user-manager-server/src/main/java/de/itvsh/kop/user/common/callcontext/GrpcCallContextInterceptor.java b/user-manager-server/src/main/java/de/itvsh/kop/user/common/callcontext/GrpcCallContextInterceptor.java index b7f3cc50..68a3cd54 100644 --- a/user-manager-server/src/main/java/de/itvsh/kop/user/common/callcontext/GrpcCallContextInterceptor.java +++ b/user-manager-server/src/main/java/de/itvsh/kop/user/common/callcontext/GrpcCallContextInterceptor.java @@ -31,6 +31,7 @@ import javax.inject.Inject; import org.apache.logging.log4j.CloseableThreadContext; +import de.itvsh.kop.user.common.errorhandling.ResourceNotFoundException; import io.grpc.ForwardingServerCallListener; import io.grpc.Metadata; import io.grpc.ServerCall; @@ -128,6 +129,15 @@ public class GrpcCallContextInterceptor implements ServerInterceptor { try (var ctc = CloseableThreadContext.put(REQUEST_ID_KEY, requestId)) { startSecurityContext(); runnable.run(); + } catch (Exception e) { + if (e instanceof ResourceNotFoundException) { + LOG.error(e.getMessage()); + LOG.error("Resource not found exception"); + originCall.close(Status.NOT_FOUND, headers); + } + LOG.error("Unknown exception"); + originCall.close(Status.UNKNOWN, headers); + } finally { clearSecurityContext(); } } diff --git a/user-manager-server/src/test/java/de/itvsh/kop/user/common/callcontext/GrpcCallContextInterceptorTest.java b/user-manager-server/src/test/java/de/itvsh/kop/user/common/callcontext/GrpcCallContextInterceptorTest.java index 8717f1f9..57e9d47e 100644 --- a/user-manager-server/src/test/java/de/itvsh/kop/user/common/callcontext/GrpcCallContextInterceptorTest.java +++ b/user-manager-server/src/test/java/de/itvsh/kop/user/common/callcontext/GrpcCallContextInterceptorTest.java @@ -39,9 +39,13 @@ import org.mockito.Mock; import org.mockito.Spy; import org.springframework.util.ReflectionUtils; +import de.itvsh.kop.user.User; +import de.itvsh.kop.user.UserTestFactory; import de.itvsh.kop.user.common.callcontext.GrpcCallContextInterceptor.LogContextSettingListener; +import de.itvsh.kop.user.common.errorhandling.ResourceNotFoundException; import io.grpc.Metadata; import io.grpc.ServerCall; +import io.grpc.Status; class GrpcCallContextInterceptorTest { @@ -204,14 +208,42 @@ class GrpcCallContextInterceptorTest { verify(listener).clearSecurityContext(); } - // TOCHECK Was genau wird hier getestet? - @Test - void shouldClearSecurityContextOnException() { - var testException = new RuntimeException(); + @DisplayName("catch exception") + @Nested + class TestException { - assertThatThrownBy(() -> listener.doSurroundOn(() -> { - throw testException; - })).isSameAs(testException); + @Test + void shouldClearSecurityContextOnException() { + try { + listener.doSurroundOn(() -> { + throw new RuntimeException(); + }); + } catch (Exception e) { + verify(listener).clearSecurityContext(); + } + } + + @Test + void shouldCloseCallWithStatusNotFound() { + try { + listener.doSurroundOn(() -> { + throw new ResourceNotFoundException(User.class, UserTestFactory.ID); + }); + } catch (Exception e) { + verify(originCall).close(Status.NOT_FOUND, headers); + } + } + + @Test + void shouldCloseCallWithStatusUnknown() { + try { + listener.doSurroundOn(() -> { + throw new RuntimeException(); + }); + } catch (Exception e) { + verify(originCall).close(Status.UNKNOWN, headers); + } + } } } @@ -288,6 +320,7 @@ class GrpcCallContextInterceptorTest { listener.onReady(); verify(listener).handleMissingClientName(); + verify(originCall).close(any(), eq(headers)); } @Test -- GitLab