Microsoft Azure common code library providing shared utilities, exceptions, and profile management for all Azure Python SDK packages.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core exception classes providing consistent error handling across all Azure services. The exception hierarchy includes automatic subclass creation based on HTTP status codes and comprehensive re-exports from msrest and msrestazure libraries.
Base exception classes for Azure operations with automatic HTTP error subclass creation.
class AzureException(Exception):
"""
Base exception class for Azure operations.
This is the root exception class that all other Azure exceptions inherit from.
Use this in except blocks when you want to catch any Azure-related error.
"""
passclass AzureHttpError(AzureException):
"""
HTTP error with status code, auto-creates specific subclasses based on status.
This class automatically creates specific subclasses for common HTTP status codes:
- 404 becomes AzureMissingResourceHttpError
- 409 becomes AzureConflictHttpError
Args:
message (str): Error message describing the HTTP error
status_code (int): HTTP status code (404, 409, etc.)
Attributes:
status_code (int): The HTTP status code associated with this error
"""
def __init__(self, message, status_code): ...
def __new__(cls, message, status_code, *args, **kwargs): ...class AzureConflictHttpError(AzureHttpError):
"""
HTTP 409 conflict error.
Automatically created when AzureHttpError is instantiated with status_code=409.
Indicates that the request conflicts with the current state of the resource.
Args:
message (str): Error message describing the conflict
status_code (int): Always 409 for this exception type
"""
def __init__(self, message, status_code): ...class AzureMissingResourceHttpError(AzureHttpError):
"""
HTTP 404 missing resource error.
Automatically created when AzureHttpError is instantiated with status_code=404.
Indicates that the requested resource was not found.
Args:
message (str): Error message describing the missing resource
status_code (int): Always 404 for this exception type
"""
def __init__(self, message, status_code): ...Re-exported exceptions from msrest and msrestazure libraries for convenient access to common Azure SDK exceptions.
# From msrest.exceptions
class ClientException(Exception):
"""Base exception for client-side errors."""
pass
class SerializationError(ClientException):
"""Error during request serialization."""
pass
class DeserializationError(ClientException):
"""Error during response deserialization."""
pass
class TokenExpiredError(ClientException):
"""Authentication token has expired."""
pass
class ClientRequestError(ClientException):
"""Error in client request processing."""
pass
class AuthenticationError(ClientException):
"""Authentication failed."""
pass
class HttpOperationError(ClientException):
"""HTTP operation failed."""
pass# From msrestazure.azure_exceptions
class CloudError(Exception):
"""Azure cloud service error."""
passThese classes are re-exported through azure.common.credentials for convenient access.
# From msrest.authentication (via azure.common.credentials)
class BasicAuthentication:
"""Basic HTTP authentication."""
pass
class BasicTokenAuthentication:
"""Basic token authentication."""
pass
class OAuthTokenAuthentication:
"""OAuth token authentication."""
passThese classes are re-exported through azure.common.credentials for convenient access.
# From msrestazure.azure_active_directory (via azure.common.credentials)
class InteractiveCredentials:
"""Interactive Azure AD credentials."""
pass
class ServicePrincipalCredentials:
"""Service principal credentials."""
pass
class UserPassCredentials:
"""Username/password credentials."""
passfrom azure.common import AzureException, AzureHttpError, AzureConflictHttpError, AzureMissingResourceHttpError
try:
# Simulate an Azure operation
raise AzureHttpError("Resource not found", 404)
except AzureMissingResourceHttpError as e:
print(f"Resource not found: {e}")
print(f"Status code: {e.status_code}")
except AzureConflictHttpError as e:
print(f"Resource conflict: {e}")
print(f"Status code: {e.status_code}")
except AzureHttpError as e:
print(f"HTTP error: {e}")
print(f"Status code: {e.status_code}")
except AzureException as e:
print(f"General Azure error: {e}")from azure.common import AzureHttpError, AzureConflictHttpError, AzureMissingResourceHttpError
# These will automatically create the appropriate subclass
error_404 = AzureHttpError("Not found", 404)
error_409 = AzureHttpError("Conflict", 409)
error_500 = AzureHttpError("Server error", 500)
print(type(error_404)) # <class 'azure.common.AzureMissingResourceHttpError'>
print(type(error_409)) # <class 'azure.common.AzureConflictHttpError'>
print(type(error_500)) # <class 'azure.common.AzureHttpError'>from azure.common.exceptions import CloudError, SerializationError, AuthenticationError
try:
# Some Azure SDK operation
pass
except CloudError as e:
print(f"Azure cloud error: {e}")
except SerializationError as e:
print(f"Request serialization failed: {e}")
except AuthenticationError as e:
print(f"Authentication failed: {e}")from azure.common.credentials import (
BasicAuthentication,
ServicePrincipalCredentials,
InteractiveCredentials
)
# Create basic authentication
auth = BasicAuthentication('username', 'password')
# Create service principal credentials
sp_creds = ServicePrincipalCredentials(
client_id='your-client-id',
secret='your-client-secret',
tenant='your-tenant-id'
)
# Create interactive credentials
interactive_creds = InteractiveCredentials(
client_id='your-client-id',
tenant='your-tenant-id'
)Install with Tessl CLI
npx tessl i tessl/pypi-azure-common