How to convert status codes into grpc.StatusCode enum in Python?

588 views
Skip to first unread message

busu...@google.com

unread,
Oct 5, 2020, 7:15:12 PM10/5/20
to grpc.io
Hi,

How do I convert the integer status code (5) into the enum (grpc.StatusCode.NOT_FOUND)?



I tried the following (see Programmatic access to enumeration members and their attributes) but get an error.
>>> import grpc
>>> grpc.StatusCode(5)
ValueError: 5 is not a valid StatusCode

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/google/home/busunkim/.pyenv/versions/3.7.6/lib/python3.7/enum.py", line 310, in __call__
    return cls.__new__(cls, value)
  File "/usr/local/google/home/busunkim/.pyenv/versions/3.7.6/lib/python3.7/enum.py", line 564, in __new__
    raise exc
  File "/usr/local/google/home/busunkim/.pyenv/versions/3.7.6/lib/python3.7/enum.py", line 548, in __new__
    result = cls._missing_(value)
  File "/usr/local/google/home/busunkim/.pyenv/versions/3.7.6/lib/python3.7/enum.py", line 577, in _missing_
    raise ValueError("%r is not a valid %s" % (value, cls.__name__))
ValueError: 5 is not a valid StatusCode


Context: I'd like turn a status code from a long running operation (google/longrunning/operations.proto and google/rpc/status.proto) into a grpc.StatusCode for nicer exceptions in google-api-core

An operation message looks something like this:

name: "projects/my-project/instances/test-instance/databases/db2/operations/_auto_op_43eacb16d4b5d9da"
metadata {
  value: "\nHprojects/my-project/instances/test-instance/databases/db2\0223CREATE TABLE Albums (\n  id INT64,\n) PRIMARY KEY(id)"
}
done: true
error {
  code: 9
  message: "Duplicate name in schema: Albums."
}


Thanks!

Lidi Zheng

unread,
Oct 5, 2020, 7:48:30 PM10/5/20
to Bu Sun Kim, grpc.io
This is a constraint of current "grpc.StatusCode" design. Each enum is constructed by an int and a str, e.g., (0, 'ok'). So, to convert literals into enums, we need to write:

        grpc.StatusCode((5, 'not found'))

For a short-term fix, you may encapsulate an iterating function to map int to enum, like:

        [x for x in grpc.StatusCode if x.value[0] == 5]

Do you think it would be helpful to support direct enum conversion? If we add a new enum conversion feature, then the client library needs to bump up the grpcio minimum version again. The conversion can be done without regression via overriding the initialization method of "grpc.StatusCode".


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/375d3166-2c49-4fb4-becd-44bfbf7b9f3do%40googlegroups.com.

Bu Sun Kim

unread,
Oct 5, 2020, 8:29:54 PM10/5/20
to grpc.io
Thank you for the help Lidi!

This only shows up once in google-api-core so constructing a dict worked well (PR). It would be nice to be able to do it directly, but it definitely isn't high priority.

To unsubscribe from this group and stop receiving emails from it, send an email to grp...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages