Handling errors from streams in python

29 views
Skip to first unread message

li...@vedra.io

unread,
Aug 21, 2018, 9:22:22 AM8/21/18
to grpc.io
Hi,

I've been trying ascertain the correct way to handle error behaviour from streaming endpoints.

My code effectively does the following:

service MyService {
    rpc
StreamEndpoint(Request) returns (stream Response)
}

message
Request {
    repeated
string query_ids = 1;
}

message
Response {
   
string object_id = 1;
}

class MyService(MyServiceServicer):

   
def StreamEndpoint(self, request, context):
       
if not request.query_ids:
            context
.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context
.set_details('Must set at least one id')
           
yield Empty()
       
for obj in self.db.query(id_list=request.query_ids):
           
yield obj.to_protobuf()


I was expecting the client upon calling StreamEndpoint without query_ids to throw an RpcError (the client is also in Python) upon getting a code that isn't OK. However, it doesn't, even though the code in the response is correctly set to INVALID_ARGUMENT.

Is this the correct behaviour? If so, what is usually the recommended way of handling these errors in python, as I have relied on the exception on non-streaming endpoints.

Thanks

Mehrdad Afshari

unread,
Aug 21, 2018, 11:31:40 AM8/21/18
to li...@vedra.io, grpc.io
Can you please share a minimal client code that exhibits the behavior?

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/92a1d587-c1d6-4e71-92ab-4d428d8c2152%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

li...@vedra.io

unread,
Aug 21, 2018, 1:12:39 PM8/21/18
to grpc.io
So, this is the unit test code that fails (the stub and server are initialized in the setUp):

    def test_list_invalid(self):
        request
= service_pb2.Request()
       
with self.assertRaises(expected_exception=grpc.RpcError) as error:
           
for _ in self.stub.StreamEndpoint(request):
               
pass
       
self.assertEqual(error.exception.code(), grpc.StatusCode.INVALID_ARGUMENT)

This test completes with AssertionError: RpcError not raised. Although, if I check the response, it does have the correct status code.
Reply all
Reply to author
Forward
0 new messages