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.

289 lines
9.7 KiB

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