protobuf: make toStatusProto return null when metadata key is absent

This commit is contained in:
Eric Gribkoff 2017-05-30 11:47:20 -07:00 committed by GitHub
parent 917f6faac6
commit 37e2131dc9
2 changed files with 15 additions and 4 deletions

View File

@ -160,11 +160,13 @@ public final class StatusProto {
private static com.google.rpc.Status toStatusProto(Status status, Metadata trailers) { private static com.google.rpc.Status toStatusProto(Status status, Metadata trailers) {
if (trailers != null) { if (trailers != null) {
com.google.rpc.Status statusProto = trailers.get(STATUS_DETAILS_KEY); com.google.rpc.Status statusProto = trailers.get(STATUS_DETAILS_KEY);
if (statusProto != null) {
checkArgument( checkArgument(
status.getCode().value() == statusProto.getCode(), status.getCode().value() == statusProto.getCode(),
"com.google.rpc.Status code must match gRPC status code"); "com.google.rpc.Status code must match gRPC status code");
return statusProto; return statusProto;
} }
}
return null; return null;
} }
} }

View File

@ -148,6 +148,15 @@ public class StatusProtoTest {
assertNull(StatusProto.fromThrowable(status.asException())); 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 @Test
public void fromThrowableWithNestedStatusRuntimeException() { public void fromThrowableWithNestedStatusRuntimeException() {
StatusRuntimeException sre = StatusProto.toStatusRuntimeException(STATUS_PROTO); StatusRuntimeException sre = StatusProto.toStatusRuntimeException(STATUS_PROTO);