Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @author Dave Syer
* @author Andrey Litvitski
* @author Yinghuai Fu
* @author WooSeung Kim
* @see ServerInterceptor
* @see GrpcExceptionHandler
* @see <a href="https://github.com/spring-projects/spring-grpc/issues/438">gh-438</a>
Expand Down Expand Up @@ -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<ReqT, RespT> extends SimpleForwardingServerCallListener<ReqT> {

private final Log logger = LogFactory.getLog(getClass());
Expand Down Expand Up @@ -167,15 +177,15 @@ 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);
}
catch (Throwable e) {
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));
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*
* @author Dave Syer
* @author Andrey Litvitski
* @author WooSeung Kim
*/
public class GrpcExceptionHandlerInterceptorTests {

Expand Down Expand Up @@ -77,4 +78,74 @@ void propagatesTrailersFromStatusExceptionWhenStartCallThrows() {
assertThat(extracted).isEqualTo(statusWithDetails);
}

@Test
void propagatesTrailersFromThrownStatusExceptionWhenHandlerReturnsNull() {
Status statusWithDetails = statusWithDetails();
ServerCall<Empty, Empty> call = unaryCall();
ServerCallHandler<Empty, Empty> 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<Empty, Empty> call = unaryCall();
ServerCallHandler<Empty, Empty> 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<Empty, Empty> call = unaryCall();
ServerCallHandler<Empty, Empty> 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<Empty, Empty> unaryCall() {
ServerCall<Empty, Empty> call = mock(ServerCall.class);
MethodDescriptor<Empty, Empty> method = MethodDescriptor.<Empty, Empty>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<Empty, Empty> call) {
ArgumentCaptor<io.grpc.Status> statusCaptor = ArgumentCaptor.forClass(io.grpc.Status.class);
ArgumentCaptor<Metadata> trailersCaptor = ArgumentCaptor.forClass(Metadata.class);
verify(call, times(1)).close(statusCaptor.capture(), trailersCaptor.capture());
return StatusProto.fromThrowable(new StatusException(statusCaptor.getValue(), trailersCaptor.getValue()));
}

}