Skip to content

Commit

Permalink
Improved error message in the from_dict (Azure#19416)
Browse files Browse the repository at this point in the history
* Improved error message in the `from_dict`

* lint

* spec lin

* tests

* Add AAD support

* eafp

* Revert "Add AAD support"

This reverts commit 1cc83d9.

* language

* lint
  • Loading branch information
Rakshith Bhyravabhotla authored and rakshith91 committed Jul 16, 2021
1 parent d75091e commit 99c8fdf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
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)

0 comments on commit 99c8fdf

Please sign in to comment.