Skip to content

azure.core.exceptions.ODataV4Format.__str__ returns duplicated error messages #41937

@jiasli

Description

@jiasli
  • Package Name: azure-core
  • Package Version: 1.35.0
  • Operating System: Windows
  • Python Version: 3.12

Describe the bug

After #18229, azure.core.exceptions.ODataV4Format.__str__ returns duplicated error messages. For example:

> az group show --name non-exist
(ResourceGroupNotFound) Resource group 'non-exist' could not be found.
Code: ResourceGroupNotFound
Message: Resource group 'non-exist' could not be found.

The output can be overwhelming if the error message is long.

> az keyvault create -g testpolicy1 -n testkv
(RequestDisallowedByPolicy) Resource 'testkv' was disallowed by policy. Policy identifiers: '[{"policyAssignment":{"name
":"Users must use MFA for Create/Update operations","id":"/subscriptions/9fb3fda4-e572-422a-a972-1011d3593176/resourceGr
oups/testpolicy1/providers/Microsoft.Authorization/policyAssignments/mfa-create-update-policy"},"policyDefinition":{"nam
e":"Users must authenticate with multi-factor authentication to create or update resources","id":"/providers/Microsoft.A
uthorization/policyDefinitions/4e6c27d5-a6ee-49cf-b2b4-d8fe90fa2b8b","version":"1.0.0-preview"}},{"policyAssignment":{"n
ame":"Users must use MFA for Create operation","id":"/subscriptions/9fb3fda4-e572-422a-a972-1011d3593176/resourceGroups/
testpolicy1/providers/Microsoft.Authorization/policyAssignments/4e6c27d5-a6ee-49cf-b2b4-d8fe90fa2b8b"},"policyDefinition
":{"name":"Users must authenticate with multi-factor authentication to create or update resources","id":"/providers/Micr
osoft.Authorization/policyDefinitions/4e6c27d5-a6ee-49cf-b2b4-d8fe90fa2b8b","version":"1.0.0-preview"}}]'.
Code: RequestDisallowedByPolicy
Message: Resource 'testkv' was disallowed by policy. Policy identifiers: '[{"policyAssignment":{"name":"Users must use M
FA for Create/Update operations","id":"/subscriptions/9fb3fda4-e572-422a-a972-1011d3593176/resourceGroups/testpolicy1/pr
oviders/Microsoft.Authorization/policyAssignments/mfa-create-update-policy"},"policyDefinition":{"name":"Users must auth
enticate with multi-factor authentication to create or update resources","id":"/providers/Microsoft.Authorization/policy
Definitions/4e6c27d5-a6ee-49cf-b2b4-d8fe90fa2b8b","version":"1.0.0-preview"}},{"policyAssignment":{"name":"Users must us
e MFA for Create operation","id":"/subscriptions/9fb3fda4-e572-422a-a972-1011d3593176/resourceGroups/testpolicy1/provide
rs/Microsoft.Authorization/policyAssignments/4e6c27d5-a6ee-49cf-b2b4-d8fe90fa2b8b"},"policyDefinition":{"name":"Users mu
st authenticate with multi-factor authentication to create or update resources","id":"/providers/Microsoft.Authorization
/policyDefinitions/4e6c27d5-a6ee-49cf-b2b4-d8fe90fa2b8b","version":"1.0.0-preview"}}]'.
Target: testkv

azure.core.exceptions.ODataV4Format.__str__ includes self.message and self.message_details():

def __str__(self) -> str:
return "({}) {}\n{}".format(self.code, self.message, self.message_details())

self.message_details() includes self.message again:

error_str += "\nMessage: {}".format(self.message)

The result of azure.core.exceptions.ODataV4Format.__str__ is later used to create an HttpResponseError:

if self.error:
message = str(self.error)
else:
message = message or "Operation returned an invalid status '{}'".format(self.reason)
super(HttpResponseError, self).__init__(message=message, **kwargs)

and saved in the message attribute and args[0]:

self.message: str = str(message)
self.continuation_token: Optional[str] = kwargs.get("continuation_token")
super(AzureError, self).__init__(self.message, *args)

Even though I can use ex.error to access the original ODataV4Format and its self.message and self.message_details(), but I don't think it is a good idea for azure.core.exceptions.ODataV4Format.__str__ to return duplicated error messages. Returning self.message is enough in my opinion.

Metadata

Metadata

Assignees

Labels

Azure.CoreClientThis issue points to a problem in the data-plane of the library.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions