Merge pull request #18488 from lidizheng/example-errors
Add Python example for error handlingpull/18562/head
commit
56605cd55d
7 changed files with 373 additions and 0 deletions
@ -0,0 +1,56 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
load("@grpc_python_dependencies//:requirements.bzl", "requirement") |
||||
|
||||
py_library( |
||||
name = "client", |
||||
testonly = 1, |
||||
srcs = ["client.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//src/python/grpcio_status/grpc_status:grpc_status", |
||||
"//examples:py_helloworld", |
||||
requirement('googleapis-common-protos'), |
||||
], |
||||
) |
||||
|
||||
py_library( |
||||
name = "server", |
||||
testonly = 1, |
||||
srcs = ["server.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//src/python/grpcio_status/grpc_status:grpc_status", |
||||
"//examples:py_helloworld", |
||||
] + select({ |
||||
"//conditions:default": [requirement("futures")], |
||||
"//:python3": [], |
||||
}), |
||||
) |
||||
|
||||
py_test( |
||||
name = "test/_error_handling_example_test", |
||||
srcs = ["test/_error_handling_example_test.py"], |
||||
deps = [ |
||||
":client", |
||||
":server", |
||||
"//src/python/grpcio_tests/tests:bazel_namespace_package_hack", |
||||
], |
||||
size = "small", |
||||
imports = [ |
||||
"../../../src/python/grpcio_status", |
||||
"../../../src/python/grpcio_tests", |
||||
], |
||||
) |
@ -0,0 +1,107 @@ |
||||
# gRPC Python Error Handling Example |
||||
|
||||
The goal of this example is sending error status from server that is more complicated than a code and detail string. |
||||
|
||||
The 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. |
||||
|
||||
Ideally, 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. |
||||
|
||||
|
||||
### Requirement |
||||
``` |
||||
grpcio>=1.18.0 |
||||
grpcio-status>=1.18.0 |
||||
googleapis-common-protos>=1.5.5 |
||||
``` |
||||
|
||||
|
||||
### Error Detail Proto |
||||
|
||||
You may provide any custom proto message as error detail in your implementation. Here are protos are defined by Google Cloud Library Team: |
||||
|
||||
* [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. |
||||
* [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. |
||||
|
||||
|
||||
### Definition of Status Proto |
||||
|
||||
Here 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). |
||||
|
||||
```proto |
||||
// The `Status` type defines a logical error model that is suitable for different |
||||
// programming environments, including REST APIs and RPC APIs. It is used by |
||||
// [gRPC](https://github.com/grpc). The error model is designed to be: |
||||
// |
||||
// - Simple to use and understand for most users |
||||
// - Flexible enough to meet unexpected needs |
||||
// |
||||
// # Overview |
||||
// |
||||
// The `Status` message contains three pieces of data: error code, error message, |
||||
// and error details. The error code should be an enum value of |
||||
// [google.rpc.Code][google.rpc.Code], but it may accept additional error codes if needed. The |
||||
// error message should be a developer-facing English message that helps |
||||
// developers *understand* and *resolve* the error. If a localized user-facing |
||||
// error message is needed, put the localized message in the error details or |
||||
// localize it in the client. The optional error details may contain arbitrary |
||||
// information about the error. There is a predefined set of error detail types |
||||
// in the package `google.rpc` that can be used for common error conditions. |
||||
// |
||||
// # Language mapping |
||||
// |
||||
// The `Status` message is the logical representation of the error model, but it |
||||
// is not necessarily the actual wire format. When the `Status` message is |
||||
// exposed in different client libraries and different wire protocols, it can be |
||||
// mapped differently. For example, it will likely be mapped to some exceptions |
||||
// in Java, but more likely mapped to some error codes in C. |
||||
// |
||||
// # Other uses |
||||
// |
||||
// The error model and the `Status` message can be used in a variety of |
||||
// environments, either with or without APIs, to provide a |
||||
// consistent developer experience across different environments. |
||||
// |
||||
// Example uses of this error model include: |
||||
// |
||||
// - Partial errors. If a service needs to return partial errors to the client, |
||||
// it may embed the `Status` in the normal response to indicate the partial |
||||
// errors. |
||||
// |
||||
// - Workflow errors. A typical workflow has multiple steps. Each step may |
||||
// have a `Status` message for error reporting. |
||||
// |
||||
// - Batch operations. If a client uses batch request and batch response, the |
||||
// `Status` message should be used directly inside batch response, one for |
||||
// each error sub-response. |
||||
// |
||||
// - Asynchronous operations. If an API call embeds asynchronous operation |
||||
// results in its response, the status of those operations should be |
||||
// represented directly using the `Status` message. |
||||
// |
||||
// - Logging. If some API errors are stored in logs, the message `Status` could |
||||
// be used directly after any stripping needed for security/privacy reasons. |
||||
message Status { |
||||
// The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. |
||||
int32 code = 1; |
||||
|
||||
// A developer-facing error message, which should be in English. Any |
||||
// user-facing error message should be localized and sent in the |
||||
// [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. |
||||
string message = 2; |
||||
|
||||
// A list of messages that carry the error details. There is a common set of |
||||
// message types for APIs to use. |
||||
repeated google.protobuf.Any details = 3; |
||||
} |
||||
``` |
||||
|
||||
|
||||
### Usage of Well-Known-Proto `Any` |
||||
|
||||
Please check [ProtoBuf Document: Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#any) |
||||
|
||||
```Python |
||||
any_message.Pack(message) |
||||
any_message.Unpack(message) |
||||
assert any_message.Is(message.DESCRIPTOR) |
||||
``` |
@ -0,0 +1,56 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""This example handles rich error status in client-side.""" |
||||
|
||||
from __future__ import print_function |
||||
import logging |
||||
|
||||
import grpc |
||||
from grpc_status import rpc_status |
||||
from google.rpc import error_details_pb2 |
||||
|
||||
from examples.protos import helloworld_pb2 |
||||
from examples.protos import helloworld_pb2_grpc |
||||
|
||||
_LOGGER = logging.getLogger(__name__) |
||||
|
||||
|
||||
def process(stub): |
||||
try: |
||||
response = stub.SayHello(helloworld_pb2.HelloRequest(name='Alice')) |
||||
_LOGGER.info('Call success: %s', response.message) |
||||
except grpc.RpcError as rpc_error: |
||||
_LOGGER.error('Call failure: %s', rpc_error) |
||||
status = rpc_status.from_call(rpc_error) |
||||
for detail in status.details: |
||||
if detail.Is(error_details_pb2.QuotaFailure.DESCRIPTOR): |
||||
info = error_details_pb2.QuotaFailure() |
||||
detail.Unpack(info) |
||||
_LOGGER.error('Quota failure: %s', info) |
||||
else: |
||||
raise RuntimeError('Unexpected failure: %s' % detail) |
||||
|
||||
|
||||
def main(): |
||||
# NOTE(gRPC Python Team): .close() is possible on a channel and should be |
||||
# used in circumstances in which the with statement does not fit the needs |
||||
# of the code. |
||||
with grpc.insecure_channel('localhost:50051') as channel: |
||||
stub = helloworld_pb2_grpc.GreeterStub(channel) |
||||
process(stub) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig() |
||||
main() |
@ -0,0 +1,90 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""This example sends out rich error status from server-side.""" |
||||
|
||||
from concurrent import futures |
||||
import time |
||||
import logging |
||||
import threading |
||||
|
||||
import grpc |
||||
from grpc_status import rpc_status |
||||
|
||||
from google.protobuf import any_pb2 |
||||
from google.rpc import code_pb2, status_pb2, error_details_pb2 |
||||
|
||||
from examples.protos import helloworld_pb2 |
||||
from examples.protos import helloworld_pb2_grpc |
||||
|
||||
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
||||
|
||||
|
||||
def create_greet_limit_exceed_error_status(name): |
||||
detail = any_pb2.Any() |
||||
detail.Pack( |
||||
error_details_pb2.QuotaFailure( |
||||
violations=[ |
||||
error_details_pb2.QuotaFailure.Violation( |
||||
subject="name: %s" % name, |
||||
description="Limit one greeting per person", |
||||
) |
||||
],)) |
||||
return status_pb2.Status( |
||||
code=code_pb2.RESOURCE_EXHAUSTED, |
||||
message='Request limit exceeded.', |
||||
details=[detail], |
||||
) |
||||
|
||||
|
||||
class LimitedGreeter(helloworld_pb2_grpc.GreeterServicer): |
||||
|
||||
def __init__(self): |
||||
self._lock = threading.RLock() |
||||
self._greeted = set() |
||||
|
||||
def SayHello(self, request, context): |
||||
with self._lock: |
||||
if request.name in self._greeted: |
||||
rich_status = create_greet_limit_exceed_error_status( |
||||
request.name) |
||||
context.abort_with_status(rpc_status.to_status(rich_status)) |
||||
else: |
||||
self._greeted.add(request.name) |
||||
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) |
||||
|
||||
|
||||
def create_server(server_address): |
||||
server = grpc.server(futures.ThreadPoolExecutor()) |
||||
helloworld_pb2_grpc.add_GreeterServicer_to_server(LimitedGreeter(), server) |
||||
port = server.add_insecure_port(server_address) |
||||
return server, port |
||||
|
||||
|
||||
def serve(server): |
||||
server.start() |
||||
try: |
||||
while True: |
||||
time.sleep(_ONE_DAY_IN_SECONDS) |
||||
except KeyboardInterrupt: |
||||
server.stop(None) |
||||
|
||||
|
||||
def main(): |
||||
server, unused_port = create_server('[::]:50051') |
||||
serve(server) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig() |
||||
main() |
@ -0,0 +1,54 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Tests of the error handling example.""" |
||||
|
||||
# NOTE(lidiz) This module only exists in Bazel BUILD file, for more details |
||||
# please refer to comments in the "bazel_namespace_package_hack" module. |
||||
try: |
||||
from tests import bazel_namespace_package_hack |
||||
bazel_namespace_package_hack.sys_path_to_site_dir_hack() |
||||
except ImportError: |
||||
pass |
||||
|
||||
import unittest |
||||
import logging |
||||
|
||||
import grpc |
||||
|
||||
from examples.protos import helloworld_pb2_grpc |
||||
from examples.python.errors import client as error_handling_client |
||||
from examples.python.errors import server as error_handling_server |
||||
|
||||
|
||||
class ErrorHandlingExampleTest(unittest.TestCase): |
||||
|
||||
def setUp(self): |
||||
self._server, port = error_handling_server.create_server('[::]:0') |
||||
self._server.start() |
||||
self._channel = grpc.insecure_channel('localhost:%d' % port) |
||||
|
||||
def tearDown(self): |
||||
self._channel.close() |
||||
self._server.stop(None) |
||||
|
||||
def test_error_handling_example(self): |
||||
stub = helloworld_pb2_grpc.GreeterStub(self._channel) |
||||
error_handling_client.process(stub) |
||||
error_handling_client.process(stub) |
||||
# No unhandled exception raised, test passed! |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig() |
||||
unittest.main(verbosity=2) |
Loading…
Reference in new issue