Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set error code and error message? #234

Open
ghost opened this issue Feb 18, 2021 · 7 comments
Open

Set error code and error message? #234

ghost opened this issue Feb 18, 2021 · 7 comments

Comments

@ghost
Copy link

ghost commented Feb 18, 2021

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.

@dan1els
Copy link

dan1els commented Sep 14, 2021

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
Copy link

dan1els commented Sep 21, 2021

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.

@rmichela
Copy link
Collaborator

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.

@dan1els
Copy link

dan1els commented Oct 6, 2021

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.

@rmichela
Copy link
Collaborator

rmichela commented Oct 6, 2021

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.

@dan1els
Copy link

dan1els commented Oct 7, 2021

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

@lobanovdmitry
Copy link
Contributor

lobanovdmitry commented Jan 28, 2022

@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 #289

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants