10 // ServiceError contains fields of the error response from Oss Service REST API.
11 type ServiceError struct {
12 XMLName xml.Name `xml:"Error"`
13 Code string `xml:"Code"` // The error code returned from OSS to the caller
14 Message string `xml:"Message"` // The detail error message from OSS
15 RequestID string `xml:"RequestId"` // The UUID used to uniquely identify the request
16 HostID string `xml:"HostId"` // The OSS server cluster's Id
17 Endpoint string `xml:"Endpoint"`
18 RawMessage string // The raw messages from OSS
19 StatusCode int // HTTP status code
22 // Error implements interface error
23 func (e ServiceError) Error() string {
25 return fmt.Sprintf("oss: service returned error: StatusCode=%d, ErrorCode=%s, ErrorMessage=\"%s\", RequestId=%s",
26 e.StatusCode, e.Code, e.Message, e.RequestID)
28 return fmt.Sprintf("oss: service returned error: StatusCode=%d, ErrorCode=%s, ErrorMessage=\"%s\", RequestId=%s, Endpoint=%s",
29 e.StatusCode, e.Code, e.Message, e.RequestID, e.Endpoint)
32 // UnexpectedStatusCodeError is returned when a storage service responds with neither an error
33 // nor with an HTTP status code indicating success.
34 type UnexpectedStatusCodeError struct {
35 allowed []int // The expected HTTP stats code returned from OSS
36 got int // The actual HTTP status code from OSS
39 // Error implements interface error
40 func (e UnexpectedStatusCodeError) Error() string {
41 s := func(i int) string { return fmt.Sprintf("%d %s", i, http.StatusText(i)) }
44 expected := []string{}
45 for _, v := range e.allowed {
46 expected = append(expected, s(v))
48 return fmt.Sprintf("oss: status code from service response is %s; was expecting %s",
49 got, strings.Join(expected, " or "))
52 // Got is the actual status code returned by oss.
53 func (e UnexpectedStatusCodeError) Got() int {
57 // CheckRespCode returns UnexpectedStatusError if the given response code is not
58 // one of the allowed status codes; otherwise nil.
59 func CheckRespCode(respCode int, allowed []int) error {
60 for _, v := range allowed {
65 return UnexpectedStatusCodeError{allowed, respCode}
68 // CRCCheckError is returned when crc check is inconsistent between client and server
69 type CRCCheckError struct {
70 clientCRC uint64 // Calculated CRC64 in client
71 serverCRC uint64 // Calculated CRC64 in server
72 operation string // Upload operations such as PutObject/AppendObject/UploadPart, etc
73 requestID string // The request id of this operation
76 // Error implements interface error
77 func (e CRCCheckError) Error() string {
78 return fmt.Sprintf("oss: the crc of %s is inconsistent, client %d but server %d; request id is %s",
79 e.operation, e.clientCRC, e.serverCRC, e.requestID)
82 func CheckDownloadCRC(clientCRC, serverCRC uint64) error {
83 if clientCRC == serverCRC {
86 return CRCCheckError{clientCRC, serverCRC, "DownloadFile", ""}
89 func CheckCRC(resp *Response, operation string) error {
90 if resp.Headers.Get(HTTPHeaderOssCRC64) == "" || resp.ClientCRC == resp.ServerCRC {
93 return CRCCheckError{resp.ClientCRC, resp.ServerCRC, operation, resp.Headers.Get(HTTPHeaderOssRequestID)}