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的错误码列表如下:
| 错误码 | 描述 | 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错误。