Skip to content

Add StatusProto.toStatusException overload to accept Throwable #11083

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

Merged
merged 4 commits into from
Apr 25, 2024
Merged
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
19 changes: 19 additions & 0 deletions protobuf/src/main/java/io/grpc/protobuf/StatusProto.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ public static StatusException toStatusException(
return toStatus(statusProto).asException(toMetadata(statusProto, metadata));
}

/**
* Convert a {@link com.google.rpc.Status} instance to a {@link StatusException} with additional
* metadata and the root exception thrown. The exception isn't propagated over the wire.
*
* <p>The returned {@link StatusException} will wrap a {@link Status} whose code and description
* are set from the code and message in {@code statusProto}. {@code statusProto} will be
* serialized and added to {@code metadata}. {@code metadata} will be set as the metadata of the
* returned {@link StatusException}. The {@link Throwable} is the exception that is set as the
* {@code cause} of the returned {@link StatusException}.
*
* @throws IllegalArgumentException if the value of {@code statusProto.getCode()} is not a valid
* gRPC status code.
* @since 1.3.0
*/
public static StatusException toStatusException(
com.google.rpc.Status statusProto, Metadata metadata, Throwable cause) {
return toStatus(statusProto).withCause(cause).asException(toMetadata(statusProto, metadata));
}

private static Status toStatus(com.google.rpc.Status statusProto) {
Status status = Status.fromCodeValue(statusProto.getCode());
checkArgument(status.getCode().value() == statusProto.getCode(), "invalid status code");
Expand Down
8 changes: 8 additions & 0 deletions protobuf/src/test/java/io/grpc/protobuf/StatusProtoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ public void fromThrowable_shouldReturnNullIfNoEmbeddedStatus() {
assertNull(StatusProto.fromThrowable(nestedSe));
}

@Test
public void toStatusExceptionWithMetadataAndCause_shouldCaptureCause() {
RuntimeException exc = new RuntimeException("This is a test exception.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably rename exc -> cause

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ex, exception, cause, etc would be more common, but we can live with this.

StatusException se = StatusProto.toStatusException(STATUS_PROTO, new Metadata(), exc);

assertEquals(exc, se.getCause());
}

private static final Metadata.Key<String> METADATA_KEY =
Metadata.Key.of("test-metadata", Metadata.ASCII_STRING_MARSHALLER);
private static final String METADATA_VALUE = "test metadata value";
Expand Down
Loading