Raising a server error to the client with grpc
<p>Let's consider a simple service:</p>

<pre><code>service Something {
    rpc Do(Request) returns Response;
}

message Request {
    string field = 1;
}

message Response {
    string response = 1; 
}
</code></pre>

<p>Assume I have to do some checking on the <code>Request.field</code>, I want to raise a client error if the field is invalid:</p>

<pre><code>class MyService(proto_pb2.SomethingServicer):

    def Do(self, request, context):
        if not is_valid_field(request.field):
            raise ValueError("Damn!")  # Or something like that
        return proto_pb2.Response(response="Yeah!")
</code></pre>

<p>With the following client:</p>

<pre><code>channel = grpc.insecure_channel(...)
stub = proto_pb2.SomethingStub(channel)
try:
    response = stub.Do(proto_pb2.Request(field="invalid"))
except grpc.RpcError as e:
    print(e)
</code></pre>

<blockquote>
  <p>&lt;_Rendezvous of RPC that terminated with (StatusCode.UNKNOWN, Exception calling application: Damn!)></p>
</blockquote>

<p>So I can technically handle errors. My issue is... is there a better way? Is there a good way to change the message description? Can we change the status code?</p>