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 4 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
13 changes: 13 additions & 0 deletions sdk/core/azure-core/azure/core/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ def from_dict(cls, event):
:type event: dict
:rtype: CloudEvent
"""
# 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")]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the description of the doc or a link?

Copy link
Contributor Author

@rakshith91 rakshith91 Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if all([_ in event for _ in ("subject", "eventType", "data", "dataVersion", "id", "eventTime")]):
raise ValueError(
"The event does not conform to the cloud event spec https://github.com/cloudevents/spec." +
" Try using the EventGridEvent from azure-eventgrid library"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be more explicit:
It looks like your event is an EventGrid schema. You can parse EventGrid events using EventGridEvent.from_dict of the azure-eventgrid library

)
raise ValueError(
Copy link
Member

@lmazuel lmazuel Jun 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do this whole block into a try/except around the cls at the end, pure EAFP

"The event does not conform to the cloud event spec https://github.com/cloudevents/spec." +
" source and type are required."
)

kwargs = {} # type: Dict[Any, Any]
reserved_attr = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these keys case-sensitive?

Copy link
Contributor Author

@rakshith91 rakshith91 Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - making it non case sensitive would be breaking

"data",
Expand Down
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 does not conform to the cloud event spec https://github.com/cloudevents/spec. Try using the EventGridEvent from 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. source and type 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. source and type are required."):
CloudEvent.from_dict(cloud_custom_dict)