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

Conversation

rakshith91
Copy link
Contributor

@rakshith91 rakshith91 commented Jun 23, 2021

Fixes #19410

Background:
@jongio's code tried sending EG event schema to cloud event's from_dict method which won't work. Need to imrpove the error message here.

@@ -122,6 +122,16 @@ def from_dict(cls, event):
:type event: dict
:rtype: CloudEvent
"""
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.

raise ValueError(
"The event does not conform to the cloud event 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

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

"The event does not conform to the cloud event spec https://github.com/cloudevents/spec." +
" Try using the EventGridEvent from 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

@rakshith91 rakshith91 merged commit 5efa87d into Azure:main Jun 28, 2021
iscai-msft added a commit that referenced this pull request Jun 28, 2021
…into rest_layer

* 'main' of https://github.com/Azure/azure-sdk-for-python:
  modify _on to _time (#19546)
  Improved error message in the `from_dict` (#19416)
  Regenerate code with latest commit (#19512)
  [auto-release-pipeline] update auto-calculation rule for version number (#19488)
  fix dependencies (#19513)
rakshith91 pushed a commit to rakshith91/azure-sdk-for-python that referenced this pull request Jul 16, 2021
* 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
azure-sdk pushed a commit to azure-sdk/azure-sdk-for-python that referenced this pull request Jul 1, 2022
[OperationalInsights] workspace and component purge  (Azure#19416)

* Add blockchain to latest profile

* Add additional types

* Purge update

* bad syntax fix for workspacePurge examples

* pretty

Co-authored-by: Mark Cowlishaw <markcowl@microsoft.com>
azure-sdk pushed a commit to azure-sdk/azure-sdk-for-python that referenced this pull request Jul 1, 2022
[OperationalInsights] workspace and component purge  (Azure#19416)

* Add blockchain to latest profile

* Add additional types

* Purge update

* bad syntax fix for workspacePurge examples

* pretty

Co-authored-by: Mark Cowlishaw <markcowl@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CloudEvent should throw a proper message when sent wrong event schema
3 participants