grpc interceptor does not catch runtimeException

283 Views Asked by At

I'm trying to make a grpc interceptor with java and quarkus but it's not capturing the rumtimeExcpetions, could anyone shed some light ?

@GlobalInterceptor @ApplicationScoped

public class TestHandlingInterceptor implements ServerInterceptor {

@Override
public <T, R> ServerCall.Listener<T> interceptCall(
        ServerCall<T, R> serverCall, Metadata metadata, ServerCallHandler<T, R> serverCallHandler) {
        ServerCall.Listener<T> delegate = serverCallHandler.startCall(serverCall, metadata);
        return new TestForwardingServerCallListener<>(delegate, serverCall, metadata);
}
private static class TestForwardingServerCallListener<T, R>
        extends ForwardingServerCallListener.SimpleForwardingServerCallListener<T> {
    private final ServerCall<T, R> delegate;
    private final Metadata metadata;

    public TestForwardingServerCallListener(
            ServerCall.Listener<T> listener, ServerCall<T, R> serverCall, Metadata metadata) {
        super(listener);
        this.delegate = serverCall;
        this.metadata = metadata;
    }

    @Override
    public void onHalfClose() {
        try {
            super.onHalfClose();
        } catch (RuntimeException ex) {
            handlerException(ex, delegate, metadata);
            throw ex;
        }
    }
    @Override
    public void onReady() {
        try {
            super.onReady();
        } catch (RuntimeException ex) {
            handlerException(ex, delegate, metadata);
            throw ex;
        }
    }
 
private void handlerException(
            RuntimeException exception, ServerCall<T, R> serverCall, Metadata header) {
        if (exception instanceof RuntimeException) {
            serverCall.close(Status.NOT_FOUND.withDescription(exception.getMessage()),metadata);
            
        } else {
            serverCall.close(Status.UNKNOWN, header);
        }
    }
}

}

0

There are 0 best solutions below