Azure / Azure/azure-sdk-for-python

`azure.core.exceptions.ODataV4Format.__str__` returns duplicated error messages

Open
#41,937 1 comment 0 reactions 1 assignee Claimed by @xiangyan99 View on GitHub
Azure.Core Client needs-team-attention
Dominant language
Python
Stars
5.6k
Forks
3.4k
Avg merge
1d 21h
Merged PRs (30d)
193

Description

- **Package Name**: azure-core
- **Package Version**: 1.35.0
- **Operating System**: Windows
- **Python Version**: 3.12

**Describe the bug**

After https://github.com/Azure/azure-sdk-for-python/pull/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()`:

https://github.com/Azure/azure-sdk-for-python/blob/d85023107fade43aa9a05f46ce4c1ae8b73597ed/sdk/core/azure-core/azure/core/exceptions.py#L250-L251

`self.message_details()` includes `self.message` again:

https://github.com/Azure/azure-sdk-for-python/blob/d85023107fade43aa9a05f46ce4c1ae8b73597ed/sdk/core/azure-core/azure/core/exceptions.py#L260

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

https://github.com/Azure/azure-sdk-for-python/blob/d85023107fade43aa9a05f46ce4c1ae8b73597ed/sdk/core/azure-core/azure/core/exceptions.py#L389-L394

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

https://github.com/Azure/azure-sdk-for-python/blob/d85023107fade43aa9a05f46ce4c1ae8b73597ed/sdk/core/azure-core/azure/core/exceptions.py#L303-L305

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.