All Products
Search
Document Center

Alibaba Cloud SDK:Handle exceptions

Last Updated:Jun 16, 2026

Alibaba Cloud SDK V2.0 for Node.js throws specific error types for network and business failures. Learn the exception types and how to handle them.

Alibaba Cloud SDK V2.0 for Node.js throws the following exception types:

  • UnretryableError: Caused by network issues. Thrown after the maximum number of retries is reached. Use err.data.lastRequest to obtain the request information when the error occurs.

  • ResponseError: Caused by business errors. Use the following parameters to troubleshoot:

    • code: the error code returned for the exception.

    • message: the error message returned for the exception, including the request ID of the API call.

    • data: the detailed error information returned by the server.

Sample code:

Important

In this example, error messages are printed for reference only. In production, do not ignore exceptions. Use proper exception propagation, logging, and retries to ensure system robustness and stability.

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 an environment variable.
        accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
        // Obtain the AccessKey secret of the RAM user from an environment variable.
        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 RuntimeOptions 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 based on your actual business scenario and do not ignore exceptions in your project. The error message displayed in this example is for reference only. 
        console.log(err.code);
        console.log(err.message);
        console.log(err.data);
    }
}

main();
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 RAM user from an environment variable.
            accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
            // Obtain the AccessKey secret of the RAM user from an environment variable.
            accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
            // The region that you want to access.
            regionId: 'cn-beijing',
        });
        const client = new Ecs20140526(config);
        const request = new $Ecs20140526.DescribeRegionsRequest({
            instanceChargeType: "PrePaid",
        });
        // Create a RuntimeOptions 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 based on your actual business scenario and do not ignore exceptions in your project. The error message displayed in this example is for reference only. 
            console.log(err.code);
            console.log(err.message);
            console.log(err.data);
        }
    }
}