File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
main/java/io/grpc/protobuf
test/java/io/grpc/protobuf Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -103,6 +103,25 @@ public static StatusException toStatusException(
103103 return toStatus (statusProto ).asException (toMetadata (statusProto , metadata ));
104104 }
105105
106+ /**
107+ * Convert a {@link com.google.rpc.Status} instance to a {@link StatusException} with additional
108+ * metadata and the root exception thrown. The exception isn't propagated over the wire.
109+ *
110+ * <p>The returned {@link StatusException} will wrap a {@link Status} whose code and description
111+ * are set from the code and message in {@code statusProto}. {@code statusProto} will be
112+ * serialized and added to {@code metadata}. {@code metadata} will be set as the metadata of the
113+ * returned {@link StatusException}. The {@link Throwable} is the exception that is set as the
114+ * {@code cause} of the returned {@link StatusException}.
115+ *
116+ * @throws IllegalArgumentException if the value of {@code statusProto.getCode()} is not a valid
117+ * gRPC status code.
118+ * @since 1.3.0
119+ */
120+ public static StatusException toStatusException (
121+ com .google .rpc .Status statusProto , Metadata metadata , Throwable cause ) {
122+ return toStatus (statusProto ).withCause (cause ).asException (toMetadata (statusProto , metadata ));
123+ }
124+
106125 private static Status toStatus (com .google .rpc .Status statusProto ) {
107126 Status status = Status .fromCodeValue (statusProto .getCode ());
108127 checkArgument (status .getCode ().value () == statusProto .getCode (), "invalid status code" );
Original file line number Diff line number Diff line change @@ -176,6 +176,14 @@ public void fromThrowable_shouldReturnNullIfNoEmbeddedStatus() {
176176 assertNull (StatusProto .fromThrowable (nestedSe ));
177177 }
178178
179+ @ Test
180+ public void toStatusExceptionWithMetadataAndCause_shouldCaptureCause () {
181+ RuntimeException exc = new RuntimeException ("This is a test exception." );
182+ StatusException se = StatusProto .toStatusException (STATUS_PROTO , new Metadata (), exc );
183+
184+ assertEquals (exc , se .getCause ());
185+ }
186+
179187 private static final Metadata .Key <String > METADATA_KEY =
180188 Metadata .Key .of ("test-metadata" , Metadata .ASCII_STRING_MARSHALLER );
181189 private static final String METADATA_VALUE = "test metadata value" ;
You can’t perform that action at this time.
0 commit comments