title: How to add global exception interceptor in gRPC server?
body: <p>In gRPC , how to add a global exception interceptor that intercepts any <code>RuntimeException</code> and propagate meaningful information to the client ?</p>
  
  <p>for example , a <code>divide</code> method may throw <code>ArithmeticException</code> with <code>/ by zero</code> message . In the server side , I may write :</p>
  
  <pre><code>@Override
  public void divide(DivideRequest request, StreamObserver&lt;DivideResponse&gt; responseObserver) {
    int dom = request.getDenominator();
    int num = request.getNumerator();
  
    double result = num / dom;
    responseObserver.onNext(DivideResponse.newBuilder().setValue(result).build());
    responseObserver.onCompleted();
  }
  </code></pre>
  
  <p>If the client passes denominator = 0 , it will get :</p>
  
  <pre><code>Exception in thread "main" io.grpc.StatusRuntimeException: UNKNOWN
  </code></pre>
  
  <p>And the server outputs </p>
  
  <pre><code>Exception while executing runnable io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$2@62e95ade
  java.lang.ArithmeticException: / by zero
  </code></pre>
  
  <p>The client doesn't know what's going on.</p>
  
  <p>If I want to pass <code>/ by zero</code> message to client , I have to modify server to :
  (as described in this <a href="https://stackoverflow.com/questions/38810657/exception-handling-in-grpc">question</a> )</p>
  
  <pre><code>  try {
      double result = num / dom;
      responseObserver.onNext(DivideResponse.newBuilder().setValue(result).build());
      responseObserver.onCompleted();
    } catch (Exception e) {
      logger.error("onError : {}" , e.getMessage());
      responseObserver.onError(new StatusRuntimeException(Status.INTERNAL.withDescription(e.getMessage())));
    }
  </code></pre>
  
  <p>And if client sends denominator = 0 , it will get :</p>
  
  <pre><code>Exception in thread "main" io.grpc.StatusRuntimeException: INTERNAL: / by zero
  </code></pre>
  
  <p>Good , <code>/ by zero</code> is passed to client.</p>
  
  <p>But the problem is , in a truly enterprise environment, there will be a lot of <code>RuntimeException</code>s , and if I want to pass these exception's messages to client , I will have to try catch each method , which is very cumbersome.</p>
  
  <p>Is there any global interceptor that intercepts every method , catching <code>RuntimeException</code> and trigger <code>onError</code> and propagate the error message to client ? So that I don't have to deal with <code>RuntimeException</code>s in my server code .</p>
  
  <p>Thanks a lot !</p>
  
  <p>Note : </p>
  
  <pre><code>&lt;grpc.version&gt;1.0.1&lt;/grpc.version&gt;
  com.google.protobuf:proton:3.1.0
  io.grpc:protoc-gen-grpc-java:1.0.1
  </code></pre>