I am trying to handle gRPC errors properly (Java, Spring-boot app).
Basically, I need too transfer error details from gRPC server to client, but I find it hard to understand the proper usage of StreamObserver.onError();
The method doc says:
"Receives a terminating error from the stream. May only be called once and if called it must be the last method called. In particular if an exception is thrown by an implementation of onError no further calls to any method are allowed."
What does this "no further calls are allowed" mean? In the app that I maintain, they call other gRPC methods and they get java.lang.IllegalStateException: call already closed
which is just fine, as per documentation.
I am wondering - should I (the developer) terminate the current java method (which usus gRPC calls) after an error is received? Like for example throwing an exception to stop execution. Or it is expected tht gRPC is going to terminate the execution.. (something like throwing an exception from gRPC)
Basically how do I properly use onError()
and what should I expect and handle if I call it?
I need an explanation of its usage and effects.
There are two
StreamObserver
instances involved. One is for the inbound direction, which is theStreamObserver
instance you implement and pass to the gRPC library. This is theStreamObserver
containing your logic for how to handle responses. The other is for the outbound direction, which is theStreamObserver
instance that gRPC library returns to you when calling the RPC method. This is theStreamObserver
that you use to send requests. Most of the time, these twoStreamObserver
s are interacting with each other (e.g., in a fully duplexed streaming call, the responseStreamObserver
usually calls the requestStreamObserver
'sonNext()
method, this is how you achieve ping-pong behavior)."no further calls are allowed" means you should not call any more
onNext()
,onComplete()
and/oronError()
on the outbound directionStreamObserver
when the inboundStreamObserver
'sonError()
method is invoked, even if your implementation for the inboundonError()
throws an exception. Since the inboundStreamObserver
is invoked asynchronously, it has nothing to do with your method that encloses theStreamObserver
's implementation.For example:
It has nothing to do with the
start()
method.The doc is also mentioning that you should not do things like
It's mostly for user's own
StreamObserver
implementations.StreamObserver
's returned by gRPC never throws.