This topic describes how to troubleshoot errors when you use Object Storage Service (OSS) SDK for Go.
Client errors
If errors occur when you use OSS SDK for Go, a unified error interface that can be used to troubleshoot the errors is returned. The following sample code provides an example of the error interface:
type error interface {
Error() string
}
You can troubleshoot other specific errors based on this interface. The following code provides an example of the error interface that is used to return an HTTP request error:
//net.Error
type Error interface {
error
Timeout() bool // Is the error a timeout
Temporary() bool // Is the error temporary
}
Server errors
If request errors occur when you use OSS SDK for Go, the corresponding error messages are returned. Errors that are defined in OSS SDK for Go are returned for HTTP request errors and I/O request errors. If an error occurs when the OSS server processes a request, the following sample code that is an implementation of the error interface is returned:
type ServiceError struct {
Code string // The error code returned by OSS.
Message string // The error details provided by OSS.
RequestId string // The UUID of the request.
HostId string // The host ID of the accessed OSS cluster.
StatusCode int // The HTTP status code.
}
If the HTTP status code returned by OSS is not as expected, the following sample code that is an implementation of the error interface is included in the returned response:
type UnexpectedStatusCodeError struct {
allowed []int // The expected HTTP status code returned by OSS.
got int // The actual HTTP status code returned by OSS.
}
Examples
The following code provides an example on how to handle errors:
package main
import (
"fmt"
"os"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// The function for error handling.
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
/// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the name of the bucket. Example: examplebucket.
bucketName := "examplebucket"
// Specify the name of the object. Example: exampleobject.txt.
objectName := "exampleobject.txt"
bucket, err := client.Bucket(bucketName)
if err != nil {
HandleError(err)
}
// Upload a string.
err = bucket.PutObject(objectName, strings.NewReader("Hello OSS"))
if err != nil {
HandleError(err)
}
}
OSS error codes
The following table describes the OSS error codes.
Error code | Description | HTTP status code |
AccessDenied | Access is denied. | 403 |
BucketAlreadyExists | The bucket already exists. | 409 |
BucketNotEmpty | The bucket is not empty. | 409 |
EntityTooLarge | The entity size exceeds the maximum limit. | 400 |
EntityTooSmall | The entity size is below the minimum limit. | 400 |
FileGroupTooLarge | The file group size exceeds the maximum limit. | 400 |
InvalidLinkName | The name of the symbolic link is the same as the object to which the symbolic link points. | 400 |
LinkPartNotExist | The object to which the symbolic link points does not exist. | 400 |
ObjectLinkTooLarge | The symbolic link reaches the maximum number of objects that it can point to. | 400 |
FieldItemTooLong | The total length of all form fields in the POST request exceeds the limit. | 400 |
FilePartInterity | The part changed. | 400 |
FilePartNotExist | The part does not exist. | 400 |
FilePartStale | The part has expired. | 400 |
IncorrectNumberOfFilesInPOSTRequest | The number of files in the POST request is incorrect. | 400 |
InvalidArgument | The format of the parameter is invalid. | 400 |
InvalidAccessKeyId | The AccessKey ID does not exist. | 403 |
InvalidBucketName | The bucket name is invalid. | 400 |
InvalidDigest | The digest is invalid. | 400 |
InvalidEncryptionAlgorithmError | The encryption algorithm is invalid. | 400 |
InvalidObjectName | The object name is invalid. | 400 |
InvalidPart | The part is invalid. | 400 |
InvalidPartOrder | The part sequence is invalid. | 400 |
InvalidPolicyDocument | The policy document is invalid. | 400 |
InvalidTargetBucketForLogging | An invalid destination bucket is found during logging. | 400 |
InternalError | An internal OSS error occurred. | 500 |
MalformedXML | The XML format is invalid. | 400 |
MalformedPOSTRequest | The body of the POST request is invalid. | 400 |
MaxPOSTPreDataLengthExceededError | The message body of the PostObject request, excluding the object content to upload, exceeds the limit. | 400 |
MethodNotAllowed | The method is not supported. | 405 |
MissingArgument | One or more required parameters are not specified. | 411 |
MissingContentLength | The content length is missing. | 411 |
NoSuchBucket | The specified bucket does not exist. | 404 |
NoSuchKey | The object does not exist. | 404 |
NoSuchUpload | The multipart upload ID does not exist. | 404 |
NotImplemented | The method cannot be implemented. | 501 |
PreconditionFailed | An error occurred during preprocessing. | 412 |
RequestTimeTooSkewed | The time deviation of the OSS client and OSS server exceeds 15 minutes. | 403 |
RequestTimeout | The request timed out. | 400 |
RequestIsNotMultiPartContent | The content-type value specified in the POST request is invalid. | 400 |
SignatureDoesNotMatch | A signature error occurred. | 403 |
TooManyBuckets | The number of buckets of the user exceeds the limit. | 400 |
InvalidEncryptionAlgorithmError | The encryption algorithm is invalid. | 400 |
In the preceding table, the error code is specified by the OssServiceError.Code parameter. The HTTP status code is specified by the OssServiceError.StatusCode parameter.