salesforce / reactive-grpc

Reactive stubs for gRPC
BSD 3-Clause "New" or "Revised" License
833 stars 118 forks source link

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

Closed lobanovdmitry closed 2 years ago

lobanovdmitry commented 2 years ago

Default static implementation of "prepareErrors" in

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);
    }
};
salesforce-cla[bot] commented 2 years ago

Thanks for the contribution! Before we can merge this, we need @lobanovdmitry to sign the Salesforce.com Contributor License Agreement.