This topic describes the exception types for the V2.0 SDK and how to handle them.
Exception types
Exception type | Description | Recommended action |
UnretryableException | An exception that is caused by network issues. This exception is typically thrown when the maximum number of retries is reached because of a network problem. |
|
ClientException | A client exception. The HTTP status code is in the range of 400 to 499. This exception can be caused by issues such as invalid request parameters, authentication failures, or insufficient permissions. | In Troubleshoot, copy the |
ServerException | A server exception. The HTTP status code is in the range of 500 to 599. This exception indicates a server-side issue with Alibaba Cloud, such as an internal server error or an unavailable service. | Retry the operation. If the issue persists after multiple retries, contact us for help. |
ThrottlingException | A throttling exception. This exception is thrown when an API call triggers the Alibaba Cloud throttling mechanism. | Adjust your API call frequency to comply with the throttling limits in the API documentation. |
The `ClientException`, `ServerException`, and `ThrottlingException` provide the following parameters to help you troubleshoot issues:
request_id: The unique identifier of the request.
status_code: The HTTP status code returned by OpenAPI.
code: The error code returned by OpenAPI.
message: The error message returned by OpenAPI.
data: The detailed information returned by the server when the error occurred.
Sample code:
In this example, error messages are displayed for reference only. In the actual business scenario, do not ignore exceptions in your project. We recommend that you properly handle exceptions by performing operations such as reporting exceptions, recording logs, and performing retries. This helps ensure the robustness and stability of your system.
import os
from alibabacloud_ecs20140526.client import Client
from alibabacloud_ecs20140526.models import DescribeImagesRequest
from alibabacloud_tea_openapi.exceptions import (
ClientException,
ServerException,
ThrottlingException
)
from alibabacloud_tea_openapi.models import Config
from darabonba.exceptions import UnretryableException
'''Elastic Compute Service example'''
# Initialize Config.
config = Config(
access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
region_id='<REGION_ID>'
)
try:
client = Client(config)
# Initialize Request.
request = DescribeImagesRequest(image_id='<IMAGE_ID>', region_id='<REGION_ID>')
response = client.describe_images(request)
except UnretryableException as e:
# A network exception occurred. This example only prints the exception. Handle exceptions with care. Do not ignore them in your projects.
print(e)
except (ClientException, ServerException, ThrottlingException) as e:
# A client exception occurred. This example only prints the exception. Handle exceptions with care. Do not ignore them in your projects.
print(f"RequestId:{e.request_id},StatusCode:{e.status_code},Message:{e.message},Data:{e.data}")
Resolve exceptions
If an exception occurs when you call an API, you can obtain help in the following ways:
In Troubleshoot, copy the RequestId from the exception to diagnose the error and find its cause and solution. For more information, see OpenAPI Troubleshoot.
Find a solution in the relevant product documentation based on the error code in the exception. For example, for ECS error codes, see Common error codes.
Check if a solution is available in the FAQ.
Contact us for assistance.