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