Yes, there is a better way. You may change the status details using the ServicerContext.set_details
method and you may change the status code using the ServicerContext.set_code
method. I suspect that your servicer will look something like
class MyService(proto_pb2.SomethingServicer):
def Do(self, request, context):
if not is_valid_field(request.field):
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
context.set_details('Consarnit!')
return proto_pb2.Response()
return proto_pb2.Response(response="Yeah!")
.