Skip to content

Instantly share code, notes, and snippets.

@quangthe
Created October 12, 2021 03:44
Show Gist options
  • Save quangthe/05d2aee9a951a959000689bea88a6421 to your computer and use it in GitHub Desktop.
Save quangthe/05d2aee9a951a959000689bea88a6421 to your computer and use it in GitHub Desktop.
Go error handling
import (
"fmt"
"errors"
tce "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
)
// Way 1
if _, ok := err.(*tce.TencentCloudSDKError); ok {
return fmt.Errorf("An API error has returned: %s", err)
}
// Way 2
if err != nil {
// declare pointer
var apiErr *tce.TencentCloudSDKError
// try to cast error type
if errors.As(err, &apiErr) {
// handle API error
return fmt.Errorf("An API error has returned: %s", apiErr.Error())
}
// other errors
return fmt.Errorf("An error has returned: %s", err.Error())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment