grpc-kotlin icon indicating copy to clipboard operation
grpc-kotlin copied to clipboard

How to handle errors with Interceptor

Open mastar3104 opened this issue 5 years ago • 7 comments

When using protoc-gen-grpc-java

@GRpcService
class PancakeService : PancakeBakerServiceGrpc.PancakeBakerServiceImplBase() {
    override fun bake(request: PancakeOuterClass.BakeRequest?, responseObserver: StreamObserver<PancakeOuterClass.BakeResponse>?) {
        throw Exception()
    }
}
@GRpcGlobalInterceptor
class ErrorHandleInterceptor : ServerInterceptor {
    override fun <ReqT : Any?, RespT : Any?> interceptCall(call: ServerCall<ReqT, RespT>?, headers: Metadata?, next: ServerCallHandler<ReqT, RespT>?): ServerCall.Listener<ReqT> {
        return object: ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(next?.startCall(call, headers)!!) {
            override fun onHalfClose() {
                try {
                    super.onHalfClose()
                } catch (e: Exception) {
                    call?.close(Status.INTERNAL, Metadata())
                }
            }
        }
    }
}

With this implementation method I was able to get an Exception by catch.

But when using protoc-gen-grpc-kotlin

@GRpcService
class PancakeService : PancakeBakerServiceGrpcKt.PancakeBakerServiceCoroutineImplBase() {
          override suspend fun bake(request: PancakeOuterClass.BakeRequest): PancakeOuterClass.BakeResponse {
                throw Exception()
         }
}

This throw Exception() cannot be catch. Is there a good implementation?

mastar3104 avatar Sep 23 '20 07:09 mastar3104

Just gonna echo that we have this issue as well. we've taken to wrapping every method with an exception handler, which works but requires the implementor to do work for every method they write (and we are writing a lot right now).

AWinterman avatar Dec 19 '20 00:12 AWinterman

+1

maxnilz avatar Dec 25 '20 07:12 maxnilz

Just found this one may be helpful https://github.com/grpc/grpc-kotlin/issues/141#issuecomment-726829195

maxnilz avatar Dec 25 '20 08:12 maxnilz

Correct, you can override the call object, but this is still a bug.

AWinterman avatar Dec 25 '20 19:12 AWinterman

demo for handling headers and tracing: https://github.com/feuyeux/hello-grpc/blob/main/grpc/hello-grpc-kotlin/server/src/main/kotlin/org/feuyeux/grpc/HeaderServerInterceptor.kt

feuyeux avatar Sep 24 '21 19:09 feuyeux

Seems unrelated to exceptions, unless I am mistaken?

AWinterman avatar Sep 25 '21 01:09 AWinterman

@lowasser will be best for addressing this.

jamesward avatar Oct 20 '21 15:10 jamesward