error.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package oss
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. )
  8. // ServiceError contains fields of the error response from Oss Service REST API.
  9. type ServiceError struct {
  10. XMLName xml.Name `xml:"Error"`
  11. Code string `xml:"Code"` // The error code returned from OSS to the caller
  12. Message string `xml:"Message"` // The detail error message from OSS
  13. RequestID string `xml:"RequestId"` // The UUID used to uniquely identify the request
  14. HostID string `xml:"HostId"` // The OSS server cluster's Id
  15. RawMessage string // The raw messages from OSS
  16. StatusCode int // HTTP status code
  17. }
  18. // Error implements interface error
  19. func (e ServiceError) Error() string {
  20. return fmt.Sprintf("oss: service returned error: StatusCode=%d, ErrorCode=%s, ErrorMessage=%s, RequestId=%s",
  21. e.StatusCode, e.Code, e.Message, e.RequestID)
  22. }
  23. // UnexpectedStatusCodeError is returned when a storage service responds with neither an error
  24. // nor with an HTTP status code indicating success.
  25. type UnexpectedStatusCodeError struct {
  26. allowed []int // The expected HTTP stats code returned from OSS
  27. got int // The actual HTTP status code from OSS
  28. }
  29. // Error implements interface error
  30. func (e UnexpectedStatusCodeError) Error() string {
  31. s := func(i int) string { return fmt.Sprintf("%d %s", i, http.StatusText(i)) }
  32. got := s(e.got)
  33. expected := []string{}
  34. for _, v := range e.allowed {
  35. expected = append(expected, s(v))
  36. }
  37. return fmt.Sprintf("oss: status code from service response is %s; was expecting %s",
  38. got, strings.Join(expected, " or "))
  39. }
  40. // Got is the actual status code returned by oss.
  41. func (e UnexpectedStatusCodeError) Got() int {
  42. return e.got
  43. }
  44. // checkRespCode returns UnexpectedStatusError if the given response code is not
  45. // one of the allowed status codes; otherwise nil.
  46. func checkRespCode(respCode int, allowed []int) error {
  47. for _, v := range allowed {
  48. if respCode == v {
  49. return nil
  50. }
  51. }
  52. return UnexpectedStatusCodeError{allowed, respCode}
  53. }
  54. // CRCCheckError is returned when crc check is inconsistent between client and server
  55. type CRCCheckError struct {
  56. clientCRC uint64 // Calculated CRC64 in client
  57. serverCRC uint64 // Calculated CRC64 in server
  58. operation string // Upload operations such as PutObject/AppendObject/UploadPart, etc
  59. requestID string // The request id of this operation
  60. }
  61. // Error implements interface error
  62. func (e CRCCheckError) Error() string {
  63. return fmt.Sprintf("oss: the crc of %s is inconsistent, client %d but server %d; request id is %s",
  64. e.operation, e.clientCRC, e.serverCRC, e.requestID)
  65. }
  66. func checkDownloadCRC(clientCRC, serverCRC uint64) error {
  67. if clientCRC == serverCRC {
  68. return nil
  69. }
  70. return CRCCheckError{clientCRC, serverCRC, "DownloadFile", ""}
  71. }
  72. func checkCRC(resp *Response, operation string) error {
  73. if resp.Headers.Get(HTTPHeaderOssCRC64) == "" || resp.ClientCRC == resp.ServerCRC {
  74. return nil
  75. }
  76. return CRCCheckError{resp.ClientCRC, resp.ServerCRC, operation, resp.Headers.Get(HTTPHeaderOssRequestID)}
  77. }