Name |
Date |
Size |
#Lines |
LOC |
||
---|---|---|---|---|---|---|
.. | - | - | ||||
test/ | 12-May-2024 | - | 55 | 27 | ||
BUILD.bazel | D | 12-May-2024 | 1.7 KiB | 60 | 55 | |
README.md | D | 12-May-2024 | 5 KiB | 108 | 88 | |
client.py | D | 12-May-2024 | 1.9 KiB | 57 | 30 | |
server.py | D | 12-May-2024 | 2.4 KiB | 83 | 51 |
README.md
1# gRPC Python Error Handling Example 2 3The goal of this example is sending error status from server that is more complicated than a code and detail string. 4 5The definition for an RPC method in proto files contains request message and response message. There are many error states that can be shared across RPC methods (e.g. stack trace, insufficient quota). Using a different path to handle error will make the code more maintainable. 6 7Ideally, the final status of an RPC should be described in the trailing headers of HTTP2, and gRPC Python provides helper functions in `grpcio-status` package to assist the packing and unpacking of error status. 8 9 10### Requirement 11``` 12grpcio>=1.18.0 13grpcio-status>=1.18.0 14googleapis-common-protos>=1.5.5 15``` 16 17 18### Error Detail Proto 19 20You may provide any custom proto message as error detail in your implementation. Here are protos are defined by Google Cloud Library Team: 21 22* [code.proto]([https://github.com/googleapis/api-common-protos/blob/master/google/rpc/code.proto](https://github.com/googleapis/api-common-protos/blob/87185dfffad4afa5a33a8c153f0e1ea53b4f85dc/google/rpc/code.proto)) contains definition of RPC error codes. 23* [error_details.proto]([https://github.com/googleapis/api-common-protos/blob/master/google/rpc/error_details.proto](https://github.com/googleapis/api-common-protos/blob/87185dfffad4afa5a33a8c153f0e1ea53b4f85dc/google/rpc/error_details.proto)) contains definitions of common error details. 24 25 26### Definition of Status Proto 27 28Here is the definition of Status proto. For full text, please see [status.proto](https://github.com/googleapis/api-common-protos/blob/87185dfffad4afa5a33a8c153f0e1ea53b4f85dc/google/rpc/status.proto). 29 30```proto 31// The `Status` type defines a logical error model that is suitable for different 32// programming environments, including REST APIs and RPC APIs. It is used by 33// [gRPC](https://github.com/grpc). The error model is designed to be: 34// 35// - Simple to use and understand for most users 36// - Flexible enough to meet unexpected needs 37// 38// # Overview 39// 40// The `Status` message contains three pieces of data: error code, error message, 41// and error details. The error code should be an enum value of 42// [google.rpc.Code][google.rpc.Code], but it may accept additional error codes if needed. The 43// error message should be a developer-facing English message that helps 44// developers *understand* and *resolve* the error. If a localized user-facing 45// error message is needed, put the localized message in the error details or 46// localize it in the client. The optional error details may contain arbitrary 47// information about the error. There is a predefined set of error detail types 48// in the package `google.rpc` that can be used for common error conditions. 49// 50// # Language mapping 51// 52// The `Status` message is the logical representation of the error model, but it 53// is not necessarily the actual wire format. When the `Status` message is 54// exposed in different client libraries and different wire protocols, it can be 55// mapped differently. For example, it will likely be mapped to some exceptions 56// in Java, but more likely mapped to some error codes in C. 57// 58// # Other uses 59// 60// The error model and the `Status` message can be used in a variety of 61// environments, either with or without APIs, to provide a 62// consistent developer experience across different environments. 63// 64// Example uses of this error model include: 65// 66// - Partial errors. If a service needs to return partial errors to the client, 67// it may embed the `Status` in the normal response to indicate the partial 68// errors. 69// 70// - Workflow errors. A typical workflow has multiple steps. Each step may 71// have a `Status` message for error reporting. 72// 73// - Batch operations. If a client uses batch request and batch response, the 74// `Status` message should be used directly inside batch response, one for 75// each error sub-response. 76// 77// - Asynchronous operations. If an API call embeds asynchronous operation 78// results in its response, the status of those operations should be 79// represented directly using the `Status` message. 80// 81// - Logging. If some API errors are stored in logs, the message `Status` could 82// be used directly after any stripping needed for security/privacy reasons. 83message Status { 84 // The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. 85 int32 code = 1; 86 87 // A developer-facing error message, which should be in English. Any 88 // user-facing error message should be localized and sent in the 89 // [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. 90 string message = 2; 91 92 // A list of messages that carry the error details. There is a common set of 93 // message types for APIs to use. 94 repeated google.protobuf.Any details = 3; 95} 96``` 97 98 99### Usage of Well-Known-Proto `Any` 100 101Please check [ProtoBuf Document: Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#any) 102 103```Python 104any_message.Pack(message) 105any_message.Unpack(message) 106assert any_message.Is(message.DESCRIPTOR) 107``` 108