Skip to content
Snippets Groups Projects
Commit 9a5adb50 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-3276 fix exception handling in interceptor

parent 368dcf41
No related branches found
No related tags found
No related merge requests found
...@@ -31,6 +31,7 @@ import javax.inject.Inject; ...@@ -31,6 +31,7 @@ import javax.inject.Inject;
import org.apache.logging.log4j.CloseableThreadContext; import org.apache.logging.log4j.CloseableThreadContext;
import de.itvsh.kop.user.common.errorhandling.ResourceNotFoundException;
import io.grpc.ForwardingServerCallListener; import io.grpc.ForwardingServerCallListener;
import io.grpc.Metadata; import io.grpc.Metadata;
import io.grpc.ServerCall; import io.grpc.ServerCall;
...@@ -128,6 +129,15 @@ public class GrpcCallContextInterceptor implements ServerInterceptor { ...@@ -128,6 +129,15 @@ public class GrpcCallContextInterceptor implements ServerInterceptor {
try (var ctc = CloseableThreadContext.put(REQUEST_ID_KEY, requestId)) { try (var ctc = CloseableThreadContext.put(REQUEST_ID_KEY, requestId)) {
startSecurityContext(); startSecurityContext();
runnable.run(); 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(); clearSecurityContext();
} }
} }
......
...@@ -39,9 +39,13 @@ import org.mockito.Mock; ...@@ -39,9 +39,13 @@ import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import org.springframework.util.ReflectionUtils; 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.callcontext.GrpcCallContextInterceptor.LogContextSettingListener;
import de.itvsh.kop.user.common.errorhandling.ResourceNotFoundException;
import io.grpc.Metadata; import io.grpc.Metadata;
import io.grpc.ServerCall; import io.grpc.ServerCall;
import io.grpc.Status;
class GrpcCallContextInterceptorTest { class GrpcCallContextInterceptorTest {
...@@ -204,14 +208,42 @@ class GrpcCallContextInterceptorTest { ...@@ -204,14 +208,42 @@ class GrpcCallContextInterceptorTest {
verify(listener).clearSecurityContext(); verify(listener).clearSecurityContext();
} }
// TOCHECK Was genau wird hier getestet? @DisplayName("catch exception")
@Nested
class TestException {
@Test @Test
void shouldClearSecurityContextOnException() { void shouldClearSecurityContextOnException() {
var testException = new RuntimeException(); 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);
}
}
assertThatThrownBy(() -> listener.doSurroundOn(() -> { @Test
throw testException; void shouldCloseCallWithStatusUnknown() {
})).isSameAs(testException); try {
listener.doSurroundOn(() -> {
throw new RuntimeException();
});
} catch (Exception e) {
verify(originCall).close(Status.UNKNOWN, headers);
}
}
} }
} }
...@@ -288,6 +320,7 @@ class GrpcCallContextInterceptorTest { ...@@ -288,6 +320,7 @@ class GrpcCallContextInterceptorTest {
listener.onReady(); listener.onReady();
verify(listener).handleMissingClientName(); verify(listener).handleMissingClientName();
verify(originCall).close(any(), eq(headers));
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment