Go中調用出錯,會統一返回介面error,該介面定義如下:
type error interface {
Error() string
}
其它的錯誤繼承於該介面。比如HTTP錯誤請求返回的錯誤如下:
//net.Error
type Error interface {
error
Timeout() bool // Is the error a timeout
Temporary() bool // Is the error temporary
}
使用OSS Go SDK時如果請求出錯,會有相應的error返回。HTTP請求、IO等錯誤會返回Go預定的錯誤。OSS Server處理請求出錯,返回如下的錯誤,該錯誤實現了error介面。
type ServiceError struct {
Code string // OSS返回給使用者的錯誤碼
Message string // OSS給出的詳細錯誤資訊
RequestId string // 用於唯一標識該次請求的UUID
HostId string // 用於標識訪問的OSS叢集
StatusCode int // HTTP狀態碼
}
如果OSS返回的HTTP狀態碼與預期不符返回如下錯誤,該錯誤也實現了error介面。
type UnexpectedStatusCodeError struct {
allowed []int // 預期OSS返回HTTP狀態碼
got int // OSS實際返回HTTP狀態碼
}
OSS的錯誤碼
OSS的錯誤碼列表如下:
錯誤碼 | 描述 | HTTP狀態碼 |
---|---|---|
AccessDenied | 拒絕訪問 | 403 |
BucketAlreadyExists | Bucket已經存在 | 409 |
BucketNotEmpty | Bucket不為空 | 409 |
EntityTooLarge | 實體過大 | 400 |
EntityTooSmall | 實體過小 | 400 |
FileGroupTooLarge | 檔案組過大 | 400 |
InvalidLinkName | Object Link與指向的Object同名 | 400 |
LinkPartNotExist | Object Link中指向的Object不存在 | 400 |
ObjectLinkTooLarge | Object Link中Object個數過多 | 400 |
FieldItemTooLong | Post請求中表單域過大 | 400 |
FilePartInterity | 檔案Part已改變 | 400 |
FilePartNotExist | 檔案Part不存在 | 400 |
FilePartStale | 檔案Part過時 | 400 |
IncorrectNumberOfFilesInPOSTRequest | Post請求中檔案個數非法 | 400 |
InvalidArgument | 參數格式錯誤 | 400 |
InvalidAccessKeyId | AccessKeyId不存在 | 403 |
InvalidBucketName | 無效的Bucket名字 | 400 |
InvalidDigest | 無效的摘要 | 400 |
InvalidEncryptionAlgorithmError | 指定的熵編碼密碼編譯演算法錯誤 | 400 |
InvalidObjectName | 無效的Object名字 | 400 |
InvalidPart | 無效的Part | 400 |
InvalidPartOrder | 無效的part順序 | 400 |
InvalidPolicyDocument | 無效的Policy文檔 | 400 |
InvalidTargetBucketForLogging | Logging操作中有無效的目標bucket | 400 |
InternalError | OSS內部發生錯誤 | 500 |
MalformedXML | XML格式非法 | 400 |
MalformedPOSTRequest | Post請求的body格式非法 | 400 |
MaxPOSTPreDataLengthExceededError | Post請求上傳檔案內容之外的body過大 | 400 |
MethodNotAllowed | 不支援的方法 | 405 |
MissingArgument | 缺少參數 | 411 |
MissingContentLength | 缺少內容長度 | 411 |
NoSuchBucket | Bucket不存在 | 404 |
NoSuchKey | 檔案不存在 | 404 |
NoSuchUpload | Multipart Upload ID不存在 | 404 |
NotImplemented | 無法處理的方法 | 501 |
PreconditionFailed | 預先處理錯誤 | 412 |
RequestTimeTooSkewed | 發起請求的時間和伺服器時間超出15分鐘 | 403 |
RequestTimeout | 請求逾時 | 400 |
RequestIsNotMultiPartContent | Post請求content-type非法 | 400 |
SignatureDoesNotMatch | 簽名錯誤 | 403 |
TooManyBuckets | 使用者的Bucket數目超過限制 | 400 |
InvalidEncryptionAlgorithmError | 指定的熵編碼密碼編譯演算法錯誤 | 400 |
提示:
- 上表的錯誤碼即OssServiceError.Code,HTTP狀態碼即OssServiceError.StatusCode。
- 如果試圖以OSS不支援的操作來訪問某個資源,返回405 Method Not Allowed錯誤。