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 b7f3cc5083b7d1333ef5822cfdcdb93925ae6a9f..68a3cd54397057c3bb6125990a404bcf8c3712c9 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 8717f1f96e3e23759e5afbfba68c9a5edae518a1..57e9d47eb603c60cf8051c837ed277b0f52ebe36 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