Created
October 12, 2021 03:44
-
-
Save quangthe/05d2aee9a951a959000689bea88a6421 to your computer and use it in GitHub Desktop.
Go error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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