2

I am using grpc in flutter. My question is how can I handle GRPC errors using interceptors. My purpose is to handle all the error at the same place.

I would really appreciate your help.

Khamidjon Khamidov
  • 6,783
  • 6
  • 31
  • 62

1 Answers1

3

You can implement you own interceptor like this:

class MyInterceptor implements ClientInterceptor {
  MyInterceptor();

  @override
  ResponseFuture<R> interceptUnary<Q, R>(ClientMethod<Q, R> method, Q request,
      CallOptions options, ClientUnaryInvoker<Q, R> invoker) {
   
    final response = invoker(method, request, options)
      ..catchError((e) async {
        print('handle errors here $e');
      });

    return response;
  }

  @override
  ResponseStream<R> interceptStreaming<Q, R>(
      ClientMethod<Q, R> method,
      Stream<Q> requests,
      CallOptions options,
      ClientStreamingInvoker<Q, R> invoker) {
    return invoker(method, requests, options);
  }
}
Cyrax
  • 688
  • 6
  • 12