Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Move '_catch_remap_gax_error()' before end-of-module constants.
Addresses:
#3444 (comment)
  • Loading branch information
tseaver committed Jun 5, 2017
commit 457c207b30f3eb3f22d939e26ea47808667e51d5
46 changes: 23 additions & 23 deletions core/google/cloud/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,29 @@ def _walk_subclasses(klass):
yield subsub


@contextlib.contextmanager
def _catch_remap_gax_error():
"""Remap GAX exceptions that happen in context.

.. _code.proto: https://github.com/googleapis/googleapis/blob/\
master/google/rpc/code.proto

Remaps gRPC exceptions to the classes defined in
:mod:`~google.cloud.exceptions` (according to the description
in `code.proto`_).
"""
try:
yield
except GaxError as exc:
error_code = exc_to_code(exc.cause)
error_class = _GRPC_ERROR_MAPPING.get(error_code)
if error_class is None:
raise
else:
new_exc = error_class(exc.cause.details())
six.reraise(error_class, new_exc, sys.exc_info()[2])


# Build the code->exception class mapping.
for _eklass in _walk_subclasses(GoogleCloudError):
code = getattr(_eklass, 'code', None)
Expand All @@ -272,26 +295,3 @@ def _walk_subclasses(klass):
StatusCode.UNAVAILABLE: ServiceUnavailable,
StatusCode.DATA_LOSS: InternalServerError,
}


@contextlib.contextmanager
def _catch_remap_gax_error():
"""Remap GAX exceptions that happen in context.

.. _code.proto: https://github.com/googleapis/googleapis/blob/\
master/google/rpc/code.proto

Remaps gRPC exceptions to the classes defined in
:mod:`~google.cloud.exceptions` (according to the description
in `code.proto`_).
"""
try:
yield
except GaxError as exc:
error_code = exc_to_code(exc.cause)
error_class = _GRPC_ERROR_MAPPING.get(error_code)
if error_class is None:
raise
else:
new_exc = error_class(exc.cause.details())
six.reraise(error_class, new_exc, sys.exc_info()[2])