-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Description
- 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():
azure-sdk-for-python/sdk/core/azure-core/azure/core/exceptions.py
Lines 250 to 251 in d850231
| 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:
azure-sdk-for-python/sdk/core/azure-core/azure/core/exceptions.py
Lines 389 to 394 in d850231
| 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]:
azure-sdk-for-python/sdk/core/azure-core/azure/core/exceptions.py
Lines 303 to 305 in d850231
| 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.