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.

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