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.

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