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.

100 lines
3.0 KiB

7 years ago
7 years ago
7 years ago
  1. package s3api
  2. import (
  3. "encoding/xml"
  4. "net/http"
  5. )
  6. // APIError structure
  7. type APIError struct {
  8. Code string
  9. Description string
  10. HTTPStatusCode int
  11. }
  12. // RESTErrorResponse - error response format
  13. type RESTErrorResponse struct {
  14. XMLName xml.Name `xml:"Error" json:"-"`
  15. Code string `xml:"Code" json:"Code"`
  16. Message string `xml:"Message" json:"Message"`
  17. Resource string `xml:"Resource" json:"Resource"`
  18. RequestID string `xml:"RequestId" json:"RequestId"`
  19. }
  20. // ErrorCode type of error status.
  21. type ErrorCode int
  22. // Error codes, see full list at http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
  23. const (
  24. ErrNone ErrorCode = iota
  25. ErrMethodNotAllowed
  26. ErrBucketNotEmpty
  27. ErrBucketAlreadyExists
  28. ErrBucketAlreadyOwnedByYou
  29. ErrNoSuchBucket
  30. ErrInvalidBucketName
  31. ErrInvalidDigest
  32. ErrInvalidMaxKeys
  33. ErrInternalError
  34. ErrNotImplemented
  35. )
  36. // error code to APIError structure, these fields carry respective
  37. // descriptions for all the error responses.
  38. var errorCodeResponse = map[ErrorCode]APIError{
  39. ErrMethodNotAllowed: {
  40. Code: "MethodNotAllowed",
  41. Description: "The specified method is not allowed against this resource.",
  42. HTTPStatusCode: http.StatusMethodNotAllowed,
  43. },
  44. ErrBucketNotEmpty: {
  45. Code: "BucketNotEmpty",
  46. Description: "The bucket you tried to delete is not empty",
  47. HTTPStatusCode: http.StatusConflict,
  48. },
  49. ErrBucketAlreadyExists: {
  50. Code: "BucketAlreadyExists",
  51. Description: "The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.",
  52. HTTPStatusCode: http.StatusConflict,
  53. },
  54. ErrBucketAlreadyOwnedByYou: {
  55. Code: "BucketAlreadyOwnedByYou",
  56. Description: "Your previous request to create the named bucket succeeded and you already own it.",
  57. HTTPStatusCode: http.StatusConflict,
  58. },
  59. ErrInvalidBucketName: {
  60. Code: "InvalidBucketName",
  61. Description: "The specified bucket is not valid.",
  62. HTTPStatusCode: http.StatusBadRequest,
  63. },
  64. ErrInvalidDigest: {
  65. Code: "InvalidDigest",
  66. Description: "The Content-Md5 you specified is not valid.",
  67. HTTPStatusCode: http.StatusBadRequest,
  68. },
  69. ErrInvalidMaxKeys: {
  70. Code: "InvalidArgument",
  71. Description: "Argument maxKeys must be an integer between 0 and 2147483647",
  72. HTTPStatusCode: http.StatusBadRequest,
  73. },
  74. ErrNoSuchBucket: {
  75. Code: "NoSuchBucket",
  76. Description: "The specified bucket does not exist",
  77. HTTPStatusCode: http.StatusNotFound,
  78. },
  79. ErrInternalError: {
  80. Code: "InternalError",
  81. Description: "We encountered an internal error, please try again.",
  82. HTTPStatusCode: http.StatusInternalServerError,
  83. },
  84. ErrNotImplemented: {
  85. Code: "NotImplemented",
  86. Description: "A header you provided implies functionality that is not implemented",
  87. HTTPStatusCode: http.StatusNotImplemented,
  88. },
  89. }
  90. // getAPIError provides API Error for input API error code.
  91. func getAPIError(code ErrorCode) APIError {
  92. return errorCodeResponse[code]
  93. }