SDK V1.0 for Node.js allows you to control the exception handling logic of API calls by configuring the codes parameter. When you call an API operation, the system automatically checks the Code parameter in the response. If the value of the Code parameter matches any value that you specify for the codes parameter in the client configurations, or is 200, OK, Success, or success, no exception is thrown. Otherwise, an exception is thrown.
For example, you specify InvalidOperation.NotSupportedEndpoint for the codes parameter. In this case, if the value of the Code parameter in the response of an API operation is InvalidOperation.NotSupportedEndpoint, the program will not see this as an exception and the operation will not be interrupted. The specified type of exception is silently handled. This helps improve the robustness of your code and controllability of the code logic. You can use this method to handle exceptions in a more precise manner and make your applications more flexible in handling various responses of API operations.
const RPCClient = require('@alicloud/pop-core').RPCClient;
const client = new RPCClient({
// 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,
endpoint: 'https://ecs.cn-beijing.aliyuncs.com',
apiVersion: '2014-05-26',
// Throw an exception if the response code is not 200, OK, Success, success, or a value of the codes parameter.
// The value of the codes parameter in this example is for reference only. Specify this parameter based on your business requirements.
codes: ['InvalidOperation.NotSupportedEndpoint'],
});
const params = {
RegionId: 'cn-beijing',
};
const action = 'DescribeInstances';
client.request(action, params).then((result) => {
console.log(JSON.stringify(result));
});