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.

296 lines
9.9 KiB

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