0
# Exception Handling
1
2
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.
3
4
## Capabilities
5
6
### Azure Exception Classes
7
8
Base exception classes for Azure operations with automatic HTTP error subclass creation.
9
10
```python { .api }
11
class AzureException(Exception):
12
"""
13
Base exception class for Azure operations.
14
15
This is the root exception class that all other Azure exceptions inherit from.
16
Use this in except blocks when you want to catch any Azure-related error.
17
"""
18
pass
19
```
20
21
```python { .api }
22
class AzureHttpError(AzureException):
23
"""
24
HTTP error with status code, auto-creates specific subclasses based on status.
25
26
This class automatically creates specific subclasses for common HTTP status codes:
27
- 404 becomes AzureMissingResourceHttpError
28
- 409 becomes AzureConflictHttpError
29
30
Args:
31
message (str): Error message describing the HTTP error
32
status_code (int): HTTP status code (404, 409, etc.)
33
34
Attributes:
35
status_code (int): The HTTP status code associated with this error
36
"""
37
def __init__(self, message, status_code): ...
38
def __new__(cls, message, status_code, *args, **kwargs): ...
39
```
40
41
```python { .api }
42
class AzureConflictHttpError(AzureHttpError):
43
"""
44
HTTP 409 conflict error.
45
46
Automatically created when AzureHttpError is instantiated with status_code=409.
47
Indicates that the request conflicts with the current state of the resource.
48
49
Args:
50
message (str): Error message describing the conflict
51
status_code (int): Always 409 for this exception type
52
"""
53
def __init__(self, message, status_code): ...
54
```
55
56
```python { .api }
57
class AzureMissingResourceHttpError(AzureHttpError):
58
"""
59
HTTP 404 missing resource error.
60
61
Automatically created when AzureHttpError is instantiated with status_code=404.
62
Indicates that the requested resource was not found.
63
64
Args:
65
message (str): Error message describing the missing resource
66
status_code (int): Always 404 for this exception type
67
"""
68
def __init__(self, message, status_code): ...
69
```
70
71
### Exception Re-exports
72
73
Re-exported exceptions from msrest and msrestazure libraries for convenient access to common Azure SDK exceptions.
74
75
#### MSRest Exceptions
76
77
```python { .api }
78
# From msrest.exceptions
79
class ClientException(Exception):
80
"""Base exception for client-side errors."""
81
pass
82
83
class SerializationError(ClientException):
84
"""Error during request serialization."""
85
pass
86
87
class DeserializationError(ClientException):
88
"""Error during response deserialization."""
89
pass
90
91
class TokenExpiredError(ClientException):
92
"""Authentication token has expired."""
93
pass
94
95
class ClientRequestError(ClientException):
96
"""Error in client request processing."""
97
pass
98
99
class AuthenticationError(ClientException):
100
"""Authentication failed."""
101
pass
102
103
class HttpOperationError(ClientException):
104
"""HTTP operation failed."""
105
pass
106
```
107
108
#### MSRestAzure Exceptions
109
110
```python { .api }
111
# From msrestazure.azure_exceptions
112
class CloudError(Exception):
113
"""Azure cloud service error."""
114
pass
115
```
116
117
#### MSRest Authentication Classes
118
119
These classes are re-exported through `azure.common.credentials` for convenient access.
120
121
```python { .api }
122
# From msrest.authentication (via azure.common.credentials)
123
class BasicAuthentication:
124
"""Basic HTTP authentication."""
125
pass
126
127
class BasicTokenAuthentication:
128
"""Basic token authentication."""
129
pass
130
131
class OAuthTokenAuthentication:
132
"""OAuth token authentication."""
133
pass
134
```
135
136
#### MSRestAzure Active Directory Classes
137
138
These classes are re-exported through `azure.common.credentials` for convenient access.
139
140
```python { .api }
141
# From msrestazure.azure_active_directory (via azure.common.credentials)
142
class InteractiveCredentials:
143
"""Interactive Azure AD credentials."""
144
pass
145
146
class ServicePrincipalCredentials:
147
"""Service principal credentials."""
148
pass
149
150
class UserPassCredentials:
151
"""Username/password credentials."""
152
pass
153
```
154
155
## Usage Examples
156
157
### Basic Exception Handling
158
159
```python
160
from azure.common import AzureException, AzureHttpError, AzureConflictHttpError, AzureMissingResourceHttpError
161
162
try:
163
# Simulate an Azure operation
164
raise AzureHttpError("Resource not found", 404)
165
except AzureMissingResourceHttpError as e:
166
print(f"Resource not found: {e}")
167
print(f"Status code: {e.status_code}")
168
except AzureConflictHttpError as e:
169
print(f"Resource conflict: {e}")
170
print(f"Status code: {e.status_code}")
171
except AzureHttpError as e:
172
print(f"HTTP error: {e}")
173
print(f"Status code: {e.status_code}")
174
except AzureException as e:
175
print(f"General Azure error: {e}")
176
```
177
178
### Automatic Subclass Creation
179
180
```python
181
from azure.common import AzureHttpError, AzureConflictHttpError, AzureMissingResourceHttpError
182
183
# These will automatically create the appropriate subclass
184
error_404 = AzureHttpError("Not found", 404)
185
error_409 = AzureHttpError("Conflict", 409)
186
error_500 = AzureHttpError("Server error", 500)
187
188
print(type(error_404)) # <class 'azure.common.AzureMissingResourceHttpError'>
189
print(type(error_409)) # <class 'azure.common.AzureConflictHttpError'>
190
print(type(error_500)) # <class 'azure.common.AzureHttpError'>
191
```
192
193
### Using Re-exported Exceptions
194
195
```python
196
from azure.common.exceptions import CloudError, SerializationError, AuthenticationError
197
198
try:
199
# Some Azure SDK operation
200
pass
201
except CloudError as e:
202
print(f"Azure cloud error: {e}")
203
except SerializationError as e:
204
print(f"Request serialization failed: {e}")
205
except AuthenticationError as e:
206
print(f"Authentication failed: {e}")
207
```
208
209
### Using Re-exported Authentication Classes
210
211
```python
212
from azure.common.credentials import (
213
BasicAuthentication,
214
ServicePrincipalCredentials,
215
InteractiveCredentials
216
)
217
218
# Create basic authentication
219
auth = BasicAuthentication('username', 'password')
220
221
# Create service principal credentials
222
sp_creds = ServicePrincipalCredentials(
223
client_id='your-client-id',
224
secret='your-client-secret',
225
tenant='your-tenant-id'
226
)
227
228
# Create interactive credentials
229
interactive_creds = InteractiveCredentials(
230
client_id='your-client-id',
231
tenant='your-tenant-id'
232
)
233
```