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.

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