Alibaba Cloud Darabonba SDK for Go raises two types of exceptions: generic errors and business-related SDKError exceptions.
When you call API operations by using Alibaba Cloud Darabonba SDK for Go, two types of exceptions may be thrown.
The SDK raises the following exception types:
-
error: a non-business error, such as a verification failure caused by a modified SDK source file or a parsing failure.
-
SDKError: a business error returned by an API operation. This exception includes the following parameters:
-
Code: the error code returned by the API operation.
-
Message: the error message, which includes the request ID of the failed API call.
-
Data: the detailed error information returned by the server.
-
Make sure that you use the latest version of the SDK.
github.com/alibabacloud-go/darabonba-openapi/v2
github.com/alibabacloud-go/tea-utils/v2
github.com/alibabacloud-go/tea
github.com/alibabacloud-go/vpc-20160428/v6
package main
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
ecs20160428 "github.com/alibabacloud-go/ecs-20140526/v4/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"os"
)
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
func createClient() (_result *ecs20160428.Client, _err error) {
config := &openapi.Config{
// Required. Make sure that the following environment variable is set in the code runtime environment: ALIBABA_CLOUD_ACCESS_KEY_ID.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the following environment variable is set in the code runtime environment: ALIBABA_CLOUD_ACCESS_KEY_SECRET.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
config.RegionId = tea.String("<regionId>")
_result = &ecs20160428.Client{}
_result, _err = ecs20160428.NewClient(config)
return _result, _err
}
func main() {
client, _err := createClient()
if _err != nil {
fmt.Printf("Error creating client: %v\n", _err)
return
}
describeRegionsRequest := &ecs20160428.DescribeRegionsRequest{}
resp, tryErr := func() (_result *ecs20160428.DescribeRegionsResponse, _e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
_result, _err = client.DescribeRegionsWithOptions(describeRegionsRequest, &util.RuntimeOptions{})
if _err != nil {
return nil, _err
}
return _result, nil
}()
if tryErr != nil {
if sdkError, ok := tryErr.(*tea.SDKError); ok { // Determine whether the type of the tryErr error is *tea.SDKError based on the error description.
fmt.Println(tea.StringValue(sdkError.Message))
fmt.Println(tea.StringValue(sdkError.Code))
fmt.Println(tea.StringValue(sdkError.Data))
} else {
fmt.Println(tea.String(tryErr.Error()))
}
} else {
fmt.Println(resp.Body)
}
}