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.

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