Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions packages/google-api-core/google/api_core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def cause(self):
def __str__(self):
return "{}, last exception: {}".format(self.message, self.cause)

def __reduce__(self):
return (type(self), (self.message, self.cause), self.__dict__)


class _GoogleAPICallErrorMeta(type):
"""Metaclass for registering GoogleAPICallError subclasses."""
Expand Down
41 changes: 41 additions & 0 deletions packages/google-api-core/tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import copy
import http.client
import json
import pickle
from unittest import mock

import pytest
Expand Down Expand Up @@ -434,3 +436,42 @@ def test_from_grpc_error_misleading_404():
assert exception.grpc_status_code == grpc.StatusCode.UNIMPLEMENTED
assert "Received http2 header with status: 404" in exception.message
assert "api_endpoint" in exception.message


@pytest.mark.parametrize(
"operation",
[
lambda err: pickle.loads(pickle.dumps(err)),
copy.copy,
copy.deepcopy,
],
ids=["pickle", "copy", "deepcopy"],
)
@pytest.mark.parametrize(
"cause",
[
exceptions.ServiceUnavailable("503 backend unavailable"),
None,
],
ids=["with_cause", "none_cause"],
)
def test_retry_error_copy_and_pickle(operation, cause):
original = exceptions.RetryError("Request timed out", cause)
original.custom_attr = "test_value"

result = operation(original)

assert isinstance(result, exceptions.RetryError)
assert result.message == original.message
assert str(result) == str(original)
assert result.custom_attr == "test_value"

if cause is None:
assert result.cause is None
else:
if operation is copy.copy:
assert result.cause is original.cause

assert isinstance(result.cause, type(original.cause))
assert result.cause.message == original.cause.message
assert str(result.cause) == str(original.cause)
Comment thread
yashgupta29032006 marked this conversation as resolved.
Resolved
Loading