reactive-grpc
reactive-grpc copied to clipboard
Set error code and error message?
Hi,
How can I set error codes and error messages when using reactive-grpc with Reactor? Currently, my grpc server only sends errors with Code: Unknown and empty messages.
Thank you.
Well, I did it like that:
@Override
public Mono<ResponseMsg> throwE(Mono<RequestMsg> request) {
return request
.doOnNext(it -> {throw new IllegalStateException("Illegal state exception");})
.onErrorMap(t -> Status.INVALID_ARGUMENT
.withDescription(t.getLocalizedMessage())
.augmentDescription("augmented message")
.withCause(t)
.asRuntimeException()
);
}
It will return:
% grpcurl -plaintext localhost:8080 server.ThrowService/ThrowE
ERROR:
Code: InvalidArgument
Message: Illegal state exception
augmented message
Exceptions are actually handled with the next method: com.salesforce.reactorgrpc.stub.ServerCalls#prepareError
And unfortunately, all methods of that class are all static, so there's no way to enhance the logic in a legal way.
prepareError()
merely wraps an unhandled exception as a gRPC StatusException
, if it isn't already a StatusException
or StatusRuntimeException
.
I believe you can use onErrorResume()
to transform your unhandled Exception to a StatusException
with whatever message you want.
That's correct, but it's not possible to introduce some common logic for exception handling because of that.
For instance, we're using spring grpc starter, and it contains very handy @GrpcAdvice
and @GrpcExceptionHandler
working closer to its REST analogs. https://yidongnan.github.io/grpc-spring-boot-starter/en/server/exception-handling.html
But it can't work with this library together because this library has that static prepareError()
which goes before stater interceptor, and it's static too, so there's no way to get them work together.
I see. Can you propose an API change in a PR? I'm open to making this work if I can better understand what you need.
Yeah, i'll try to find the time to do that
@rmichela Hello, just want to ask about related issue we faced. Reactor doesn't handle "Jvm Fatal Errors" (https://projectreactor.io/docs/core/release/reference/#_handling_exceptions_in_operators_or_functions), so, for example: if somewhere in the code such exceptions occur (for example NoSuchMethodError), we can't handle it using onError(), etc... While such errors are handled in https://github.com/salesforce/reactive-grpc/blob/master/reactor/reactor-grpc-stub/src/main/java/com/salesforce/reactorgrpc/stub/ServerCalls.java#L70. So, for such issues we see no logging about such fatal exception on server side (and there is no way to add it) and client just receives error with UNKNOWN status. Can you recommend how to deal with that?
UPD: Added PR to handle this https://github.com/salesforce/reactive-grpc/pull/289