Refactor errors to _errors.py

revert-34515-revert-33969-checkAbortError
Xuan Wang 12 months ago
parent 07b051f68c
commit 95074e2564
  1. 6
      src/python/grpcio/grpc/BUILD.bazel
  2. 12
      src/python/grpcio/grpc/__init__.py
  3. 1
      src/python/grpcio/grpc/_cython/_cygrpc/aio/call.pyx.pxi
  4. 1
      src/python/grpcio/grpc/_cython/_cygrpc/aio/callback_common.pyx.pxi
  5. 1
      src/python/grpcio/grpc/_cython/_cygrpc/aio/channel.pyx.pxi
  6. 2
      src/python/grpcio/grpc/_cython/_cygrpc/aio/server.pyx.pxi
  7. 1
      src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
  8. 1
      src/python/grpcio/grpc/_cython/cygrpc.pyx
  9. 23
      src/python/grpcio/grpc/_errors.py
  10. 2
      src/python/grpcio/grpc/_server.py
  11. 8
      src/python/grpcio/grpc/aio/__init__.py
  12. 8
      src/python/grpcio/grpc/aio/_call.py
  13. 3
      src/python/grpcio/grpc/aio/_channel.py
  14. 5
      src/python/grpcio/grpc/aio/_interceptor.py
  15. 2
      src/python/grpcio_tests/tests/unit/_abort_test.py
  16. 5
      src/python/grpcio_tests/tests_aio/unit/client_stream_unary_interceptor_test.py

@ -99,6 +99,11 @@ py_library(
srcs = ["_observability.py"],
)
py_library(
name = "errors",
srcs = ["_errors.py"],
)
py_library(
name = "grpcio",
srcs = ["__init__.py"],
@ -115,6 +120,7 @@ py_library(
":auth",
":channel",
":compression",
":errors",
":interceptor",
":plugin_wrapping",
":server",

@ -21,8 +21,9 @@ import sys
from grpc import _compression
from grpc._cython import cygrpc as _cygrpc
from grpc._cython.cygrpc import AbortError
from grpc._cython.cygrpc import BaseError
from grpc._errors import AbortError
from grpc._errors import BaseError
from grpc._errors import RpcError
from grpc._runtime_protos import protos
from grpc._runtime_protos import protos_and_services
from grpc._runtime_protos import services
@ -309,13 +310,6 @@ class Status(abc.ABC):
"""
############################# gRPC Exceptions ################################
class RpcError(BaseError):
"""Raised by the gRPC library to indicate non-OK-status RPC termination."""
############################## Shared Context ################################

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from grpc._errors import InternalError
_EMPTY_FLAGS = 0
_EMPTY_MASK = 0

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from grpc._errors import InternalError
cdef class CallbackFailureHandler:

@ -13,6 +13,7 @@
# limitations under the License.
#
from grpc._errors import UsageError
class _WatchConnectivityFailed(Exception):
"""Dedicated exception class for watch connectivity failed.

@ -13,6 +13,8 @@
# limitations under the License.
from grpc._errors import BaseError, AbortError, InternalError, UsageError
import inspect
import traceback
import functools

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from grpc._errors import InternalError, UsageError
cdef class Server:

@ -55,7 +55,6 @@ include "_cygrpc/tag.pyx.pxi"
include "_cygrpc/time.pyx.pxi"
include "_cygrpc/vtable.pyx.pxi"
include "_cygrpc/_hooks.pyx.pxi"
include "_cygrpc/common.pyx.pxi"
include "_cygrpc/observability.pyx.pxi"
include "_cygrpc/grpc_gevent.pyx.pxi"

@ -1,4 +1,4 @@
# Copyright 2023 The gRPC Authors
# Copyright 2024 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.
@ -12,29 +12,46 @@
# See the License for the specific language governing permissions and
# limitations under the License.
############################# gRPC Exceptions ################################
class BaseError(Exception):
"""
The base class for exceptions generated by gRPC.
"""
class UsageError(BaseError):
"""
Raised when the usage of API by applications is inappropriate.
For example, trying to invoke RPC on a closed channel, mixing two styles
of streaming API on the client side. This exception should not be
suppressed.
"""
class AbortError(BaseError):
"""
Raised when calling abort in servicer methods.
This exception should not be suppressed. Applications may catch it to
perform certain clean-up logic, and then re-raise it.
"""
class InternalError(BaseError):
"""
Raised upon unexpected errors in native code.
"""
class RpcError(BaseError):
"""Raised by the gRPC library to indicate non-OK-status RPC termination."""
__all__ = (
"BaseError",
"UsageError",
"AbortError",
"InternalError",
"RpcError",
)

@ -42,7 +42,7 @@ from grpc import _common # pytype: disable=pyi-error
from grpc import _compression # pytype: disable=pyi-error
from grpc import _interceptor # pytype: disable=pyi-error
from grpc._cython import cygrpc
from grpc._cython.cygrpc import AbortError
from grpc._errors import AbortError
from grpc._typing import ArityAgnosticMethodHandler
from grpc._typing import ChannelArgumentType
from grpc._typing import DeserializingFunction

@ -20,13 +20,13 @@ created. AsyncIO doesn't provide thread safety for most of its APIs.
from typing import Any, Optional, Sequence, Tuple
import grpc
from grpc._cython.cygrpc import AbortError
from grpc._cython.cygrpc import BaseError
from grpc._cython.cygrpc import EOF
from grpc._cython.cygrpc import InternalError
from grpc._cython.cygrpc import UsageError
from grpc._cython.cygrpc import init_grpc_aio
from grpc._cython.cygrpc import shutdown_grpc_aio
from grpc._errors import AbortError
from grpc._errors import BaseError
from grpc._errors import InternalError
from grpc._errors import UsageError
from ._base_call import Call
from ._base_call import RpcContext

@ -24,6 +24,8 @@ from typing import Any, AsyncIterator, Generator, Generic, Optional, Tuple
import grpc
from grpc import _common
from grpc._cython import cygrpc
from grpc._errors import InternalError
from grpc._errors import UsageError
from . import _base_call
from ._metadata import Metadata
@ -337,7 +339,7 @@ class _StreamResponseMixin(Call):
if self._response_style is _APIStyle.UNKNOWN:
self._response_style = style
elif self._response_style is not style:
raise cygrpc.UsageError(_API_STYLE_ERROR)
raise UsageError(_API_STYLE_ERROR)
def cancel(self) -> bool:
if super().cancel():
@ -418,7 +420,7 @@ class _StreamRequestMixin(Call):
def _raise_for_different_style(self, style: _APIStyle):
if self._request_style is not style:
raise cygrpc.UsageError(_API_STYLE_ERROR)
raise UsageError(_API_STYLE_ERROR)
def cancel(self) -> bool:
if super().cancel():
@ -490,7 +492,7 @@ class _StreamRequestMixin(Call):
)
try:
await self._cython_call.send_serialized_message(serialized_request)
except cygrpc.InternalError as err:
except InternalError as err:
self._cython_call.set_internal_error(str(err))
await self._raise_for_status()
except asyncio.CancelledError:

@ -22,6 +22,7 @@ from grpc import _common
from grpc import _compression
from grpc import _grpcio_metadata
from grpc._cython import cygrpc
from grpc._errors import InternalError
from . import _base_call
from . import _base_channel
@ -431,7 +432,7 @@ class Channel(_base_channel.Channel):
continue
else:
# Unidentified Call object
raise cygrpc.InternalError(
raise InternalError(
f"Unrecognized call object: {candidate}"
)

@ -30,6 +30,7 @@ from typing import (
import grpc
from grpc._cython import cygrpc
from grpc._errors import UsageError
from . import _base_call
from ._call import AioRpcError
@ -562,7 +563,7 @@ class _InterceptedStreamRequestMixin:
# should be expected through an iterators provided
# by the caller.
if self._write_to_iterator_queue is None:
raise cygrpc.UsageError(_API_STYLE_ERROR)
raise UsageError(_API_STYLE_ERROR)
try:
call = await self._interceptors_task
@ -588,7 +589,7 @@ class _InterceptedStreamRequestMixin:
# should be expected through an iterators provided
# by the caller.
if self._write_to_iterator_queue is None:
raise cygrpc.UsageError(_API_STYLE_ERROR)
raise UsageError(_API_STYLE_ERROR)
try:
call = await self._interceptors_task

@ -20,7 +20,7 @@ import unittest
import weakref
import grpc
from grpc._cython.cygrpc import AbortError
from grpc import AbortError
from tests.unit import test_common
from tests.unit.framework.common import test_constants

@ -17,6 +17,7 @@ import logging
import unittest
import grpc
from grpc._errors import UsageError
from grpc.experimental import aio
from src.proto.grpc.testing import messages_pb2
@ -544,10 +545,10 @@ class TestStreamUnaryClientInterceptor(AioTestBase):
call = stub.StreamingInputCall(request_iterator())
with self.assertRaises(grpc._cython.cygrpc.UsageError):
with self.assertRaises(UsageError):
await call.write(request)
with self.assertRaises(grpc._cython.cygrpc.UsageError):
with self.assertRaises(UsageError):
await call.done_writing()
await channel.close()

Loading…
Cancel
Save