All Products
Search
Document Center

Alibaba Cloud SDK:Handle an exception

Last Updated:Jul 29, 2024

This topic describes the exception types in Alibaba Cloud SDK V2.0 for PHP and how to handle an exception.

The exceptions that may occur when you use Alibaba Cloud SDK V2.0 for PHP can be classified into the following types:

  1. InvalidArgumentException: This type of exception occurs if you do not specify one or more required parameters or the specified value is invalid when you initialize the SDK client. You can view the exception details in the returned error message.

  2. TeaUnretryableException: This type of exception is caused by network issues. TeaUnretryableException is thrown if the number of retries reaches the upper limit. You can use getLastException to obtain the request information when the exception occurs.

  3. TeaError: This type of exception is caused by business errors. The following three parameters are provided to troubleshoot errors.

    1. code: the error code that is returned when the exception occurs.

    2. message: the error message that is returned when the exception occurs. The message contains the ID of the API request for which the exception is thrown.

    3. data: the detailed error information that is returned by the server for the exception.

Important

In this example, error messages are printed for reference only. In your actual business scenario, handle exceptions with caution and do not ignore exceptions in your project. We recommend that you take reasonable measures to handle exceptions, such as propagating exceptions in a proper manner, recording logs, and performing retries. This helps ensure the robustness and stability of the system.

<?php

require_once 'vendor/autoload.php';
use AlibabaCloud\SDK\Ecs\V20140526\Ecs;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Ecs\V20140526\Models\DescribeInstancesRequest;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;

class ProxyDemo
{


    public static function main()
    {
        try {
            $config = new Config([
                "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
                "endpoint" => "ecs.cn-beijing.aliyuncs.com", // <endpoint>
            ]);
            $client = new Ecs($config);

            $runtime = new RuntimeOptions([]);
            $describeInstancesRequest = new DescribeInstancesRequest([
                "regionId" => "cn-beijing"
            ]);
            $resp = $client->describeInstancesWithOptions($describeInstancesRequest, $runtime);
            var_dump($resp);
        } catch (\Exception $error) {
            if ($error instanceof TeaError) {
                // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
                print_r("message:" . $error->getMessage() . "\n");
                print_r("code:" . $error->getCode() . "\n");
                print_r($error->data);
            } elseif ($error instanceof TeaUnableRetryError) {
                // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
                print_r($error->getLastException());
            } else {
                // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
                print_r("message:" . $error->getMessage());
            }
        }
    }
}
ProxyDemo::main();