diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptor.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptor.java index 231acc6c..05028231 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptor.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptor.java @@ -46,6 +46,7 @@ * @author Dave Syer * @author Andrey Litvitski * @author Yinghuai Fu + * @author WooSeung Kim * @see ServerInterceptor * @see GrpcExceptionHandler * @see gh-438 @@ -109,6 +110,15 @@ private static Metadata headers(Throwable t) { return result != null ? result : new Metadata(); } + private static StatusException statusException(Throwable exception) { + Metadata trailers = new Metadata(); + Metadata thrown = Status.trailersFromThrowable(exception); + if (thrown != null) { + trailers.merge(thrown); + } + return Status.fromThrowable(exception).asException(trailers); + } + static class ExceptionHandlerListener extends SimpleForwardingServerCallListener { private final Log logger = LogFactory.getLog(getClass()); @@ -167,7 +177,7 @@ public void onHalfClose() { private void handle(Throwable t) { this.exception = t; - StatusException statusEx = Status.fromThrowable(t).asException(); + StatusException statusEx = statusException(t); try { statusEx = this.exceptionHandler.handleException(t); } @@ -175,7 +185,7 @@ private void handle(Throwable t) { this.logger.trace("Handler unable to handle exception", t); } if (statusEx == null) { - statusEx = Status.fromThrowable(t).asException(); + statusEx = statusException(t); } try { this.call.close(statusEx.getStatus(), headers(statusEx)); @@ -204,7 +214,7 @@ static class FallbackHandler implements GrpcExceptionHandler { if (logger.isDebugEnabled()) { logger.error("Unknown exception", exception); } - return Status.fromThrowable(exception).asException(); + return statusException(exception); } return status; } diff --git a/spring-grpc-core/src/test/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptorTests.java b/spring-grpc-core/src/test/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptorTests.java index b4856564..edc0a3da 100644 --- a/spring-grpc-core/src/test/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptorTests.java +++ b/spring-grpc-core/src/test/java/org/springframework/grpc/server/exception/GrpcExceptionHandlerInterceptorTests.java @@ -34,6 +34,7 @@ * * @author Dave Syer * @author Andrey Litvitski + * @author WooSeung Kim */ public class GrpcExceptionHandlerInterceptorTests { @@ -77,4 +78,74 @@ void propagatesTrailersFromStatusExceptionWhenStartCallThrows() { assertThat(extracted).isEqualTo(statusWithDetails); } + @Test + void propagatesTrailersFromThrownStatusExceptionWhenHandlerReturnsNull() { + Status statusWithDetails = statusWithDetails(); + ServerCall call = unaryCall(); + ServerCallHandler next = (c, headers) -> new ServerCall.Listener<>() { + @Override + public void onHalfClose() { + throw StatusProto.toStatusRuntimeException(statusWithDetails); + } + }; + new GrpcExceptionHandlerInterceptor(ex -> null).interceptCall(call, new Metadata(), next).onHalfClose(); + assertThat(closedStatus(call)).isEqualTo(statusWithDetails); + } + + @Test + void propagatesTrailersFromThrownStatusExceptionWhenStartCallThrows() { + Status statusWithDetails = statusWithDetails(); + ServerCall call = unaryCall(); + ServerCallHandler next = (c, headers) -> { + throw StatusProto.toStatusRuntimeException(statusWithDetails); + }; + new GrpcExceptionHandlerInterceptor(ex -> null).interceptCall(call, new Metadata(), next); + assertThat(closedStatus(call)).isEqualTo(statusWithDetails); + } + + @Test + void propagatesTrailersFromThrownStatusExceptionWhenHandlerThrows() { + Status statusWithDetails = statusWithDetails(); + ServerCall call = unaryCall(); + ServerCallHandler next = (c, headers) -> new ServerCall.Listener<>() { + @Override + public void onHalfClose() { + throw StatusProto.toStatusRuntimeException(statusWithDetails); + } + }; + GrpcExceptionHandler handler = ex -> { + throw new IllegalStateException("handler failed"); + }; + new GrpcExceptionHandlerInterceptor(handler).interceptCall(call, new Metadata(), next).onHalfClose(); + assertThat(closedStatus(call)).isEqualTo(statusWithDetails); + } + + private static Status statusWithDetails() { + return Status.newBuilder() + .setCode(Code.FAILED_PRECONDITION_VALUE) + .setMessage("precondition failed") + .addDetails(Any.pack(Empty.getDefaultInstance())) + .build(); + } + + @SuppressWarnings("unchecked") + private static ServerCall unaryCall() { + ServerCall call = mock(ServerCall.class); + MethodDescriptor method = MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("test/Test") + .setRequestMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance())) + .build(); + when(call.getMethodDescriptor()).thenReturn(method); + return call; + } + + private static Status closedStatus(ServerCall call) { + ArgumentCaptor statusCaptor = ArgumentCaptor.forClass(io.grpc.Status.class); + ArgumentCaptor trailersCaptor = ArgumentCaptor.forClass(Metadata.class); + verify(call, times(1)).close(statusCaptor.capture(), trailersCaptor.capture()); + return StatusProto.fromThrowable(new StatusException(statusCaptor.getValue(), trailersCaptor.getValue())); + } + }