This article presents exception handling when using the TOS SDK.
TOS Server-Side Error
SDK Custom Errors
Client Error - TosClientError
TosClientError is caused by incorrect client input parameters, connection timeouts, or the execution of special client operations such as canceling resumable uploads. For example, when using CreateBucketV2 to create a bucket, an exception is thrown if the bucket name exceeds 63 characters in length.
Server-Side Error - TosServerError
Requests are sent to the TOS server via the TOS Go SDK interface. The response includes status codes that comply with HTTP standards and business error codes, which together indicate the status of the current request.
Error Handling Examples
The following code demonstrates exception handling when attempting to download a non-existent object. If a TosServerError exception occurs, the code prints the exception message, HTTP request status code, and request ID. If a TosClientError exception occurs, it prints the exception message and the underlying cause of the error.
"github.com/volcengine/ve-tos-golang-sdk/v2/tos"
func checkErr(err error) {
accessKey = os.Getenv("TOS_ACCESS_KEY")
secretKey = os.Getenv("TOS_SECRET_KEY")
endpoint = "your endpoint"
bucketName = "your bucket name"
objectKey = "your object key"
client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
get, err := client.GetObjectV2(context.Background(), &tos.GetObjectV2Input{
switch terr := err.(type) {
case *tos.TosServerError:
fmt.Printf("Message:%s StatusCode:%d RequestID:%s", terr.Message, terr.StatusCode, terr.RequestID)
if terr.StatusCode == http.StatusNotFound {
case *tos.TosClientError:
fmt.Printf("Message:%s Cause:%v", terr.Message, terr.Cause)
fmt.Println("GetObjectV2 Request ID:", get.RequestID)