Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protobuf: StatusProto#fromStatusAndTrailers fall-back use status #6278

Merged
merged 3 commits into from Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion protobuf/src/main/java/io/grpc/protobuf/StatusProto.java
Expand Up @@ -148,7 +148,8 @@ public static com.google.rpc.Status fromThrowable(Throwable t) {

/**
* Extracts the google.rpc.Status from trailers, and makes sure they match the gRPC
* {@code status}.
* {@code status}. If the trailers doesn't contain status entry, it uses {@code status} param to
creamsoup marked this conversation as resolved.
Show resolved Hide resolved
* generate status.
creamsoup marked this conversation as resolved.
Show resolved Hide resolved
*
* @return the embedded google.rpc.Status or {@code null} if it is not present.
* @since 1.11.0
Expand All @@ -164,6 +165,15 @@ public static com.google.rpc.Status fromStatusAndTrailers(Status status, Metadat
return statusProto;
}
}
// fall-back to status, this is useful if the error is local. e.g. Server is unavailable.
if (status != null) {
creamsoup marked this conversation as resolved.
Show resolved Hide resolved
com.google.rpc.Status.Builder statusBuilder = com.google.rpc.Status.newBuilder()
.setCode(status.getCode().value());
if (status.getDescription() != null) {
statusBuilder.setMessage(status.getDescription());
}
return statusBuilder.build();
}
return null;
}
}
23 changes: 14 additions & 9 deletions protobuf/src/test/java/io/grpc/protobuf/StatusProtoTest.java
Expand Up @@ -126,20 +126,25 @@ public void toStatusException_shouldThrowIfStatusCodeInvalid() throws Exception
}

@Test
public void fromThrowable_shouldReturnNullIfTrailersAreNull() {
Status status = Status.fromCodeValue(0);
public void fromThrowable_runtimeException_shouldReturnDerivedStatusIfTrailersAreNull() {
Status status = Status.UNAVAILABLE.withDescription("not available");

assertNull(StatusProto.fromThrowable(status.asRuntimeException()));
assertNull(StatusProto.fromThrowable(status.asException()));
com.google.rpc.Status statusFromThrowable =
StatusProto.fromThrowable(status.asRuntimeException());

assertEquals(statusFromThrowable.getCode(), status.getCode().value());
assertEquals(statusFromThrowable.getMessage(), status.getDescription());
}

@Test
public void fromThrowable_shouldReturnNullIfStatusDetailsKeyIsMissing() {
Status status = Status.fromCodeValue(0);
Metadata emptyMetadata = new Metadata();
public void fromThrowable_exception_shouldReturnDerivedStatusIfTrailersAreNull() {
Status status = Status.UNAVAILABLE.withDescription("not available");

com.google.rpc.Status statusFromThrowable =
StatusProto.fromThrowable(status.asException());

assertNull(StatusProto.fromThrowable(status.asRuntimeException(emptyMetadata)));
assertNull(StatusProto.fromThrowable(status.asException(emptyMetadata)));
assertEquals(statusFromThrowable.getCode(), status.getCode().value());
assertEquals(statusFromThrowable.getMessage(), status.getDescription());
}

@Test
Expand Down