Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] move SerializationError and DeserializationError to exceptions #24312

Merged
merged 4 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Features Added

- Add `SerializationError` and `DeserializationError` in `azure.core.serialization` for errors raised during serialization / deserialization #24113
- Add `SerializationError` and `DeserializationError` in `azure.core.serialization` for errors raised during serialization / deserialization #24312
xiangyan99 marked this conversation as resolved.
Show resolved Hide resolved

## 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