You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.7 KiB

7 years ago
7 years ago
7 years ago
  1. package s3api
  2. import (
  3. "encoding/xml"
  4. "net/http"
  5. )
  6. // APIError structure
  7. type APIError struct {
  8. Code string
  9. Description string
  10. HTTPStatusCode int
  11. }
  12. // RESTErrorResponse - error response format
  13. type RESTErrorResponse struct {
  14. XMLName xml.Name `xml:"Error" json:"-"`
  15. Code string `xml:"Code" json:"Code"`
  16. Message string `xml:"Message" json:"Message"`
  17. Resource string `xml:"Resource" json:"Resource"`
  18. RequestID string `xml:"RequestId" json:"RequestId"`
  19. }
  20. // ErrorCode type of error status.
  21. type ErrorCode int
  22. // Error codes, see full list at http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
  23. const (
  24. ErrNone ErrorCode = iota
  25. ErrMethodNotAllowed
  26. ErrBucketNotEmpty
  27. ErrBucketAlreadyExists
  28. ErrBucketAlreadyOwnedByYou
  29. ErrNoSuchBucket
  30. ErrNoSuchUpload
  31. ErrInvalidBucketName
  32. ErrInvalidDigest
  33. ErrInvalidMaxKeys
  34. ErrInvalidMaxUploads
  35. ErrInvalidMaxParts
  36. ErrInvalidPartNumberMarker
  37. ErrInvalidPart
  38. ErrInvalidPartOrder
  39. ErrInternalError
  40. ErrMalformedXML
  41. ErrNotImplemented
  42. )
  43. // error code to APIError structure, these fields carry respective
  44. // descriptions for all the error responses.
  45. var errorCodeResponse = map[ErrorCode]APIError{
  46. ErrMethodNotAllowed: {
  47. Code: "MethodNotAllowed",
  48. Description: "The specified method is not allowed against this resource.",
  49. HTTPStatusCode: http.StatusMethodNotAllowed,
  50. },
  51. ErrBucketNotEmpty: {
  52. Code: "BucketNotEmpty",
  53. Description: "The bucket you tried to delete is not empty",
  54. HTTPStatusCode: http.StatusConflict,
  55. },
  56. ErrBucketAlreadyExists: {
  57. Code: "BucketAlreadyExists",
  58. Description: "The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.",
  59. HTTPStatusCode: http.StatusConflict,
  60. },
  61. ErrBucketAlreadyOwnedByYou: {
  62. Code: "BucketAlreadyOwnedByYou",
  63. Description: "Your previous request to create the named bucket succeeded and you already own it.",
  64. HTTPStatusCode: http.StatusConflict,
  65. },
  66. ErrInvalidBucketName: {
  67. Code: "InvalidBucketName",
  68. Description: "The specified bucket is not valid.",
  69. HTTPStatusCode: http.StatusBadRequest,
  70. },
  71. ErrInvalidDigest: {
  72. Code: "InvalidDigest",
  73. Description: "The Content-Md5 you specified is not valid.",
  74. HTTPStatusCode: http.StatusBadRequest,
  75. },
  76. ErrInvalidMaxUploads: {
  77. Code: "InvalidArgument",
  78. Description: "Argument max-uploads must be an integer between 0 and 2147483647",
  79. HTTPStatusCode: http.StatusBadRequest,
  80. },
  81. ErrInvalidMaxKeys: {
  82. Code: "InvalidArgument",
  83. Description: "Argument maxKeys must be an integer between 0 and 2147483647",
  84. HTTPStatusCode: http.StatusBadRequest,
  85. },
  86. ErrInvalidMaxParts: {
  87. Code: "InvalidArgument",
  88. Description: "Argument max-parts must be an integer between 0 and 2147483647",
  89. HTTPStatusCode: http.StatusBadRequest,
  90. },
  91. ErrInvalidPartNumberMarker: {
  92. Code: "InvalidArgument",
  93. Description: "Argument partNumberMarker must be an integer.",
  94. HTTPStatusCode: http.StatusBadRequest,
  95. },
  96. ErrNoSuchBucket: {
  97. Code: "NoSuchBucket",
  98. Description: "The specified bucket does not exist",
  99. HTTPStatusCode: http.StatusNotFound,
  100. },
  101. ErrNoSuchUpload: {
  102. Code: "NoSuchUpload",
  103. Description: "The specified multipart upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed.",
  104. HTTPStatusCode: http.StatusNotFound,
  105. },
  106. ErrInternalError: {
  107. Code: "InternalError",
  108. Description: "We encountered an internal error, please try again.",
  109. HTTPStatusCode: http.StatusInternalServerError,
  110. },
  111. ErrMalformedXML: {
  112. Code: "MalformedXML",
  113. Description: "The XML you provided was not well-formed or did not validate against our published schema.",
  114. HTTPStatusCode: http.StatusBadRequest,
  115. },
  116. ErrInvalidPart: {
  117. Code: "InvalidPart",
  118. Description: "One or more of the specified parts could not be found. The part may not have been uploaded, or the specified entity tag may not match the part's entity tag.",
  119. HTTPStatusCode: http.StatusBadRequest,
  120. },
  121. ErrInvalidPartOrder: {
  122. Code: "InvalidPartOrder",
  123. Description: "The list of parts was not in ascending order. The parts list must be specified in order by part number.",
  124. HTTPStatusCode: http.StatusBadRequest,
  125. },
  126. ErrNotImplemented: {
  127. Code: "NotImplemented",
  128. Description: "A header you provided implies functionality that is not implemented",
  129. HTTPStatusCode: http.StatusNotImplemented,
  130. },
  131. }
  132. // getAPIError provides API Error for input API error code.
  133. func getAPIError(code ErrorCode) APIError {
  134. return errorCodeResponse[code]
  135. }