问题
I have a server written in Java and client written in PHP. How can client catch exception from server if anything goes wrong? I can't find anything about exception handling in gRPC documentation.
Thank you!
回答1:
For handled exceptions, call responseObserver.onError()
. If you pass in a StatusRuntimeException
or StatusException
(generally created via status.asRuntimeException()
) the status code and description will be communicated to the client. Unhandled exceptions within a callback will cancel the RPC and will continue propagating the exception (generally leading in an UncaughtExceptionHandler
being called for the executor).
回答2:
In the response at the client side (php) http://www.grpc.io/grpc/php/source-class-Grpc.UnaryCall.html#82
the status here will have the code and details fields which will determine the response code and the appropriate message if set as mentioned in Eric's response. Based on that appropriate error handling can be done at the client.
来源:https://stackoverflow.com/questions/38810657/exception-handling-in-grpc