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.

346 lines
12 KiB

4 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
4 years ago
  1. package s3err
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "net/http"
  6. )
  7. // APIError structure
  8. type APIError struct {
  9. Code string
  10. Description string
  11. HTTPStatusCode int
  12. }
  13. // RESTErrorResponse - error response format
  14. type RESTErrorResponse struct {
  15. XMLName xml.Name `xml:"Error" json:"-"`
  16. Code string `xml:"Code" json:"Code"`
  17. Message string `xml:"Message" json:"Message"`
  18. Resource string `xml:"Resource" json:"Resource"`
  19. RequestID string `xml:"RequestId" json:"RequestId"`
  20. // Underlying HTTP status code for the returned error
  21. StatusCode int `xml:"-" json:"-"`
  22. }
  23. // Error - Returns S3 error string.
  24. func (e RESTErrorResponse) Error() string {
  25. if e.Message == "" {
  26. msg, ok := s3ErrorResponseMap[e.Code]
  27. if !ok {
  28. msg = fmt.Sprintf("Error response code %s.", e.Code)
  29. }
  30. return msg
  31. }
  32. return e.Message
  33. }
  34. // ErrorCode type of error status.
  35. type ErrorCode int
  36. // Error codes, see full list at http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
  37. const (
  38. ErrNone ErrorCode = iota
  39. ErrAccessDenied
  40. ErrMethodNotAllowed
  41. ErrBucketNotEmpty
  42. ErrBucketAlreadyExists
  43. ErrBucketAlreadyOwnedByYou
  44. ErrNoSuchBucket
  45. ErrNoSuchKey
  46. ErrNoSuchUpload
  47. ErrInvalidBucketName
  48. ErrInvalidDigest
  49. ErrInvalidMaxKeys
  50. ErrInvalidMaxUploads
  51. ErrInvalidMaxParts
  52. ErrInvalidPartNumberMarker
  53. ErrInvalidPart
  54. ErrInternalError
  55. ErrInvalidCopyDest
  56. ErrInvalidCopySource
  57. ErrAuthHeaderEmpty
  58. ErrSignatureVersionNotSupported
  59. ErrMalformedPOSTRequest
  60. ErrPOSTFileRequired
  61. ErrPostPolicyConditionInvalidFormat
  62. ErrEntityTooSmall
  63. ErrEntityTooLarge
  64. ErrMissingFields
  65. ErrMissingCredTag
  66. ErrCredMalformed
  67. ErrMalformedXML
  68. ErrMalformedDate
  69. ErrMalformedPresignedDate
  70. ErrMalformedCredentialDate
  71. ErrMissingSignHeadersTag
  72. ErrMissingSignTag
  73. ErrUnsignedHeaders
  74. ErrInvalidQueryParams
  75. ErrInvalidQuerySignatureAlgo
  76. ErrExpiredPresignRequest
  77. ErrMalformedExpires
  78. ErrNegativeExpires
  79. ErrMaximumExpires
  80. ErrSignatureDoesNotMatch
  81. ErrContentSHA256Mismatch
  82. ErrInvalidAccessKeyID
  83. ErrRequestNotReadyYet
  84. ErrMissingDateHeader
  85. ErrInvalidRequest
  86. ErrNotImplemented
  87. )
  88. // error code to APIError structure, these fields carry respective
  89. // descriptions for all the error responses.
  90. var errorCodeResponse = map[ErrorCode]APIError{
  91. ErrAccessDenied: {
  92. Code: "AccessDenied",
  93. Description: "Access Denied.",
  94. HTTPStatusCode: http.StatusForbidden,
  95. },
  96. ErrMethodNotAllowed: {
  97. Code: "MethodNotAllowed",
  98. Description: "The specified method is not allowed against this resource.",
  99. HTTPStatusCode: http.StatusMethodNotAllowed,
  100. },
  101. ErrBucketNotEmpty: {
  102. Code: "BucketNotEmpty",
  103. Description: "The bucket you tried to delete is not empty",
  104. HTTPStatusCode: http.StatusConflict,
  105. },
  106. ErrBucketAlreadyExists: {
  107. Code: "BucketAlreadyExists",
  108. 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.",
  109. HTTPStatusCode: http.StatusConflict,
  110. },
  111. ErrBucketAlreadyOwnedByYou: {
  112. Code: "BucketAlreadyOwnedByYou",
  113. Description: "Your previous request to create the named bucket succeeded and you already own it.",
  114. HTTPStatusCode: http.StatusConflict,
  115. },
  116. ErrInvalidBucketName: {
  117. Code: "InvalidBucketName",
  118. Description: "The specified bucket is not valid.",
  119. HTTPStatusCode: http.StatusBadRequest,
  120. },
  121. ErrInvalidDigest: {
  122. Code: "InvalidDigest",
  123. Description: "The Content-Md5 you specified is not valid.",
  124. HTTPStatusCode: http.StatusBadRequest,
  125. },
  126. ErrInvalidMaxUploads: {
  127. Code: "InvalidArgument",
  128. Description: "Argument max-uploads must be an integer between 0 and 2147483647",
  129. HTTPStatusCode: http.StatusBadRequest,
  130. },
  131. ErrInvalidMaxKeys: {
  132. Code: "InvalidArgument",
  133. Description: "Argument maxKeys must be an integer between 0 and 2147483647",
  134. HTTPStatusCode: http.StatusBadRequest,
  135. },
  136. ErrInvalidMaxParts: {
  137. Code: "InvalidArgument",
  138. Description: "Argument max-parts must be an integer between 0 and 2147483647",
  139. HTTPStatusCode: http.StatusBadRequest,
  140. },
  141. ErrInvalidPartNumberMarker: {
  142. Code: "InvalidArgument",
  143. Description: "Argument partNumberMarker must be an integer.",
  144. HTTPStatusCode: http.StatusBadRequest,
  145. },
  146. ErrNoSuchBucket: {
  147. Code: "NoSuchBucket",
  148. Description: "The specified bucket does not exist",
  149. HTTPStatusCode: http.StatusNotFound,
  150. },
  151. ErrNoSuchKey: {
  152. Code: "NoSuchKey",
  153. Description: "The specified key does not exist.",
  154. HTTPStatusCode: http.StatusNotFound,
  155. },
  156. ErrNoSuchUpload: {
  157. Code: "NoSuchUpload",
  158. Description: "The specified multipart upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed.",
  159. HTTPStatusCode: http.StatusNotFound,
  160. },
  161. ErrInternalError: {
  162. Code: "InternalError",
  163. Description: "We encountered an internal error, please try again.",
  164. HTTPStatusCode: http.StatusInternalServerError,
  165. },
  166. ErrInvalidPart: {
  167. Code: "InvalidPart",
  168. 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.",
  169. HTTPStatusCode: http.StatusBadRequest,
  170. },
  171. ErrInvalidCopyDest: {
  172. Code: "InvalidRequest",
  173. 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.",
  174. HTTPStatusCode: http.StatusBadRequest,
  175. },
  176. ErrInvalidCopySource: {
  177. Code: "InvalidArgument",
  178. Description: "Copy Source must mention the source bucket and key: sourcebucket/sourcekey.",
  179. HTTPStatusCode: http.StatusBadRequest,
  180. },
  181. ErrMalformedXML: {
  182. Code: "MalformedXML",
  183. Description: "The XML you provided was not well-formed or did not validate against our published schema.",
  184. HTTPStatusCode: http.StatusBadRequest,
  185. },
  186. ErrAuthHeaderEmpty: {
  187. Code: "InvalidArgument",
  188. Description: "Authorization header is invalid -- one and only one ' ' (space) required.",
  189. HTTPStatusCode: http.StatusBadRequest,
  190. },
  191. ErrSignatureVersionNotSupported: {
  192. Code: "InvalidRequest",
  193. Description: "The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.",
  194. HTTPStatusCode: http.StatusBadRequest,
  195. },
  196. ErrMalformedPOSTRequest: {
  197. Code: "MalformedPOSTRequest",
  198. Description: "The body of your POST request is not well-formed multipart/form-data.",
  199. HTTPStatusCode: http.StatusBadRequest,
  200. },
  201. ErrPOSTFileRequired: {
  202. Code: "InvalidArgument",
  203. Description: "POST requires exactly one file upload per request.",
  204. HTTPStatusCode: http.StatusBadRequest,
  205. },
  206. ErrPostPolicyConditionInvalidFormat: {
  207. Code: "PostPolicyInvalidKeyName",
  208. Description: "Invalid according to Policy: Policy Condition failed",
  209. HTTPStatusCode: http.StatusForbidden,
  210. },
  211. ErrEntityTooSmall: {
  212. Code: "EntityTooSmall",
  213. Description: "Your proposed upload is smaller than the minimum allowed object size.",
  214. HTTPStatusCode: http.StatusBadRequest,
  215. },
  216. ErrEntityTooLarge: {
  217. Code: "EntityTooLarge",
  218. Description: "Your proposed upload exceeds the maximum allowed object size.",
  219. HTTPStatusCode: http.StatusBadRequest,
  220. },
  221. ErrMissingFields: {
  222. Code: "MissingFields",
  223. Description: "Missing fields in request.",
  224. HTTPStatusCode: http.StatusBadRequest,
  225. },
  226. ErrMissingCredTag: {
  227. Code: "InvalidRequest",
  228. Description: "Missing Credential field for this request.",
  229. HTTPStatusCode: http.StatusBadRequest,
  230. },
  231. ErrCredMalformed: {
  232. Code: "AuthorizationQueryParametersError",
  233. Description: "Error parsing the X-Amz-Credential parameter; the Credential is mal-formed; expecting \"<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request\".",
  234. HTTPStatusCode: http.StatusBadRequest,
  235. },
  236. ErrMalformedDate: {
  237. Code: "MalformedDate",
  238. Description: "Invalid date format header, expected to be in ISO8601, RFC1123 or RFC1123Z time format.",
  239. HTTPStatusCode: http.StatusBadRequest,
  240. },
  241. ErrMalformedPresignedDate: {
  242. Code: "AuthorizationQueryParametersError",
  243. Description: "X-Amz-Date must be in the ISO8601 Long Format \"yyyyMMdd'T'HHmmss'Z'\"",
  244. HTTPStatusCode: http.StatusBadRequest,
  245. },
  246. ErrMissingSignHeadersTag: {
  247. Code: "InvalidArgument",
  248. Description: "Signature header missing SignedHeaders field.",
  249. HTTPStatusCode: http.StatusBadRequest,
  250. },
  251. ErrMissingSignTag: {
  252. Code: "AccessDenied",
  253. Description: "Signature header missing Signature field.",
  254. HTTPStatusCode: http.StatusBadRequest,
  255. },
  256. ErrUnsignedHeaders: {
  257. Code: "AccessDenied",
  258. Description: "There were headers present in the request which were not signed",
  259. HTTPStatusCode: http.StatusBadRequest,
  260. },
  261. ErrInvalidQueryParams: {
  262. Code: "AuthorizationQueryParametersError",
  263. 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.",
  264. HTTPStatusCode: http.StatusBadRequest,
  265. },
  266. ErrInvalidQuerySignatureAlgo: {
  267. Code: "AuthorizationQueryParametersError",
  268. Description: "X-Amz-Algorithm only supports \"AWS4-HMAC-SHA256\".",
  269. HTTPStatusCode: http.StatusBadRequest,
  270. },
  271. ErrExpiredPresignRequest: {
  272. Code: "AccessDenied",
  273. Description: "Request has expired",
  274. HTTPStatusCode: http.StatusForbidden,
  275. },
  276. ErrMalformedExpires: {
  277. Code: "AuthorizationQueryParametersError",
  278. Description: "X-Amz-Expires should be a number",
  279. HTTPStatusCode: http.StatusBadRequest,
  280. },
  281. ErrNegativeExpires: {
  282. Code: "AuthorizationQueryParametersError",
  283. Description: "X-Amz-Expires must be non-negative",
  284. HTTPStatusCode: http.StatusBadRequest,
  285. },
  286. ErrMaximumExpires: {
  287. Code: "AuthorizationQueryParametersError",
  288. 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",
  289. HTTPStatusCode: http.StatusBadRequest,
  290. },
  291. ErrInvalidAccessKeyID: {
  292. Code: "InvalidAccessKeyId",
  293. Description: "The access key ID you provided does not exist in our records.",
  294. HTTPStatusCode: http.StatusForbidden,
  295. },
  296. ErrRequestNotReadyYet: {
  297. Code: "AccessDenied",
  298. Description: "Request is not valid yet",
  299. HTTPStatusCode: http.StatusForbidden,
  300. },
  301. ErrSignatureDoesNotMatch: {
  302. Code: "SignatureDoesNotMatch",
  303. Description: "The request signature we calculated does not match the signature you provided. Check your key and signing method.",
  304. HTTPStatusCode: http.StatusForbidden,
  305. },
  306. ErrContentSHA256Mismatch: {
  307. Code: "XAmzContentSHA256Mismatch",
  308. Description: "The provided 'x-amz-content-sha256' header does not match what was computed.",
  309. HTTPStatusCode: http.StatusBadRequest,
  310. },
  311. ErrMissingDateHeader: {
  312. Code: "AccessDenied",
  313. Description: "AWS authentication requires a valid Date or x-amz-date header",
  314. HTTPStatusCode: http.StatusBadRequest,
  315. },
  316. ErrInvalidRequest: {
  317. Code: "InvalidRequest",
  318. Description: "Invalid Request",
  319. HTTPStatusCode: http.StatusBadRequest,
  320. },
  321. ErrNotImplemented: {
  322. Code: "NotImplemented",
  323. Description: "A header you provided implies functionality that is not implemented",
  324. HTTPStatusCode: http.StatusNotImplemented,
  325. },
  326. }
  327. // GetAPIError provides API Error for input API error code.
  328. func GetAPIError(code ErrorCode) APIError {
  329. return errorCodeResponse[code]
  330. }