簡體   English   中英

ServerInterceptor gRPC 在 Quarkus 中沒有捕捉到 SmallRye Mutiny Reactive 的異常

[英]ServerInterceptor gRPC not catching exceptions with SmallRye Mutiny Reactive in Quarkus

我有兩個可用的 gRPC 端點和一個 ServerInterceptor,它應該在拋出異常時攔截異常。

gRPC 服務定義如下:

@GrpcService
public class AccountsResource implements AccountsApiService {

    @Inject
    AccountsService accountsService;

    @Override
    public Uni<GetTppPlatformGroupIdResponse> getTppPlatformGroupId(Empty request) {
        Context.Key<String> tppIamClientIdKey = Constant.TPP_IAM_CLIENT_ID_CONTEXT_KEY;
        String tppIamClientId = tppIamClientIdKey.get(Context.current());

        return Uni.createFrom().item(() -> accountsService.getTppPlatformGroupIdResponse(tppIamClientId))
                .runSubscriptionOn(Infrastructure.getDefaultExecutor());
    }
}

gRPC 服務使用 SmallRye Mutiny Reactive 來處理請求。

其他帖子解釋說應該重寫onHalfClose方法並插入一個 try/catch 塊來捕獲自定義異常,然后將這些異常映射到可以使用的 StatusRuntimeException gRPC。 我嘗試了以下方法:

@ApplicationScoped
@GlobalInterceptor
public class ExceptionInterceptor implements ServerInterceptor {

    private static final Logger LOG = LogManager.getLogger(ExceptionInterceptor.class);

    @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 (Throwable ex) {
                handleException(ex, serverCall, metadata);
            }
        }

        @Override
        public void onReady() {
            try {
                super.onReady();
            } catch (Throwable ex) {
                handleException(ex, serverCall, metadata);
            }
        }
    }

handleException方法將 Throwable 映射到 StatusRuntimeException 並拋出 StatusRuntimeException。

到目前為止一切順利,當運行應用程序並模擬自定義異常時,ExceptionInterceptor 確實進入了onHalfClose方法。 然而,即使拋出異常,go 也不會進入 catch 塊。 而是將此錯誤消息記錄到控制台:

2022-01-24 12:07:36,232 WARN [io.qua.grp.run.ServerCalls] (executor-thread-0) gRPC service threw an exception other than StatusRuntimeException

如何解決這個問題?

使用這個https://github.com/grpc/grpc-kotlin/issues/141 ,它在 kotlin 但可以適應 Z93F725A07423FE1C8846F448B33D21。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM