Repository: rapidpro/rapidpro-python

Issue:
TembaSerializationException always has the message <exception str() failed>
Hello,

I was using the `TembaObject.deserialize()` method to deserialize archived `Run` objects in my project (https://github.com/JustFixNYC/tenants2/pull/902) and noticed that an unusual exception was occurring.  It's easily reproducible in the REPL:

```
>>> from temba_client.serialization import TembaSerializationException
>>>
>>> raise TembaSerializationException('boop')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
temba_client.exceptions.TembaSerializationException: <exception str() failed>
```

The actual cause of `str()` failing can be found via:

```
>>> str(TembaSerializationException('boop'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/venv/lib/python3.8/site-packages/temba_client/exceptions.py", line 3, in __str__
    return self.message
AttributeError: 'TembaSerializationException' object has no attribute 'message'
```

So it looks like `.message` isn't being set in the constructor for `TembaSerializationException`?  For now I'm just catching the exceptions and logging their `args[0]` attribute, but fixing these so they have meaningful error messages would be helpful.


Discussion:

Patch:
diff --git a/temba_client/exceptions.py b/temba_client/exceptions.py
index 253903c..95594ba 100644
--- a/temba_client/exceptions.py
+++ b/temba_client/exceptions.py
@@ -56,7 +56,8 @@ class TembaHttpError(TembaException):
 
 
 class TembaSerializationException(TembaException):
-    pass
+    def __init__(self, message):
+        self.message = message
 
 
 class TembaMultipleResultsError(TembaException):


Test patch:
diff --git a/temba_client/tests.py b/temba_client/tests.py
index 35578a2..1da8715 100644
--- a/temba_client/tests.py
+++ b/temba_client/tests.py
@@ -106,6 +106,12 @@ class TestType(TembaObject):
     meh = ObjectDictField(item_class=TestSubType)
 
 
+class TembaSerializationExceptionTest(TembaTest):
+    def test_str_works(self):
+        err = TembaSerializationException("boop")
+        self.assertEqual(str(err), "boop")
+
+
 class FieldsTest(TembaTest):
     def test_boolean(self):
         field = BooleanField()