s3api_errors.go 14 KB

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