s3api_errors.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. ErrAccessDenied
  26. ErrMethodNotAllowed
  27. ErrBucketNotEmpty
  28. ErrBucketAlreadyExists
  29. ErrBucketAlreadyOwnedByYou
  30. ErrNoSuchBucket
  31. ErrNoSuchKey
  32. ErrNoSuchUpload
  33. ErrInvalidBucketName
  34. ErrInvalidDigest
  35. ErrInvalidMaxKeys
  36. ErrInvalidMaxUploads
  37. ErrInvalidMaxParts
  38. ErrInvalidPartNumberMarker
  39. ErrInvalidPart
  40. ErrInternalError
  41. ErrInvalidCopyDest
  42. ErrInvalidCopySource
  43. ErrAuthHeaderEmpty
  44. ErrSignatureVersionNotSupported
  45. ErrMissingFields
  46. ErrMissingCredTag
  47. ErrCredMalformed
  48. ErrMalformedXML
  49. ErrMalformedDate
  50. ErrMalformedPresignedDate
  51. ErrMalformedCredentialDate
  52. ErrMissingSignHeadersTag
  53. ErrMissingSignTag
  54. ErrUnsignedHeaders
  55. ErrInvalidQueryParams
  56. ErrInvalidQuerySignatureAlgo
  57. ErrExpiredPresignRequest
  58. ErrMalformedExpires
  59. ErrNegativeExpires
  60. ErrMaximumExpires
  61. ErrSignatureDoesNotMatch
  62. ErrContentSHA256Mismatch
  63. ErrInvalidAccessKeyID
  64. ErrRequestNotReadyYet
  65. ErrMissingDateHeader
  66. ErrInvalidRequest
  67. ErrNotImplemented
  68. )
  69. // error code to APIError structure, these fields carry respective
  70. // descriptions for all the error responses.
  71. var errorCodeResponse = map[ErrorCode]APIError{
  72. ErrAccessDenied: {
  73. Code: "AccessDenied",
  74. Description: "Access Denied.",
  75. HTTPStatusCode: http.StatusForbidden,
  76. },
  77. ErrMethodNotAllowed: {
  78. Code: "MethodNotAllowed",
  79. Description: "The specified method is not allowed against this resource.",
  80. HTTPStatusCode: http.StatusMethodNotAllowed,
  81. },
  82. ErrBucketNotEmpty: {
  83. Code: "BucketNotEmpty",
  84. Description: "The bucket you tried to delete is not empty",
  85. HTTPStatusCode: http.StatusConflict,
  86. },
  87. ErrBucketAlreadyExists: {
  88. Code: "BucketAlreadyExists",
  89. 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.",
  90. HTTPStatusCode: http.StatusConflict,
  91. },
  92. ErrBucketAlreadyOwnedByYou: {
  93. Code: "BucketAlreadyOwnedByYou",
  94. Description: "Your previous request to create the named bucket succeeded and you already own it.",
  95. HTTPStatusCode: http.StatusConflict,
  96. },
  97. ErrInvalidBucketName: {
  98. Code: "InvalidBucketName",
  99. Description: "The specified bucket is not valid.",
  100. HTTPStatusCode: http.StatusBadRequest,
  101. },
  102. ErrInvalidDigest: {
  103. Code: "InvalidDigest",
  104. Description: "The Content-Md5 you specified is not valid.",
  105. HTTPStatusCode: http.StatusBadRequest,
  106. },
  107. ErrInvalidMaxUploads: {
  108. Code: "InvalidArgument",
  109. Description: "Argument max-uploads must be an integer between 0 and 2147483647",
  110. HTTPStatusCode: http.StatusBadRequest,
  111. },
  112. ErrInvalidMaxKeys: {
  113. Code: "InvalidArgument",
  114. Description: "Argument maxKeys must be an integer between 0 and 2147483647",
  115. HTTPStatusCode: http.StatusBadRequest,
  116. },
  117. ErrInvalidMaxParts: {
  118. Code: "InvalidArgument",
  119. Description: "Argument max-parts must be an integer between 0 and 2147483647",
  120. HTTPStatusCode: http.StatusBadRequest,
  121. },
  122. ErrInvalidPartNumberMarker: {
  123. Code: "InvalidArgument",
  124. Description: "Argument partNumberMarker must be an integer.",
  125. HTTPStatusCode: http.StatusBadRequest,
  126. },
  127. ErrNoSuchBucket: {
  128. Code: "NoSuchBucket",
  129. Description: "The specified bucket does not exist",
  130. HTTPStatusCode: http.StatusNotFound,
  131. },
  132. ErrNoSuchKey: {
  133. Code: "NoSuchKey",
  134. Description: "The specified key does not exist.",
  135. HTTPStatusCode: http.StatusNotFound,
  136. },
  137. ErrNoSuchUpload: {
  138. Code: "NoSuchUpload",
  139. Description: "The specified multipart upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed.",
  140. HTTPStatusCode: http.StatusNotFound,
  141. },
  142. ErrInternalError: {
  143. Code: "InternalError",
  144. Description: "We encountered an internal error, please try again.",
  145. HTTPStatusCode: http.StatusInternalServerError,
  146. },
  147. ErrInvalidPart: {
  148. Code: "InvalidPart",
  149. 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.",
  150. HTTPStatusCode: http.StatusBadRequest,
  151. },
  152. ErrInvalidCopyDest: {
  153. Code: "InvalidRequest",
  154. Description: "This copy request is illegal because it is trying to copy an object to itself without changing the object's metadata, storage class, website redirect location or encryption attributes.",
  155. HTTPStatusCode: http.StatusBadRequest,
  156. },
  157. ErrInvalidCopySource: {
  158. Code: "InvalidArgument",
  159. Description: "Copy Source must mention the source bucket and key: sourcebucket/sourcekey.",
  160. HTTPStatusCode: http.StatusBadRequest,
  161. },
  162. ErrMalformedXML: {
  163. Code: "MalformedXML",
  164. Description: "The XML you provided was not well-formed or did not validate against our published schema.",
  165. HTTPStatusCode: http.StatusBadRequest,
  166. },
  167. ErrAuthHeaderEmpty: {
  168. Code: "InvalidArgument",
  169. Description: "Authorization header is invalid -- one and only one ' ' (space) required.",
  170. HTTPStatusCode: http.StatusBadRequest,
  171. },
  172. ErrSignatureVersionNotSupported: {
  173. Code: "InvalidRequest",
  174. Description: "The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.",
  175. HTTPStatusCode: http.StatusBadRequest,
  176. },
  177. ErrMissingFields: {
  178. Code: "MissingFields",
  179. Description: "Missing fields in request.",
  180. HTTPStatusCode: http.StatusBadRequest,
  181. },
  182. ErrMissingCredTag: {
  183. Code: "InvalidRequest",
  184. Description: "Missing Credential field for this request.",
  185. HTTPStatusCode: http.StatusBadRequest,
  186. },
  187. ErrCredMalformed: {
  188. Code: "AuthorizationQueryParametersError",
  189. Description: "Error parsing the X-Amz-Credential parameter; the Credential is mal-formed; expecting \"<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request\".",
  190. HTTPStatusCode: http.StatusBadRequest,
  191. },
  192. ErrMalformedDate: {
  193. Code: "MalformedDate",
  194. Description: "Invalid date format header, expected to be in ISO8601, RFC1123 or RFC1123Z time format.",
  195. HTTPStatusCode: http.StatusBadRequest,
  196. },
  197. ErrMalformedPresignedDate: {
  198. Code: "AuthorizationQueryParametersError",
  199. Description: "X-Amz-Date must be in the ISO8601 Long Format \"yyyyMMdd'T'HHmmss'Z'\"",
  200. HTTPStatusCode: http.StatusBadRequest,
  201. },
  202. ErrMissingSignHeadersTag: {
  203. Code: "InvalidArgument",
  204. Description: "Signature header missing SignedHeaders field.",
  205. HTTPStatusCode: http.StatusBadRequest,
  206. },
  207. ErrMissingSignTag: {
  208. Code: "AccessDenied",
  209. Description: "Signature header missing Signature field.",
  210. HTTPStatusCode: http.StatusBadRequest,
  211. },
  212. ErrUnsignedHeaders: {
  213. Code: "AccessDenied",
  214. Description: "There were headers present in the request which were not signed",
  215. HTTPStatusCode: http.StatusBadRequest,
  216. },
  217. ErrInvalidQueryParams: {
  218. Code: "AuthorizationQueryParametersError",
  219. Description: "Query-string authentication version 4 requires the X-Amz-Algorithm, X-Amz-Credential, X-Amz-Signature, X-Amz-Date, X-Amz-SignedHeaders, and X-Amz-Expires parameters.",
  220. HTTPStatusCode: http.StatusBadRequest,
  221. },
  222. ErrInvalidQuerySignatureAlgo: {
  223. Code: "AuthorizationQueryParametersError",
  224. Description: "X-Amz-Algorithm only supports \"AWS4-HMAC-SHA256\".",
  225. HTTPStatusCode: http.StatusBadRequest,
  226. },
  227. ErrExpiredPresignRequest: {
  228. Code: "AccessDenied",
  229. Description: "Request has expired",
  230. HTTPStatusCode: http.StatusForbidden,
  231. },
  232. ErrMalformedExpires: {
  233. Code: "AuthorizationQueryParametersError",
  234. Description: "X-Amz-Expires should be a number",
  235. HTTPStatusCode: http.StatusBadRequest,
  236. },
  237. ErrNegativeExpires: {
  238. Code: "AuthorizationQueryParametersError",
  239. Description: "X-Amz-Expires must be non-negative",
  240. HTTPStatusCode: http.StatusBadRequest,
  241. },
  242. ErrMaximumExpires: {
  243. Code: "AuthorizationQueryParametersError",
  244. Description: "X-Amz-Expires must be less than a week (in seconds); that is, the given X-Amz-Expires must be less than 604800 seconds",
  245. HTTPStatusCode: http.StatusBadRequest,
  246. },
  247. ErrInvalidAccessKeyID: {
  248. Code: "InvalidAccessKeyId",
  249. Description: "The access key ID you provided does not exist in our records.",
  250. HTTPStatusCode: http.StatusForbidden,
  251. },
  252. ErrRequestNotReadyYet: {
  253. Code: "AccessDenied",
  254. Description: "Request is not valid yet",
  255. HTTPStatusCode: http.StatusForbidden,
  256. },
  257. ErrSignatureDoesNotMatch: {
  258. Code: "SignatureDoesNotMatch",
  259. Description: "The request signature we calculated does not match the signature you provided. Check your key and signing method.",
  260. HTTPStatusCode: http.StatusForbidden,
  261. },
  262. ErrContentSHA256Mismatch: {
  263. Code: "XAmzContentSHA256Mismatch",
  264. Description: "The provided 'x-amz-content-sha256' header does not match what was computed.",
  265. HTTPStatusCode: http.StatusBadRequest,
  266. },
  267. ErrMissingDateHeader: {
  268. Code: "AccessDenied",
  269. Description: "AWS authentication requires a valid Date or x-amz-date header",
  270. HTTPStatusCode: http.StatusBadRequest,
  271. },
  272. ErrInvalidRequest: {
  273. Code: "InvalidRequest",
  274. Description: "Invalid Request",
  275. HTTPStatusCode: http.StatusBadRequest,
  276. },
  277. ErrNotImplemented: {
  278. Code: "NotImplemented",
  279. Description: "A header you provided implies functionality that is not implemented",
  280. HTTPStatusCode: http.StatusNotImplemented,
  281. },
  282. }
  283. // getAPIError provides API Error for input API error code.
  284. func getAPIError(code ErrorCode) APIError {
  285. return errorCodeResponse[code]
  286. }