Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def _send(self, method, url, param=None, body=None, api_version=V1_0):
r = send_raw_request(self._cli_ctx, method, url, resource=self._resource, uri_parameters=param,
body=body)
except HTTPError as ex:
raise GraphError(ex.response.json()['error']['message'], ex.response) from ex
# https://learn.microsoft.com/en-us/graph/errors#error-resource-type
error = ex.response.json()['error']
code = error['code']
message = error['message']
raise GraphError(f"({code}) {message}", ex.response) from ex
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Will code and message always exist? Should we use err.get('Code', '')?

Copy link
Copy Markdown
Member Author

@jiasli jiasli Sep 23, 2024

Choose a reason for hiding this comment

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

Will code and message always exist?

A good catch! I can't guarantee this. Using .get() is definitely more robust.

But, we can't use err.get('code', ''). If code does exist but is null, it will be rendered as 'None'. https://docs.python.org/3.12/library/stdtypes.html#dict.get

It's better to use err.get('code') or ''.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wait. code and message are guaranteed to exist: https://learn.microsoft.com/en-us/graph/errors#error-resource-type

image

# Other exceptions like AuthenticationError should not be handled here, so we don't catch CLIError

if r.text:
Expand Down
Loading