Skip to content

Commit

Permalink
protobuf: StatusProto#fromStatusAndTrailers fall-back use status (#6278)
Browse files Browse the repository at this point in the history
  • Loading branch information
creamsoup committed Oct 15, 2019
1 parent adcfb3e commit eda5e2e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
20 changes: 14 additions & 6 deletions protobuf/src/main/java/io/grpc/protobuf/StatusProto.java
Expand Up @@ -147,14 +147,16 @@ 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}.
* Extracts the {@code google.rpc.Status} from trailers, and makes sure they match the gRPC
* {@code status}. If the trailers do not contain a {@code google.rpc.Status}, it uses
* {@code status} param to generate a {@code google.rpc.Status}.
*
* @return the embedded google.rpc.Status or {@code null} if it is not present.
* @return the embedded google.rpc.Status
* @since 1.11.0
*/
@Nullable
public static com.google.rpc.Status fromStatusAndTrailers(Status status, Metadata trailers) {
public static com.google.rpc.Status fromStatusAndTrailers(
Status status, @Nullable Metadata trailers) {
checkNotNull(status, "status");
if (trailers != null) {
com.google.rpc.Status statusProto = trailers.get(STATUS_DETAILS_KEY);
if (statusProto != null) {
Expand All @@ -164,6 +166,12 @@ public static com.google.rpc.Status fromStatusAndTrailers(Status status, Metadat
return statusProto;
}
}
return null;
// fall-back to status, this is useful if the error is local. e.g. Server is unavailable.
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();
}
}
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

0 comments on commit eda5e2e

Please sign in to comment.