Skip to content
This repository was archived by the owner on Mar 13, 2021. It is now read-only.

Commit f01868e

Browse files
Dave Syertrisberg
authored andcommitted
Existing gRPC exceptions can be passed through unchanged
They aren't particularly exceptional and there is no need to wrap them again.
1 parent aae2bdb commit f01868e

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/main/java/io/projectriff/invoker/ExceptionConverter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66
import java.util.concurrent.TimeoutException;
77

88
import io.grpc.Status;
9+
import io.grpc.StatusException;
10+
import io.grpc.StatusRuntimeException;
911

1012
/**
1113
* Utility class for converting exceptions to gRPC status values.
1214
*/
1315
final class ExceptionConverter {
1416
static Status createStatus(Throwable t) {
17+
if (t instanceof StatusRuntimeException) {
18+
return ((StatusRuntimeException) t).getStatus();
19+
}
20+
if (t instanceof StatusException) {
21+
return ((StatusException) t).getStatus();
22+
}
1523
return Status.fromCode(determineCode(t)).withDescription(t.getMessage())
1624
.withCause(t);
1725
}

src/main/java/io/projectriff/invoker/JavaFunctionInvokerServer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.atomic.AtomicReference;
2222
import java.util.function.Function;
2323

24+
import io.grpc.StatusRuntimeException;
2425
import io.grpc.stub.StreamObserver;
2526
import io.projectriff.grpc.function.MessageFunctionGrpc;
2627

@@ -119,9 +120,16 @@ public StreamObserver<io.projectriff.grpc.function.FunctionProtos.Message> call(
119120
message -> responseObserver
120121
.onNext(MessageConversionUtils.toGrpc(payloadToBytes(message))),
121122
t -> {
123+
if (t instanceof StatusRuntimeException) {
124+
// Not particularly exceptional.
125+
logger.info("RPC ending: " + t.getClass().getName() + " ("
126+
+ t.getMessage() + ")");
127+
}
128+
else {
129+
t = ExceptionConverter.createStatus(t).asRuntimeException();
130+
logger.error("RPC ending", t);
131+
}
122132
responseObserver.onError(t);
123-
throw new IllegalStateException(
124-
ExceptionConverter.createStatus(t).asException());
125133
}, () -> {
126134
// Make sure the emitter is disposed (should work even if it already
127135
// was since it's idempotent)

src/test/java/io/projectriff/invoker/ExceptionConverterTests.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.concurrent.TimeoutException;
77

88
import io.grpc.Status;
9+
import io.grpc.StatusException;
10+
import io.grpc.StatusRuntimeException;
911

1012
import org.junit.Test;
1113

@@ -15,6 +17,17 @@ public class ExceptionConverterTests {
1517

1618
private static final String TEST_MESSAGE = "test message";
1719

20+
@Test
21+
public void statusRuntime() {
22+
checkStatus(new StatusRuntimeException(Status.CANCELLED), Status.Code.CANCELLED,
23+
null);
24+
}
25+
26+
@Test
27+
public void status() {
28+
checkStatus(new StatusException(Status.CANCELLED), Status.Code.CANCELLED, null);
29+
}
30+
1831
@Test
1932
public void cancelled() {
2033
checkStatus(new CancellationException(TEST_MESSAGE), Status.Code.CANCELLED);
@@ -107,10 +120,17 @@ public void unauthenticated() {
107120
}
108121

109122
private void checkStatus(Throwable cause, Status.Code expectedCode) {
123+
checkStatus(cause, expectedCode, TEST_MESSAGE);
124+
}
125+
126+
private void checkStatus(Throwable cause, Status.Code expectedCode,
127+
String description) {
110128
Status s = ExceptionConverter.createStatus(cause);
111129
assertThat(s.getCode()).isEqualTo(expectedCode);
112-
assertThat(s.getDescription()).isEqualTo(TEST_MESSAGE);
113-
assertThat(s.getCause()).isEqualTo(cause);
130+
assertThat(s.getDescription()).isEqualTo(description);
131+
if (s.getCause() != null) {
132+
assertThat(s.getCause()).isEqualTo(cause);
133+
}
114134
}
115135

116136
@SuppressWarnings("serial")

0 commit comments

Comments
 (0)