All Products
Search
Document Center

Simple Log Service:Exception handling

Last Updated:Oct 26, 2023

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

Exception types and handling mechanism

SDK exceptions are classified into the following types:

  • Exceptions that are returned by Simple Log Service. This type of exceptions are handled by Simple Log Service SDK. For more information about this type of exceptions, see the description and error codes of each API operation. For more information about the error codes, see Error codes.

  • Network exceptions that occur when you use Simple Log Service SDK to send requests. This type of exceptions include network disconnection and server response timeout.

  • Exceptions that are generated by Simple Log Service SDK and related to platforms and programming languages, such as, memory overflow.

Simple Log Service SDK in each programming language throws exceptions for handling.

  • Exceptions that are returned by Simple Log Service and network exceptions that occur during API request sending are handled by Simple Log Service SDK. Then, Simple Log Service SDK packages the exceptions in the LogException class and throws the exceptions.

  • Exceptions that are generated by Simple Log Service SDK and related to platforms and programming languages are not handled by Simple Log Service SDK. Simple Log Service SDK directly packages the exceptions in the Native Exception class of the related platform and programming language and throws the exceptions.

LogException

The LogException class is defined by Simple Log Service SDK and is used to handle the exceptions that are related to the logic of Simple Log Service. The LogException class inherits the base class that is used to handle exceptions in each programming language. The LogException class provides the following exception information:

  • Error code: indicates the exception type. The error code of an exception that is returned by Simple 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 in different programming languages, see API Reference.

  • Error message: indicates the message that describes an exception. The error message of an exception that is returned by Simple 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 messages in different programming languages, see API Reference.

  • Request ID: indicates the ID of the request from which an exception is raised. The request ID is generated by Simple Log Service. The request ID is valid only if Simple Log Service returns an error message. Otherwise, the ID is an empty string. If an exception is raised when you send an API request, you can record and provide the relevant request ID to the Simple Log Service team for troubleshooting.

Request failures and retries

When you use Simple Log Service SDK to access Simple Log Service, your access request may fail due to different exceptions such as temporary network disconnection, transmission latency, or slow server response. Simple Log Service directly throws the exceptions, and does not implement the retry logic. In this case, you must specify a custom handling logic, such as retrying a request or throwing an exception.

Examples

For example, network exceptions occur when you access a project that is named big-game in the China (Hangzhou) region. To handle the exceptions, you can use the following code in different programming languages to specify the number of retries:

  • Java

    // Other code. 
    // Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");   
    String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); 
    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);
            // Process the returned response. 
            break;
        }
        catch(LogException e)
        {
            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 code.

  • .NET (C#)

    // Other code. 
    // Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    String accessId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");     
    String accessKey = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");    
    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);
            // Process the returned response. 
            break;
        }
        catch(LogException e)
        {
            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 code.

  • PHP

    <?php
    // Other code. 
    $endpoint = 'cn-hangzhou.sls.aliyuncs.com';
    // Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    $accessId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'); 
    $accessKey = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'); 
    $maxRetries = 3;
    // Create a 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);
            // Process the returned response. 
            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 code.

  • Python

    from aliyun.log import LogClient, ListLogstoresRequest, LogException;
    
    # Other code. 
    from aliyun.log.logclient import xrange
    
    endpoint = 'cn-hangzhou.log.aliyuncs.com'
    # Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    accessId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
    accessKey =  os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
    maxRetries = 3
    # Create a client. 
    client = LogClient(endpoint, accessId, accessKey)
    project = 'big-game'
    lsRequest = ListLogstoresRequest(project)
    for i in xrange(maxRetries):
        try:
            res = client.ListLogstores(lsRequest)
            # Process the returned response. 
            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 ex:
            print("unrecoverable exception when listing logstores.")
            break
    # Other code.