diff --git a/protobuf/src/main/java/io/grpc/protobuf/StatusProto.java b/protobuf/src/main/java/io/grpc/protobuf/StatusProto.java index 798c039420..f54e75bb49 100644 --- a/protobuf/src/main/java/io/grpc/protobuf/StatusProto.java +++ b/protobuf/src/main/java/io/grpc/protobuf/StatusProto.java @@ -160,10 +160,12 @@ public final class StatusProto { private static com.google.rpc.Status toStatusProto(Status status, Metadata trailers) { if (trailers != null) { com.google.rpc.Status statusProto = trailers.get(STATUS_DETAILS_KEY); - checkArgument( - status.getCode().value() == statusProto.getCode(), - "com.google.rpc.Status code must match gRPC status code"); - return statusProto; + if (statusProto != null) { + checkArgument( + status.getCode().value() == statusProto.getCode(), + "com.google.rpc.Status code must match gRPC status code"); + return statusProto; + } } return null; } diff --git a/protobuf/src/test/java/io/grpc/protobuf/StatusProtoTest.java b/protobuf/src/test/java/io/grpc/protobuf/StatusProtoTest.java index 8a085cbc56..505bd1cf80 100644 --- a/protobuf/src/test/java/io/grpc/protobuf/StatusProtoTest.java +++ b/protobuf/src/test/java/io/grpc/protobuf/StatusProtoTest.java @@ -148,6 +148,15 @@ public class StatusProtoTest { assertNull(StatusProto.fromThrowable(status.asException())); } + @Test + public void fromThrowable_shouldReturnNullIfStatusDetailsKeyIsMissing() { + Status status = Status.fromCodeValue(0); + Metadata emptyMetadata = new Metadata(); + + assertNull(StatusProto.fromThrowable(status.asRuntimeException(emptyMetadata))); + assertNull(StatusProto.fromThrowable(status.asException(emptyMetadata))); + } + @Test public void fromThrowableWithNestedStatusRuntimeException() { StatusRuntimeException sre = StatusProto.toStatusRuntimeException(STATUS_PROTO);