All Products
Search
Document Center

Alibaba Cloud SDK:Handle an exception

Last Updated:Jul 08, 2024

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

Errors are thrown if exceptions occur when you use Alibaba Cloud SDK V2.0 for Node.js. The exceptions can be classified into the following types:

  • UnretryableError: This type of exception is caused by network issues. UnretryableError is thrown if the number of retries reaches the upper limit. You can use err.data.lastRequest to obtain the request information when the exception occurs.

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

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

    • 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.

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

    TypeScript example:

    import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
    import * as $OpenApi from '@alicloud/openapi-client';
    import * as $Util from '@alicloud/tea-util';
    
    export default class Client {
        static async main(): Promise<void> {
            const config = new $OpenApi.Config({
                // Obtain the AccessKey ID of the Resource Access Management (RAM) user from environment variables.
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // Obtain the AccessKey secret of the RAM user from environment variables.
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // The region ID.
                regionId: 'cn-beijing',
            });
            const client = new Ecs20140526(config);
            const request = new $Ecs20140526.DescribeRegionsRequest({
                instanceChargeType: "PrePaid",
            });
            // Create a RuntimeObject instance and configure runtime parameters. 
            const runtime = new $Util.RuntimeOptions();
            try {
                const resp = await client.describeRegionsWithOptions(request, runtime);
                console.log(resp.headers);
                console.log(resp.body);
            } catch (err) {
                // 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. 
                console.log(err.code);
                console.log(err.message);
                console.log(err.data);
            }
        }
    }
    

    JavaScript example:

    const { default: Ecs20140526, DescribeRegionsRequest } = require('@alicloud/ecs20140526');
    const { Config } = require('@alicloud/openapi-client');
    const { RuntimeOptions } = require('@alicloud/tea-util');
    async function main() {
        const config = new Config({
            // Obtain the AccessKey ID of the RAM user from environment variables.
            accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
            // Obtain the AccessKey secret of the RAM user from environment variables.
            accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
            // The region ID.
            regionId: 'cn-beijing',
        });
        const client = new Ecs20140526(config);
        const request = new DescribeRegionsRequest({
            instanceChargeType: "PrePaid",
        });
        // Create a RuntimeObject instance and configure runtime parameters. 
        const runtime = new RuntimeOptions();
        try {
            const resp = await client.describeRegionsWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        } catch (err) {
            // 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. 
            console.log(err.code);
            console.log(err.message);
            console.log(err.data);
        }
    }
    
    main();