Configure the codes parameter to control which API response codes the SDK treats as successful, suppressing exceptions for expected non-200 responses.
By default, SDK V1.0 for Node.js throws an exception whenever an API call returns a response code that is not 200, OK, Success, or success. Use the codes parameter in your client configuration to extend this success set. Any code you add to codes is treated as successful, so the SDK does not throw an exception for that response.
How it works
When an API operation completes, the SDK checks the Code field in the response against your configured success set:
No exception thrown: The response
Codeis200,OK,Success,success, or any value you specified incodes.Exception thrown: The response
Codeis anything else.
Use codes when a specific response code is an expected outcome of your business logic — for example, an endpoint-not-supported response that you want to handle in your application code rather than as an SDK exception.
Configure the codes parameter
Pass an array of response codes to the codes option when creating your RPCClient instance. The following example adds InvalidOperation.NotSupportedEndpoint to the success set so that the SDK does not throw an exception when ECS returns that code.
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));
});
With this configuration, a DescribeInstances call that returns InvalidOperation.NotSupportedEndpoint resolves the Promise normally — your .then() handler receives the result. Any other unexpected code still throws an exception, so only the codes you explicitly list are suppressed.