When you use an SDK to send API requests to Log Service, exceptions may occur due to network interruption or network latency. This topic describes the handling mechanism of exceptions that may occur when you use an SDK to send API requests.

Exception types and handling mechanism

SDK exceptions are divided into the following types:
  • Exceptions that are returned by Log Service. This type of exceptions is handled by SDKs. For more information about the error codes of this type of exceptions, see Common error codes.
  • Network exceptions that occur when you use an SDK to send requests. This type of exceptions includes network disconnection and server response timeout.
  • Exceptions that are generated by SDKs and related to platforms or languages, for example, memory overflows.
Exceptions that occur during the implementation of SDKs are thrown out for troubleshooting. Exceptions are handled by using the following mechanism:
  • Exceptions that are returned by Log Service and network exceptions are packaged in the LogException class and thrown out by SDKs.
  • Exceptions that are generated by SDKs and related to platforms or languages are not handled by SDKs. They are thrown into the Native Exception class of the related platform or language.

LogException

The LogException class is defined by SDKs to handle the exceptions that are returned by Log Service or network exceptions. This class inherits the base class that is used to handle all exceptions in each programming language. The class provides the following exception information:
  • Error code: indicates the exception type. The error code of an exception that is returned by Log Service is the same as the relevant API error code. The error code of a network exception is RequestError. For more information about error codes, see API reference.
  • Error message: indicates the message that is returned together with an exception. The error code of an exception that is returned by Log Service is the same as the relevant API error message. The error message of a network exception is request is failed. For more information about error codes, see API reference.
  • Request ID: indicates the ID of the request of the returned exception. The request ID is generated by Log Service. This ID is valid only if Log Service returns an error message. Otherwise, the ID is an empty string. If you receive a request exception, you can provide the relevant request ID to the Log Service team for troubleshooting.

Request failures and retries

When you use an SDK to access Log Service, the request may fail due to temporary network disconnection, transmission latency, or slow server response. These exceptions are directly thrown out. Log Service does not implement the retry logic. Therefore, when you use an SDK, you must customize the handling logic, for example, retry the request or report an exception.

Examples

Network exceptions occur when you access the project that is named big-game in the China (Hangzhou) region. To handle such exceptions, you can use the following code in different programming languages to specify the number of retries:
  • Java:
    // Other pieces of code.
    String accessId = "your_access_id";   //TODO: The AccessKey ID of your Alibaba Cloud account.
    String accessKey = "your_access_key"; //TODO: The AccessKey secret of your Alibaba Cloud account.
    String project = "big-game";
    String endpoint = "cn-hangzhou.sls.aliyuncs.com";
    int max_retries = 3;
    /*
     * Create a client.
     */
    Client client = new Client(accessId, accessKey, endpoint);
    ListLogStoresRequest lsRequest = new ListLogStoresRequest(project);
    for (int i = 0; i < max_retries; i++)
    {
        try
        {
            ListLogStoresResponse res = client.ListLogStores(lsRequest)
            //TODO: Handle returned exceptions.
            break;
        }
        catch(LogException ex)
        {
            if (e.GetErrorCode() == "RequestError")
            {
                if ( i == max_retries - 1)
                {
                    System.out.println("request is still failed after all retries.");
                    break;
                }
                else
                    System.out.println("request error happens, retry it!") ;
            }
            else
            {
                System.out.println("error code :" + e.GetErrorCode());
                System.out.println("error message :" + e.GetErrorMessage());
                System.out.println("error requestId :" + e.GetRequestId());
                break;
            }
        }
        catch(Exception $ex)
        {
            System.out.println("unrecoverable exception when listing logstores.");
            break;
        }
    }
    // Other pieces of code.
  • .NET (C#):
    // Other pieces of code.
    String accessId = "your_access_id";     //TODO: The AccessKey ID of your Alibaba Cloud account.
    String accessKey = "your_access_key";    //TODO: The AccessKey secret of your Alibaba Cloud account.
    String project = "big-game";
    String endpoint = "cn-hangzhou.sls.aliyuncs.com";
    int max_retries = 3;
    // Create a client.
    SLSClient client = new SLSClient(endpoint, accessId, accessKey);
    ListLogstoresRequest request = new ListLogstoresRequest();
    request.Project = project;
    for (int i = 0; i < max_retries; i++)
    {
        try
        {
            ListLogstoresResponse response = client.ListLogstores(request);
            //TODO: Handle returned exceptions.
            break;
        }
        catch(LogException ex)
        {
            if (e.errorCode == "SLSRequestError")
            {
                if ( i == max_retries - 1)
                {
                    Console.Writeline("request is still failed after all retries.");
                    break;
                }
                else
                {
                    Console.Writeline("request error happens, retry it!") ;
                }
            }
            else
            {
                Console.Writeline("error code :" + e.errorCode);
                Console.Writeline("error message :" + e.Message);
                Console.Writeline("error requestId :" + e.RequestId);
                break;
            }
        }
        catch(Exception $ex)
        {
            Console.Writeline("unrecoverable exception when listing logstores.");
            break;
        }
    }
    // Other pieces of code.
  • PHP :
    <? php
    // Other pieces of code.
    $endpoint = 'cn-hangzhou.sls.aliyuncs.com';
    $accessId = 'your_access_id'; // TODO: The AccessKey ID of your Alibaba Cloud account.
    $accessKey = 'your_access_key'; //TODO: The AccessKey secret of your Alibaba Cloud account.
    $maxRetries = 3;
    // Create an sls client.
    $client = new Aliyun_Sls_Client($endpoint, $accessId, $accessKey);
    $project = 'big-game';
    $request = new Aliyun_Sls_Models_ListLogstoresRequest($project);
    for($i = 0; $i < $maxRetries; ++$i)
    {
        try
        {
            $response = $client->ListLogstores($request);
            //TODO: Handle returned exceptions.
            break;
        }
        catch (Aliyun_Sls_Exception $e)
        {
            if ($e->getErrorCode()=='RequestError')
            {
                if ($i+1 == $maxRetries)
                {
                    echo "error code :" . $e->getErrorCode() . PHP_EOL;
                    echo "error message :" . $e->getErrorMessage() . PHP_EOL;
                    break;
                }
                echo 'request error happens, retry it!' . PHP_EOL;
            }
            else
            {
                echo "error code :" . $e->getErrorCode() . PHP_EOL;
                echo "error message :" . $e->getErrorMessage() . PHP_EOL;
                echo "error requestId :" . $e->getRequestId() . PHP_EOL;
                break;
            }
        }
        catch (Exception $ex)
        {
            echo 'unrecoverable exception when listing logstores.' . PHP_EOL;
            var_dump($ex);
            break;
        }
    }
    // Other pieces of code.
  • Python:
    // Other pieces of code.
    endpoint = 'cn-hangzhou.sls.aliyuncs.com'
    accessId = 'your_access_id' #  TODO: The AccessKey ID of your Alibaba Cloud account.
    accessKey = 'your_access_key' #  TODO: The AccessKey secret of your Alibaba Cloud account.
    maxRetries = 3
    # Create a client instance.
    client = Client(endpoint, accessId, accessKey)
    project = 'big-game'
    lsRequest = ListLogstoresRequest(project)
    for i in xrange(maxRetries):
        try:
            res = client.ListLogstores(lsRequest)
            # TODO: Handle returned exceptions.
            break
        except LogException as e:
            if e.get_error_code() == "RequestError":
                if i+1 == maxRetries:
                    print "error code :" + e.get_error_code()
                    print "error message :" + e.get_error_message()
                    break
                else:
                    print "request error happens, retry it!"
            else:
                print "error code :" + e.get_error_code()
                print "error message :" + e.get_error_message()
                print "error requestId :" + e.get_request_id()
                break
        except Exception as e:
            print 'unrecoverable exception when listing logstores.'
            break
    // Other pieces of code.