This topic describes the error handling mechanism of Intelligent Media Management (IMM) SDKs, types of exceptions, and retry policies.
Error handling mechanism
IMM SDK uses exceptions to indicate errors. If an operation does not throw an exception, the operation is successful. If an operation throws an exception, the operation fails.
Types of exceptions
Exceptions are defined in the SDK for handling internal logical errors. You may encounter the following types of exceptions when you use IMM SDK:
Exceptions that indicate errors returned from the server or network errors encountered when IMM SDK sends requests to the server. For example, a network error occurs in case of a network connection failure or server response timeout.
This type of errors is handled by IMM SDK and encapsulated in a unified exception class. When such an error occurs, IMM SDK throws an exception. For more information about this type of errors, see error codes for IMM API operations. This type of errors includes the following information:
Error code: If the error is returned by the server, the error code is the same as that returned by the API operation. If the error is an SDK network error, the error code is RequestError.
Error message: If the error is returned by the server, the error message is the same as that returned by the API operation. If the error is an SDK network error, the error message is "request is failed".
Request ID: the ID of the request for the returned exception. An error includes a request ID only when an error message is returned for the request. If no error message is returned, an empty request ID string is returned. If you receive a request error, contact us.
Exceptions that indicate errors that are generated by IMM SDK and related to platforms or programming languages, for example, memory overflows.
IMM SDK does not handle this type of errors but throws exceptions declared by a native exception class for the platform or programming language so that the requester can handle errors accordingly.
Retry policies
When you use IMM SDK to send a request, the request may fail due to factors such as temporary network disconnection, transmission latency, or slow server response. An exception is thrown for these errors and no retry logic is internally implemented to handle these errors. You need to define the error handling logic, such as request retries or error reporting.
The following sample code provides an error handling example:
@staticmethod
def test_get_project() -> None:
# Specify the project name.
project_name = "myproject1"
# Create the project.
client = Sample.create_client("imm_access_key_id", "imm_access_key_secret")
create_project_request = imm_20200930_models.CreateProjectRequest(
# Specify the IMM project.
project_name=project_name,
# Specify the service role.
service_role='AliyunIMMDefaultRole'
)
runtime = util_models.RuntimeOptions()
response = client.create_project_with_options(create_project_request, runtime)
assert response.body.project.project_name == project_name
# Obtain the project.
get_project_request = imm_20200930_models.GetProjectRequest(project_name='myproject1-2')
runtime = util_models.RuntimeOptions()
try:
# Print the response.
response = client.get_project_with_options(get_project_request, runtime)
print(response.body.to_map())
except Exception as error:
# Print the error message if necessary.
UtilClient.assert_as_string(error.message)
assert error.code == "ResourceNotFound"