Our generated clients raise exceptions defined in azure-core
. While the base for all exceptions is AzureError
,
HttpResponseError
is also a common base catch-all for exceptions, as these errors are thrown in the case of a request being made, and a non-successful
status code being received from the service.
Our generated code also offers some default mapping of status codes to exceptions. These are 401
to ClientAuthenticationError
, 404
to
ResourceNotFoundError
, and 409
to ResourceExistsError
.
A very basic form of error handling looks like this:
from azure.identity import DefaultAzureCredential
from azure.pets import PetsClient
client = PetsClient(credential=DefaultAzureCredential())
try:
dog = client.get_dog()
except HttpResponseError as e:
print("{}: {}".format(e.status_code, e.message))
You can also catch errors with more granularity, i.e. just catching a ResourceExistsError
.
from azure.identity import DefaultAzureCredential
from azure.pets import PetsClient
client = PetsClient(credential=DefaultAzureCredential())
try:
dog = client.get_dog()
except ResourceExistsError as e:
print(e.message)
A final note regarding error models: If you define your own special error model (like this), we still expose these to the users. Though the error thrown to
the user will be one defined in azure-core
(most likely HttpResponseError
), we expose your specially-defined swagger
models through the model
property on the returned error. I.e.:
from azure.identity import DefaultAzureCredential
from azure.pets import PetsClient
client = PetsClient(credential=DefaultAzureCredential())
try:
dog = client.get_dog()
except HttpResponseError as e:
pet_action_error = e.model
Our generated libraries use the standard logging
library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.
Our logger's name is azure
.
Detailed DEBUG level logging, including request/response bodies and un-redacted headers, can be enabled on a client with the logging_enable argument:
import logging
import sys
from azure.identity import DefaultAzureCredential
from azure.pets import PetsClient
# Create a logger for the 'azure' SDK
logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)
# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
client = PetsClient(credential=DefaultAzureCredential(), logging_enable=True)
Network trace logging can also be enabled for any single operation:
dog = client.get_dog(logging_enable=True)