grpc-kotlin
grpc-kotlin copied to clipboard
How to handle errors with Interceptor
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?
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).
+1
Just found this one may be helpful https://github.com/grpc/grpc-kotlin/issues/141#issuecomment-726829195
Correct, you can override the call object, but this is still a bug.
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
Seems unrelated to exceptions, unless I am mistaken?
@lowasser will be best for addressing this.