The V2.0 SDK defines four exception types. Learn how to identify and handle each type.
Exception types
|
Exception type |
Description |
Recommended action |
|
UnretryableException |
A network-related exception, typically thrown after the maximum number of retries is exhausted due to a network problem. |
|
|
ClientException |
A client-side exception with an HTTP status code in the 400–499 range, caused by issues such as invalid request parameters, authentication failures, or insufficient permissions. |
In Troubleshoot, copy the |
|
ServerException |
A server-side exception with an HTTP status code in the 500–599 range, indicating an Alibaba Cloud service issue such as an internal server error or service unavailability. |
Retry the operation. If the issue persists after multiple retries, contact us for help. |
|
ThrottlingException |
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. |
`ClientException`, `ServerException`, and `ThrottlingException` expose the following parameters for troubleshooting:
-
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:
This example only prints output for demonstration. In production, handle exceptions properly—propagate them, log them, or attempt recovery to ensure system stability.
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 during an API call, you can resolve it 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.