summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2020-07-07 08:58:41 +0000
committerGerrit Code Review <review@openstack.org>2020-07-07 08:58:41 +0000
commit46759750e0d34296c218a71c78cc7b60115693ef (patch)
tree2c5472d8aff75e84d260431967b9702bef998095
parent3e053f05be424c00fb7ba5733f72db6beeaff393 (diff)
parent7c036fa18dca97c93e36eb91e30fd6fe4e3a48f0 (diff)
downloadironic-python-agent-46759750e0d34296c218a71c78cc7b60115693ef.tar.gz
Merge "Fix serializing ironic-lib exceptions" into stable/train5.0.3
-rw-r--r--ironic_python_agent/encoding.py12
-rw-r--r--ironic_python_agent/tests/unit/test_encoding.py21
-rw-r--r--releasenotes/notes/lib-exc-41ee122eb4a04bc4.yaml6
3 files changed, 39 insertions, 0 deletions
diff --git a/ironic_python_agent/encoding.py b/ironic_python_agent/encoding.py
index 715d1341..b1b44cd1 100644
--- a/ironic_python_agent/encoding.py
+++ b/ironic_python_agent/encoding.py
@@ -15,6 +15,8 @@
import json
import uuid
+from ironic_lib import exception as lib_exc
+
class Serializable(object):
"""Base class for things that can be serialized."""
@@ -43,6 +45,14 @@ class SerializableComparable(Serializable):
return self.serialize() != other.serialize()
+def serialize_lib_exc(exc):
+ """Serialize an ironic-lib exception."""
+ return {'type': exc.__class__.__name__,
+ 'code': exc.code,
+ 'message': str(exc),
+ 'details': ''}
+
+
class RESTJSONEncoder(json.JSONEncoder):
"""A slightly customized JSON encoder."""
def encode(self, o):
@@ -68,5 +78,7 @@ class RESTJSONEncoder(json.JSONEncoder):
return o.serialize()
elif isinstance(o, uuid.UUID):
return str(o)
+ elif isinstance(o, lib_exc.IronicException):
+ return serialize_lib_exc(o)
else:
return json.JSONEncoder.default(self, o)
diff --git a/ironic_python_agent/tests/unit/test_encoding.py b/ironic_python_agent/tests/unit/test_encoding.py
index 64cd9b63..862cd9e8 100644
--- a/ironic_python_agent/tests/unit/test_encoding.py
+++ b/ironic_python_agent/tests/unit/test_encoding.py
@@ -12,6 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.
+import json
+
+from ironic_lib import exception as lib_exc
+
from ironic_python_agent import encoding
from ironic_python_agent.tests.unit import base
@@ -59,3 +63,20 @@ class TestSerializableComparable(base.IronicAgentTest):
# Ensure __hash__ is None
obj = SerializableComparableTesting('hello', 'world')
self.assertIsNone(obj.__hash__)
+
+
+class TestEncoder(base.IronicAgentTest):
+
+ encoder = encoding.RESTJSONEncoder()
+
+ def test_encoder(self):
+ expected = {'jack': 'hello', 'jill': 'world'}
+ obj = SerializableTesting('hello', 'world')
+ self.assertEqual(expected, json.loads(self.encoder.encode(obj)))
+
+ def test_ironic_lib(self):
+ obj = lib_exc.InstanceDeployFailure(reason='boom')
+ encoded = json.loads(self.encoder.encode(obj))
+ self.assertEqual(500, encoded['code'])
+ self.assertEqual('InstanceDeployFailure', encoded['type'])
+ self.assertIn('boom', encoded['message'])
diff --git a/releasenotes/notes/lib-exc-41ee122eb4a04bc4.yaml b/releasenotes/notes/lib-exc-41ee122eb4a04bc4.yaml
new file mode 100644
index 00000000..cb3e8c2f
--- /dev/null
+++ b/releasenotes/notes/lib-exc-41ee122eb4a04bc4.yaml
@@ -0,0 +1,6 @@
+---
+fixes:
+ - |
+ Fixes serializing exceptions originating from ironic-lib. Previously an
+ attempt to do so would result in a ``TypeError``, for example:
+ `Object of type 'InstanceDeployFailure' is not JSON serializable`.