xref: /aosp_15_r20/external/grpc-grpc/examples/python/errors/README.md (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1*cc02d7e2SAndroid Build Coastguard Worker# gRPC Python Error Handling Example
2*cc02d7e2SAndroid Build Coastguard Worker
3*cc02d7e2SAndroid Build Coastguard WorkerThe goal of this example is sending error status from server that is more complicated than a code and detail string.
4*cc02d7e2SAndroid Build Coastguard Worker
5*cc02d7e2SAndroid Build Coastguard WorkerThe 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*cc02d7e2SAndroid Build Coastguard Worker
7*cc02d7e2SAndroid Build Coastguard WorkerIdeally, 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*cc02d7e2SAndroid Build Coastguard Worker
9*cc02d7e2SAndroid Build Coastguard Worker
10*cc02d7e2SAndroid Build Coastguard Worker### Requirement
11*cc02d7e2SAndroid Build Coastguard Worker```
12*cc02d7e2SAndroid Build Coastguard Workergrpcio>=1.18.0
13*cc02d7e2SAndroid Build Coastguard Workergrpcio-status>=1.18.0
14*cc02d7e2SAndroid Build Coastguard Workergoogleapis-common-protos>=1.5.5
15*cc02d7e2SAndroid Build Coastguard Worker```
16*cc02d7e2SAndroid Build Coastguard Worker
17*cc02d7e2SAndroid Build Coastguard Worker
18*cc02d7e2SAndroid Build Coastguard Worker### Error Detail Proto
19*cc02d7e2SAndroid Build Coastguard Worker
20*cc02d7e2SAndroid Build Coastguard WorkerYou may provide any custom proto message as error detail in your implementation. Here are protos are defined by Google Cloud Library Team:
21*cc02d7e2SAndroid Build Coastguard Worker
22*cc02d7e2SAndroid Build Coastguard Worker* [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*cc02d7e2SAndroid Build Coastguard Worker* [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*cc02d7e2SAndroid Build Coastguard Worker
25*cc02d7e2SAndroid Build Coastguard Worker
26*cc02d7e2SAndroid Build Coastguard Worker### Definition of Status Proto
27*cc02d7e2SAndroid Build Coastguard Worker
28*cc02d7e2SAndroid Build Coastguard WorkerHere 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*cc02d7e2SAndroid Build Coastguard Worker
30*cc02d7e2SAndroid Build Coastguard Worker```proto
31*cc02d7e2SAndroid Build Coastguard Worker// The `Status` type defines a logical error model that is suitable for different
32*cc02d7e2SAndroid Build Coastguard Worker// programming environments, including REST APIs and RPC APIs. It is used by
33*cc02d7e2SAndroid Build Coastguard Worker// [gRPC](https://github.com/grpc). The error model is designed to be:
34*cc02d7e2SAndroid Build Coastguard Worker//
35*cc02d7e2SAndroid Build Coastguard Worker// - Simple to use and understand for most users
36*cc02d7e2SAndroid Build Coastguard Worker// - Flexible enough to meet unexpected needs
37*cc02d7e2SAndroid Build Coastguard Worker//
38*cc02d7e2SAndroid Build Coastguard Worker// # Overview
39*cc02d7e2SAndroid Build Coastguard Worker//
40*cc02d7e2SAndroid Build Coastguard Worker// The `Status` message contains three pieces of data: error code, error message,
41*cc02d7e2SAndroid Build Coastguard Worker// and error details. The error code should be an enum value of
42*cc02d7e2SAndroid Build Coastguard Worker// [google.rpc.Code][google.rpc.Code], but it may accept additional error codes if needed.  The
43*cc02d7e2SAndroid Build Coastguard Worker// error message should be a developer-facing English message that helps
44*cc02d7e2SAndroid Build Coastguard Worker// developers *understand* and *resolve* the error. If a localized user-facing
45*cc02d7e2SAndroid Build Coastguard Worker// error message is needed, put the localized message in the error details or
46*cc02d7e2SAndroid Build Coastguard Worker// localize it in the client. The optional error details may contain arbitrary
47*cc02d7e2SAndroid Build Coastguard Worker// information about the error. There is a predefined set of error detail types
48*cc02d7e2SAndroid Build Coastguard Worker// in the package `google.rpc` that can be used for common error conditions.
49*cc02d7e2SAndroid Build Coastguard Worker//
50*cc02d7e2SAndroid Build Coastguard Worker// # Language mapping
51*cc02d7e2SAndroid Build Coastguard Worker//
52*cc02d7e2SAndroid Build Coastguard Worker// The `Status` message is the logical representation of the error model, but it
53*cc02d7e2SAndroid Build Coastguard Worker// is not necessarily the actual wire format. When the `Status` message is
54*cc02d7e2SAndroid Build Coastguard Worker// exposed in different client libraries and different wire protocols, it can be
55*cc02d7e2SAndroid Build Coastguard Worker// mapped differently. For example, it will likely be mapped to some exceptions
56*cc02d7e2SAndroid Build Coastguard Worker// in Java, but more likely mapped to some error codes in C.
57*cc02d7e2SAndroid Build Coastguard Worker//
58*cc02d7e2SAndroid Build Coastguard Worker// # Other uses
59*cc02d7e2SAndroid Build Coastguard Worker//
60*cc02d7e2SAndroid Build Coastguard Worker// The error model and the `Status` message can be used in a variety of
61*cc02d7e2SAndroid Build Coastguard Worker// environments, either with or without APIs, to provide a
62*cc02d7e2SAndroid Build Coastguard Worker// consistent developer experience across different environments.
63*cc02d7e2SAndroid Build Coastguard Worker//
64*cc02d7e2SAndroid Build Coastguard Worker// Example uses of this error model include:
65*cc02d7e2SAndroid Build Coastguard Worker//
66*cc02d7e2SAndroid Build Coastguard Worker// - Partial errors. If a service needs to return partial errors to the client,
67*cc02d7e2SAndroid Build Coastguard Worker//     it may embed the `Status` in the normal response to indicate the partial
68*cc02d7e2SAndroid Build Coastguard Worker//     errors.
69*cc02d7e2SAndroid Build Coastguard Worker//
70*cc02d7e2SAndroid Build Coastguard Worker// - Workflow errors. A typical workflow has multiple steps. Each step may
71*cc02d7e2SAndroid Build Coastguard Worker//     have a `Status` message for error reporting.
72*cc02d7e2SAndroid Build Coastguard Worker//
73*cc02d7e2SAndroid Build Coastguard Worker// - Batch operations. If a client uses batch request and batch response, the
74*cc02d7e2SAndroid Build Coastguard Worker//     `Status` message should be used directly inside batch response, one for
75*cc02d7e2SAndroid Build Coastguard Worker//     each error sub-response.
76*cc02d7e2SAndroid Build Coastguard Worker//
77*cc02d7e2SAndroid Build Coastguard Worker// - Asynchronous operations. If an API call embeds asynchronous operation
78*cc02d7e2SAndroid Build Coastguard Worker//     results in its response, the status of those operations should be
79*cc02d7e2SAndroid Build Coastguard Worker//     represented directly using the `Status` message.
80*cc02d7e2SAndroid Build Coastguard Worker//
81*cc02d7e2SAndroid Build Coastguard Worker// - Logging. If some API errors are stored in logs, the message `Status` could
82*cc02d7e2SAndroid Build Coastguard Worker//     be used directly after any stripping needed for security/privacy reasons.
83*cc02d7e2SAndroid Build Coastguard Workermessage Status {
84*cc02d7e2SAndroid Build Coastguard Worker  // The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code].
85*cc02d7e2SAndroid Build Coastguard Worker  int32 code = 1;
86*cc02d7e2SAndroid Build Coastguard Worker
87*cc02d7e2SAndroid Build Coastguard Worker  // A developer-facing error message, which should be in English. Any
88*cc02d7e2SAndroid Build Coastguard Worker  // user-facing error message should be localized and sent in the
89*cc02d7e2SAndroid Build Coastguard Worker  // [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client.
90*cc02d7e2SAndroid Build Coastguard Worker  string message = 2;
91*cc02d7e2SAndroid Build Coastguard Worker
92*cc02d7e2SAndroid Build Coastguard Worker  // A list of messages that carry the error details.  There is a common set of
93*cc02d7e2SAndroid Build Coastguard Worker  // message types for APIs to use.
94*cc02d7e2SAndroid Build Coastguard Worker  repeated google.protobuf.Any details = 3;
95*cc02d7e2SAndroid Build Coastguard Worker}
96*cc02d7e2SAndroid Build Coastguard Worker```
97*cc02d7e2SAndroid Build Coastguard Worker
98*cc02d7e2SAndroid Build Coastguard Worker
99*cc02d7e2SAndroid Build Coastguard Worker### Usage of Well-Known-Proto `Any`
100*cc02d7e2SAndroid Build Coastguard Worker
101*cc02d7e2SAndroid Build Coastguard WorkerPlease check [ProtoBuf Document: Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#any)
102*cc02d7e2SAndroid Build Coastguard Worker
103*cc02d7e2SAndroid Build Coastguard Worker```Python
104*cc02d7e2SAndroid Build Coastguard Workerany_message.Pack(message)
105*cc02d7e2SAndroid Build Coastguard Workerany_message.Unpack(message)
106*cc02d7e2SAndroid Build Coastguard Workerassert any_message.Is(message.DESCRIPTOR)
107*cc02d7e2SAndroid Build Coastguard Worker```
108