桃花长相依
我刚刚发表了一篇关于gRPC Java 中的异常处理和错误传播主题的文章。您可以使用拦截器处理异常,例如:public class ExceptionHandler implements ServerInterceptor { @Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) { ServerCall.Listener<ReqT> listener = serverCallHandler.startCall(serverCall, metadata); return new ExceptionHandlingServerCallListener<>(listener, serverCall, metadata); } private class ExceptionHandlingServerCallListener<ReqT, RespT> extends ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT> { private ServerCall<ReqT, RespT> serverCall; private Metadata metadata; ExceptionHandlingServerCallListener(ServerCall.Listener<ReqT> listener, ServerCall<ReqT, RespT> serverCall, Metadata metadata) { super(listener); this.serverCall = serverCall; this.metadata = metadata; } @Override public void onHalfClose() { try { super.onHalfClose(); } catch (RuntimeException ex) { handleException(ex, serverCall, metadata); throw ex; } } @Override public void onReady() { try { super.onReady(); } catch (RuntimeException ex) { handleException(ex, serverCall, metadata); throw ex; } } private void handleException(RuntimeException exception, ServerCall<ReqT, RespT> serverCall, Metadata metadata) { if (exception instanceof IllegalArgumentException) { serverCall.close(Status.INVALID_ARGUMENT.withDescription(exception.getMessage()), metadata); } else { serverCall.close(Status.UNKNOWN, metadata); } } }}