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.

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