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

Provide a way to override "prepareError()" to prepare meaningful errors in one place and handle 'jvm fatal' exceptions #289

Merged
merged 5 commits into from
Apr 10, 2023

Conversation

lobanovdmitry
Copy link
Contributor

Default static implementation of "prepareErrors" in

  • com.salesforce.reactorgrpc.stub.ServerCalls
  • com.salesforce.rx3grpc.stub.ServerCalls
  • com.salesforce.rxgrpc.stub.ServerCalls
    can't be customized. But sometimes it is very helpful:
  1. You can define generic exception mapping on a service level in one place instead of setting it in every method.
  2. Handle "JVM fatal" exceptions that are not propagated to onError reactive operator.

By overriding "prepareError" method, users can put some custom logic of handling in a service. For example:

new ReactorGreeterGrpc.GreeterImplBase() {
    @Override
    public Mono<HelloResponse> sayHello(Mono<HelloRequest> reactorRequest) {
        return reactorRequest.map(this::map);
    }

    private HelloResponse map(HelloRequest request) {
        // some critical exception can happen anywhere
        throw new NoSuchMethodError("Fatal!");
    }

    @Override
    protected Throwable onErrorMap(Throwable throwable) {
        // Override can contain any logic that will be executed in case of error
        if (throwable instanceof NoSuchMethodError) {
            return Status.INTERNAL.withDescription("NoSuchMethod:" + throwable.getMessage()).asRuntimeException();
        }
        return super.onErrorMap(throwable);
    }
};

@rmichela
Copy link
Collaborator

Thanks! This looks great. Let me get one more set of eyes. @nikolay-pshenichny Can you please take a look?

@rmichela rmichela merged commit a32f2a5 into salesforce:master Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants