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

Improved error message in the from_dict #19416

Merged
merged 9 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.15.1 (Unreleased)

### Bug Fixes

- Improved error message in the `from_dict` method of `CloudEvent` when a wrong schema is sent.

## 1.15.0 (2021-06-04)

Expand Down
31 changes: 23 additions & 8 deletions sdk/core/azure-core/azure/core/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,26 @@ def from_dict(cls, event):
if extensions:
kwargs["extensions"] = extensions

return cls(
id=event.get("id"),
source=event["source"],
type=event["type"],
specversion=event.get("specversion"),
time=_convert_to_isoformat(event.get("time")),
**kwargs
)
try:
event_obj = cls(
id=event.get("id"),
source=event["source"],
type=event["type"],
specversion=event.get("specversion"),
time=_convert_to_isoformat(event.get("time")),
**kwargs
)
except KeyError:
# https://github.com/cloudevents/spec Cloud event spec requires source, type,
# specversion. We autopopulate everything other than source, type.
if not all([_ in event for _ in ("source", "type")]):
if all([_ in event for _ in ("subject", "eventType", "data", "dataVersion", "id", "eventTime")]):
raise ValueError(
"The event you are trying to parse follows the Eventgrid Schema. You can parse" +
" EventGrid events using EventGridEvent.from_dict method in the azure-eventgrid library."
)
raise ValueError(
"The event does not conform to the cloud event spec https://github.com/cloudevents/spec." +
" The `source` and `type` params are required."
)
return event_obj
34 changes: 34 additions & 0 deletions sdk/core/azure-core/tests/test_messaging_cloud_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,37 @@ def test_cloud_custom_dict_ms_precision_is_eq_six_z_not():
assert date_obj.day == 18
assert date_obj.hour == 20
assert date_obj.microsecond == 123456

def test_eventgrid_event_schema_raises():
cloud_custom_dict = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"data":{"team": "event grid squad"},
"dataVersion": "1.0",
"subject":"Azure.Sdk.Sample",
"eventTime":"2020-08-07T02:06:08.11969Z",
"eventType":"pull request",
}
with pytest.raises(ValueError, match="The event you are trying to parse follows the Eventgrid Schema. You can parse EventGrid events using EventGridEvent.from_dict method in the azure-eventgrid library."):
CloudEvent.from_dict(cloud_custom_dict)

def test_wrong_schema_raises_no_source():
cloud_custom_dict = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2020-08-07T02:06:08.11969Z",
"specversion":"1.0",
}
with pytest.raises(ValueError, match="The event does not conform to the cloud event spec https://github.com/cloudevents/spec. The `source` and `type` params are required."):
CloudEvent.from_dict(cloud_custom_dict)

def test_wrong_schema_raises_no_type():
cloud_custom_dict = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"data":{"team": "event grid squad"},
"source":"Azure/Sdk/Sample",
"time":"2020-08-07T02:06:08.11969Z",
"specversion":"1.0",
}
with pytest.raises(ValueError, match="The event does not conform to the cloud event spec https://github.com/cloudevents/spec. The `source` and `type` params are required."):
CloudEvent.from_dict(cloud_custom_dict)