StatusRuntimeException is threw instead of StatusException for the caller

1,365 views
Skip to first unread message

王 梓佳

unread,
Jul 14, 2016, 10:02:26 PM7/14/16
to grp...@googlegroups.com

Dear team,

I'm studying the error handling from the example below. I just specified the exception type in place (1) as I want to the server side to throw a checkable exception,   but the caller side always receive the StatusRuntimeException no matter what type of exception is specified in Status in server side. Anything went wrong or my assumption is not correct?


https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/errorhandling/ErrorHandlingClient.java

  void run() throws Exception {
      ...
      public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        responseObserver.onError(Status.INTERNAL
            .withDescription("Eggplant Xerxes Crybaby Overbite Narwhal").asException()); //(1) specify it is a StatusException for the caller.
      }
     ...
  }

  void blockingCall() {
    ...
    try {
      stub.sayHello(HelloRequest.newBuilder().setName("Bart").build());
    } catch (Exception e) { //(2): here I expected a StatusException is threw, but actually it threw StatusRuntimeException.
      Status status = Status.fromThrowable(e);
    ...
  }


Regards,

Benson

Eric Anderson

unread,
Jul 15, 2016, 12:38:45 PM7/15/16
to 王 梓佳, grp...@googlegroups.com
Exceptions don't exist in grpc; you won't see an exception propagate from server to client. Instead, grpc uses Status, which has a enum "code" and a string "description". StatusRuntimeException and StatusException are part of the stubs to allow using grpc in an idiomatic way in Java. StatusException is a checked exception, and the blocking stub is not marked as throwing any checked exceptions, so the blocking stub must use StatusRuntimeException.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/HK2PR04MB0866460C9A8D30D51ED8B5E2D1330%40HK2PR04MB0866.apcprd04.prod.outlook.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages