Troubleshooting
January 8, 2021 ยท View on GitHub
Error Handling
Our generated clients raise exceptions defined in com.azure.core.exception. While the base for all exceptions is AzureException,
HttpResponseException 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.
A very basic form of error handling looks like this
package com.azure.pets;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.pets.models.Dog;
public static void main(String args[])
{
PetsClient client = new PetsClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
try {
Dog dog = client.getDog();
} catch (HttpResponseException e) {
System.out.println(e.getMessage());
}
}
You can also catch errors with more granularity, i.e. just catching a ResourceNotFoundException.
package com.azure.pets;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.pets.models.Dog;
public static void main(String args[])
{
PetsClient client = new PetsClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
try {
Dog dog = client.getDog();
} catch (ResourceNotFoundException e) {
System.out.println(e.getMessage());
}
}
Logging
You can set the AZURE_LOG_LEVEL environment variable to view logging statements made in the client library. For example, setting AZURE_LOG_LEVEL=LogLevel.ERROR would show all error log messages. The log levels can be found here: log levels.
View our logging wiki for more detailed instructions on enabling logging.