1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package oss
- import (
- "encoding/xml"
- "fmt"
- "net/http"
- "strings"
- )
- type ServiceError struct {
- XMLName xml.Name `xml:"Error"`
- Code string `xml:"Code"`
- Message string `xml:"Message"`
- RequestID string `xml:"RequestId"`
- HostID string `xml:"HostId"`
- RawMessage string
- StatusCode int
- }
- func (e ServiceError) Error() string {
- return fmt.Sprintf("oss: service returned error: StatusCode=%d, ErrorCode=%s, ErrorMessage=%s, RequestId=%s",
- e.StatusCode, e.Code, e.Message, e.RequestID)
- }
- type UnexpectedStatusCodeError struct {
- allowed []int
- got int
- }
- func (e UnexpectedStatusCodeError) Error() string {
- s := func(i int) string { return fmt.Sprintf("%d %s", i, http.StatusText(i)) }
- got := s(e.got)
- expected := []string{}
- for _, v := range e.allowed {
- expected = append(expected, s(v))
- }
- return fmt.Sprintf("oss: status code from service response is %s; was expecting %s",
- got, strings.Join(expected, " or "))
- }
- func (e UnexpectedStatusCodeError) Got() int {
- return e.got
- }
- func checkRespCode(respCode int, allowed []int) error {
- for _, v := range allowed {
- if respCode == v {
- return nil
- }
- }
- return UnexpectedStatusCodeError{allowed, respCode}
- }
- type CRCCheckError struct {
- clientCRC uint64
- serverCRC uint64
- operation string
- requestID string
- }
- func (e CRCCheckError) Error() string {
- return fmt.Sprintf("oss: the crc of %s is inconsistent, client %d but server %d; request id is %s",
- e.operation, e.clientCRC, e.serverCRC, e.requestID)
- }
- func checkDownloadCRC(clientCRC, serverCRC uint64) error {
- if clientCRC == serverCRC {
- return nil
- }
- return CRCCheckError{clientCRC, serverCRC, "DownloadFile", ""}
- }
- func checkCRC(resp *Response, operation string) error {
- if resp.Headers.Get(HTTPHeaderOssCRC64) == "" || resp.ClientCRC == resp.ServerCRC {
- return nil
- }
- return CRCCheckError{resp.ClientCRC, resp.ServerCRC, operation, resp.Headers.Get(HTTPHeaderOssRequestID)}
- }
|