diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 10e242697b12..ff3163d6f9b4 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -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) diff --git a/sdk/core/azure-core/azure/core/messaging.py b/sdk/core/azure-core/azure/core/messaging.py index 9131a7b46d69..f619724b18c7 100644 --- a/sdk/core/azure-core/azure/core/messaging.py +++ b/sdk/core/azure-core/azure/core/messaging.py @@ -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 diff --git a/sdk/core/azure-core/tests/test_messaging_cloud_event.py b/sdk/core/azure-core/tests/test_messaging_cloud_event.py index c82fa4f55370..bf35931c6565 100644 --- a/sdk/core/azure-core/tests/test_messaging_cloud_event.py +++ b/sdk/core/azure-core/tests/test_messaging_cloud_event.py @@ -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) \ No newline at end of file