From b670fd6c5f540c4f3ee9cab1c89b9ae9e2dcc022 Mon Sep 17 00:00:00 2001 From: Subhash Bhushan Date: Thu, 23 Sep 2021 10:18:32 -0700 Subject: [PATCH] Support pickling of exceptions --- src/protean/exceptions.py | 3 +++ tests/test_exceptions.py | 12 ++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 tests/test_exceptions.py diff --git a/src/protean/exceptions.py b/src/protean/exceptions.py index 2278c35d..1e63d80e 100644 --- a/src/protean/exceptions.py +++ b/src/protean/exceptions.py @@ -17,6 +17,9 @@ def __init__(self, messages, **kwargs): def __str__(self): return f"{dict(self.messages)}" + def __reduce__(self): + return (ProteanException, (self.messages,)) + class ConfigurationError(Exception): """Improper Configuration encountered like: diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py new file mode 100644 index 00000000..9d8e60e8 --- /dev/null +++ b/tests/test_exceptions.py @@ -0,0 +1,12 @@ +import pickle + +from protean.exceptions import ObjectNotFoundError + + +def test_pickling_of_exceptions(): + exc = ObjectNotFoundError({"_entity": "foo"}) + + pickled_exc = pickle.dumps(exc) + unpickled_exc = pickle.loads(pickled_exc) + + assert exc.messages == unpickled_exc.messages