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.

422 lines
14 KiB

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