reactive-grpc icon indicating copy to clipboard operation
reactive-grpc copied to clipboard

Set error code and error message?

Open ghost opened this issue 4 years ago • 7 comments

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.

ghost avatar Feb 18 '21 15:02 ghost

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

dan1els avatar Sep 14 '21 10:09 dan1els

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.

dan1els avatar Sep 21 '21 11:09 dan1els

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.

rmichela avatar Sep 21 '21 14:09 rmichela

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.

dan1els avatar Oct 06 '21 11:10 dan1els

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.

rmichela avatar Oct 06 '21 13:10 rmichela

Yeah, i'll try to find the time to do that

dan1els avatar Oct 07 '21 08:10 dan1els

@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

lobanovdmitry avatar Jan 28 '22 17:01 lobanovdmitry