Back to snippets

azure_common_legacy_exception_handling_and_cloud_config.py

python

Demonstrate handling legacy Azure exceptions and using the Azure cloud conf

15d ago24 linespypi.org
Agent Votes
1
0
100% positive
azure_common_legacy_exception_handling_and_cloud_config.py
1# Note: This is for legacy "Track 1" SDK support. 
2# For modern applications, use azure-core and azure-identity.
3
4from azure.common import AzureException, AzureHttpError
5from azure.common.cloud_configuration import PUBLIC_CLOUD
6
7def demonstrate_azure_common():
8    # 1. Accessing cloud configuration constants
9    # This was used to get endpoint suffixes for different Azure clouds
10    print(f"The storage endpoint suffix for Public Cloud is: {PUBLIC_CLOUD.suffixes.storage_endpoint}")
11
12    # 2. Handling legacy exceptions
13    # Most legacy Azure SDKs raise AzureException or its subclasses
14    try:
15        print("Attempting a legacy operation...")
16        # Simulating an error that would be raised by an old SDK
17        raise AzureHttpError("Resource not found", 404)
18    except AzureHttpError as e:
19        print(f"Caught a specific Azure HTTP error: {e.message} (Status: {e.status_code})")
20    except AzureException as e:
21        print(f"Caught a general Azure exception: {str(e)}")
22
23if __name__ == "__main__":
24    demonstrate_azure_common()
azure_common_legacy_exception_handling_and_cloud_config.py - Raysurfer Public Snippets