Skip to content

Commit

Permalink
[core] move SerializationError and DeserializationError to exceptions (
Browse files Browse the repository at this point in the history
…Azure#24312)

* move exceptions to exceptions

* update changelog

* fix changelog

* Update CHANGELOG.md

Co-authored-by: Xiang Yan <[email protected]>
  • Loading branch information
iscai-msft and xiangyan99 authored May 18, 2022
1 parent e0988f2 commit a1a7a37
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Release History

## 1.24.0 (2022-05-05)
## 1.24.0 (2022-05-06)

### Features Added

- Add `SerializationError` and `DeserializationError` in `azure.core.serialization` for errors raised during serialization / deserialization #24113
- Add `SerializationError` and `DeserializationError` in `azure.core.exceptions` for errors raised during serialization / deserialization #24312

## 1.23.1 (2022-03-31)

Expand Down
10 changes: 10 additions & 0 deletions sdk/core/azure-core/azure/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"StreamConsumedError",
"StreamClosedError",
"ResponseNotReadError",
"SerializationError",
"DeserializationError",
]


Expand Down Expand Up @@ -494,3 +496,11 @@ def __init__(self, response):
)
)
super(ResponseNotReadError, self).__init__(message)

class SerializationError(ValueError):
"""Raised if an error is encountered during serialization."""
...

class DeserializationError(ValueError):
"""Raised if an error is encountered during deserialization."""
...
10 changes: 1 addition & 9 deletions sdk/core/azure-core/azure/core/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .utils._utils import _FixedOffset


__all__ = ["NULL", "AzureJSONEncoder", "SerializationError", "DeserializationError"]
__all__ = ["NULL", "AzureJSONEncoder"]


class _Null(object):
Expand Down Expand Up @@ -123,11 +123,3 @@ def default(self, o): # pylint: disable=too-many-return-statements
except AttributeError:
pass
return super(AzureJSONEncoder, self).default(o)

class SerializationError(ValueError):
"""Raised if an error is encountered during serialization."""
...

class DeserializationError(ValueError):
"""Raised if an error is encountered during deserialization."""
...
24 changes: 23 additions & 1 deletion sdk/core/azure-core/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from mock import Mock

# module under test
from azure.core.exceptions import HttpResponseError, ODataV4Error, ODataV4Format
from azure.core.exceptions import HttpResponseError, ODataV4Error, ODataV4Format, SerializationError, DeserializationError
from azure.core.pipeline.transport import RequestsTransportResponse
from azure.core.pipeline.transport._base import _HttpResponseBase as PipelineTransportHttpResponseBase
from azure.core.rest._http_response_impl import _HttpResponseBaseImpl as RestHttpResponseBase
Expand Down Expand Up @@ -320,3 +320,25 @@ def test_datav4_error(self, client, http_request):
with pytest.raises(HttpResponseError) as ex:
response.raise_for_status()
assert "Content: {\"" not in str(ex.value)

def test_serialization_error():
message = "Oopsy bad input passed for serialization"
error = SerializationError(message)
with pytest.raises(SerializationError) as ex:
raise error
assert str(ex.value) == message

with pytest.raises(ValueError) as ex:
raise error
assert str(ex.value) == message

def test_deserialization_error():
message = "Oopsy bad input passed for serialization"
error = DeserializationError(message)
with pytest.raises(DeserializationError) as ex:
raise error
assert str(ex.value) == message

with pytest.raises(ValueError) as ex:
raise error
assert str(ex.value) == message
24 changes: 1 addition & 23 deletions sdk/core/azure-core/tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import json
import sys

from azure.core.serialization import AzureJSONEncoder, NULL, SerializationError, DeserializationError
from azure.core.serialization import AzureJSONEncoder, NULL
import pytest


Expand Down Expand Up @@ -442,25 +442,3 @@ def __init__(self):
]
}
assert json.loads(json_dumps_with_encoder(expected.to_dict())) == expected_dict

def test_serialization_error():
message = "Oopsy bad input passed for serialization"
error = SerializationError(message)
with pytest.raises(SerializationError) as ex:
raise error
assert str(ex.value) == message

with pytest.raises(ValueError) as ex:
raise error
assert str(ex.value) == message

def test_deserialization_error():
message = "Oopsy bad input passed for serialization"
error = DeserializationError(message)
with pytest.raises(DeserializationError) as ex:
raise error
assert str(ex.value) == message

with pytest.raises(ValueError) as ex:
raise error
assert str(ex.value) == message

0 comments on commit a1a7a37

Please sign in to comment.