我正在尝试使用 java 和 quarkus 制作一个 grpc 拦截器,但它没有捕获 rumtimeExcpetions,有人能解释一下吗?
@GlobalInterceptor @ApplicationScoped
公共类 TestHandlingInterceptor 实现 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);
}
}
}
}