Skip to content

Commit 192486b

Browse files
authored
Merge pull request #232 from Azure/failsafe_error_deserialization
2 parents 209fa61 + 8896438 commit 192486b

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ To install:
2020
Release History
2121
---------------
2222

23+
2021-01-22 Version 0.6.20
24+
+++++++++++++++++++++++++
25+
26+
**Features**
27+
28+
- Add `failsafe_deserialize` method to the `Deserializer` object. When called, ignores any deserialization errors thrown,
29+
and returns `None`. Recommended when deserializing error models, as in the case of an incorrect error model, we still want
30+
to return the `HttpResponseError` to the user (without a `model`), instead of throwing a `DeserializationError`. #232
31+
2332
2020-09-08 Version 0.6.19
2433
+++++++++++++++++++++++++
2534

msrest/serialization.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,26 @@ def _classify_target(self, target, data):
14861486
pass # Target is not a Model, no classify
14871487
return target, target.__class__.__name__
14881488

1489+
def failsafe_deserialize(self, target_obj, response_data, content_type=None):
1490+
"""Ignores any errors encountered in deserialization,
1491+
and falls back to not deserializing the object. Recommended
1492+
for use in error deserialization, as we want to return the
1493+
HttpResponseError to users, and not have them deal with
1494+
a deserialization error.
1495+
1496+
:param str target_obj: The target object type to deserialize to.
1497+
:param str/dict data: The response data to deseralize.
1498+
:param str content_type: Swagger "produces" if available.
1499+
"""
1500+
try:
1501+
return self(target_obj, data, content_type=content_type)
1502+
except:
1503+
_LOGGER.warning(
1504+
"Ran into a deserialization error. Ignoring since this is failsafe deserialization",
1505+
exc_info=True
1506+
)
1507+
return None
1508+
14891509
@staticmethod
14901510
def _unpack_content(raw_data, content_type=None):
14911511
"""Extract the correct structure for deserialization.

msrest/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
# --------------------------------------------------------------------------
2626

2727
#: version of this package. Use msrest.__version__ instead
28-
msrest_version = "0.6.19"
28+
msrest_version = "0.6.20"

tests/test_serialization.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,6 +2533,19 @@ class TestModel(Model):
25332533
m = TestModel.deserialize({'data': {'id': long_type(1)}})
25342534
assert m.data['id'] == long_type(1)
25352535

2536+
def test_failsafe_deserialization(self):
2537+
class Error(Model):
2538+
2539+
def __init__(self, **kwargs):
2540+
self.status = kwargs.pop("status")
2541+
self.message = kwargs.pop("message")
2542+
2543+
with pytest.raises(DeserializationError):
2544+
self.d(Error, json.dumps(''), 'text/html')
2545+
2546+
deserialized = self.d.failsafe_deserialize(Error, json.dumps(''), 'text/html')
2547+
assert deserialized is None
2548+
25362549
class TestModelInstanceEquality(unittest.TestCase):
25372550

25382551
def test_model_instance_equality(self):

0 commit comments

Comments
 (0)