/vendor/github.com/hashicorp/terraform/vendor/github.com/gophercloud/gophercloud/errors.go

https://github.com/alibaba/terraform-provider · Go · 408 lines · 263 code · 80 blank · 65 comment · 2 complexity · c8196d152b0ac8105d7dc5745c575248 MD5 · raw file

  1. package gophercloud
  2. import "fmt"
  3. // BaseError is an error type that all other error types embed.
  4. type BaseError struct {
  5. DefaultErrString string
  6. Info string
  7. }
  8. func (e BaseError) Error() string {
  9. e.DefaultErrString = "An error occurred while executing a Gophercloud request."
  10. return e.choseErrString()
  11. }
  12. func (e BaseError) choseErrString() string {
  13. if e.Info != "" {
  14. return e.Info
  15. }
  16. return e.DefaultErrString
  17. }
  18. // ErrMissingInput is the error when input is required in a particular
  19. // situation but not provided by the user
  20. type ErrMissingInput struct {
  21. BaseError
  22. Argument string
  23. }
  24. func (e ErrMissingInput) Error() string {
  25. e.DefaultErrString = fmt.Sprintf("Missing input for argument [%s]", e.Argument)
  26. return e.choseErrString()
  27. }
  28. // ErrInvalidInput is an error type used for most non-HTTP Gophercloud errors.
  29. type ErrInvalidInput struct {
  30. ErrMissingInput
  31. Value interface{}
  32. }
  33. func (e ErrInvalidInput) Error() string {
  34. e.DefaultErrString = fmt.Sprintf("Invalid input provided for argument [%s]: [%+v]", e.Argument, e.Value)
  35. return e.choseErrString()
  36. }
  37. // ErrUnexpectedResponseCode is returned by the Request method when a response code other than
  38. // those listed in OkCodes is encountered.
  39. type ErrUnexpectedResponseCode struct {
  40. BaseError
  41. URL string
  42. Method string
  43. Expected []int
  44. Actual int
  45. Body []byte
  46. }
  47. func (e ErrUnexpectedResponseCode) Error() string {
  48. e.DefaultErrString = fmt.Sprintf(
  49. "Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s",
  50. e.Expected, e.Method, e.URL, e.Actual, e.Body,
  51. )
  52. return e.choseErrString()
  53. }
  54. // ErrDefault400 is the default error type returned on a 400 HTTP response code.
  55. type ErrDefault400 struct {
  56. ErrUnexpectedResponseCode
  57. }
  58. // ErrDefault401 is the default error type returned on a 401 HTTP response code.
  59. type ErrDefault401 struct {
  60. ErrUnexpectedResponseCode
  61. }
  62. // ErrDefault404 is the default error type returned on a 404 HTTP response code.
  63. type ErrDefault404 struct {
  64. ErrUnexpectedResponseCode
  65. }
  66. // ErrDefault405 is the default error type returned on a 405 HTTP response code.
  67. type ErrDefault405 struct {
  68. ErrUnexpectedResponseCode
  69. }
  70. // ErrDefault408 is the default error type returned on a 408 HTTP response code.
  71. type ErrDefault408 struct {
  72. ErrUnexpectedResponseCode
  73. }
  74. // ErrDefault429 is the default error type returned on a 429 HTTP response code.
  75. type ErrDefault429 struct {
  76. ErrUnexpectedResponseCode
  77. }
  78. // ErrDefault500 is the default error type returned on a 500 HTTP response code.
  79. type ErrDefault500 struct {
  80. ErrUnexpectedResponseCode
  81. }
  82. // ErrDefault503 is the default error type returned on a 503 HTTP response code.
  83. type ErrDefault503 struct {
  84. ErrUnexpectedResponseCode
  85. }
  86. func (e ErrDefault400) Error() string {
  87. return "Invalid request due to incorrect syntax or missing required parameters."
  88. }
  89. func (e ErrDefault401) Error() string {
  90. return "Authentication failed"
  91. }
  92. func (e ErrDefault404) Error() string {
  93. return "Resource not found"
  94. }
  95. func (e ErrDefault405) Error() string {
  96. return "Method not allowed"
  97. }
  98. func (e ErrDefault408) Error() string {
  99. return "The server timed out waiting for the request"
  100. }
  101. func (e ErrDefault429) Error() string {
  102. return "Too many requests have been sent in a given amount of time. Pause" +
  103. " requests, wait up to one minute, and try again."
  104. }
  105. func (e ErrDefault500) Error() string {
  106. return "Internal Server Error"
  107. }
  108. func (e ErrDefault503) Error() string {
  109. return "The service is currently unable to handle the request due to a temporary" +
  110. " overloading or maintenance. This is a temporary condition. Try again later."
  111. }
  112. // Err400er is the interface resource error types implement to override the error message
  113. // from a 400 error.
  114. type Err400er interface {
  115. Error400(ErrUnexpectedResponseCode) error
  116. }
  117. // Err401er is the interface resource error types implement to override the error message
  118. // from a 401 error.
  119. type Err401er interface {
  120. Error401(ErrUnexpectedResponseCode) error
  121. }
  122. // Err404er is the interface resource error types implement to override the error message
  123. // from a 404 error.
  124. type Err404er interface {
  125. Error404(ErrUnexpectedResponseCode) error
  126. }
  127. // Err405er is the interface resource error types implement to override the error message
  128. // from a 405 error.
  129. type Err405er interface {
  130. Error405(ErrUnexpectedResponseCode) error
  131. }
  132. // Err408er is the interface resource error types implement to override the error message
  133. // from a 408 error.
  134. type Err408er interface {
  135. Error408(ErrUnexpectedResponseCode) error
  136. }
  137. // Err429er is the interface resource error types implement to override the error message
  138. // from a 429 error.
  139. type Err429er interface {
  140. Error429(ErrUnexpectedResponseCode) error
  141. }
  142. // Err500er is the interface resource error types implement to override the error message
  143. // from a 500 error.
  144. type Err500er interface {
  145. Error500(ErrUnexpectedResponseCode) error
  146. }
  147. // Err503er is the interface resource error types implement to override the error message
  148. // from a 503 error.
  149. type Err503er interface {
  150. Error503(ErrUnexpectedResponseCode) error
  151. }
  152. // ErrTimeOut is the error type returned when an operations times out.
  153. type ErrTimeOut struct {
  154. BaseError
  155. }
  156. func (e ErrTimeOut) Error() string {
  157. e.DefaultErrString = "A time out occurred"
  158. return e.choseErrString()
  159. }
  160. // ErrUnableToReauthenticate is the error type returned when reauthentication fails.
  161. type ErrUnableToReauthenticate struct {
  162. BaseError
  163. ErrOriginal error
  164. }
  165. func (e ErrUnableToReauthenticate) Error() string {
  166. e.DefaultErrString = fmt.Sprintf("Unable to re-authenticate: %s", e.ErrOriginal)
  167. return e.choseErrString()
  168. }
  169. // ErrErrorAfterReauthentication is the error type returned when reauthentication
  170. // succeeds, but an error occurs afterword (usually an HTTP error).
  171. type ErrErrorAfterReauthentication struct {
  172. BaseError
  173. ErrOriginal error
  174. }
  175. func (e ErrErrorAfterReauthentication) Error() string {
  176. e.DefaultErrString = fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.ErrOriginal)
  177. return e.choseErrString()
  178. }
  179. // ErrServiceNotFound is returned when no service in a service catalog matches
  180. // the provided EndpointOpts. This is generally returned by provider service
  181. // factory methods like "NewComputeV2()" and can mean that a service is not
  182. // enabled for your account.
  183. type ErrServiceNotFound struct {
  184. BaseError
  185. }
  186. func (e ErrServiceNotFound) Error() string {
  187. e.DefaultErrString = "No suitable service could be found in the service catalog."
  188. return e.choseErrString()
  189. }
  190. // ErrEndpointNotFound is returned when no available endpoints match the
  191. // provided EndpointOpts. This is also generally returned by provider service
  192. // factory methods, and usually indicates that a region was specified
  193. // incorrectly.
  194. type ErrEndpointNotFound struct {
  195. BaseError
  196. }
  197. func (e ErrEndpointNotFound) Error() string {
  198. e.DefaultErrString = "No suitable endpoint could be found in the service catalog."
  199. return e.choseErrString()
  200. }
  201. // ErrResourceNotFound is the error when trying to retrieve a resource's
  202. // ID by name and the resource doesn't exist.
  203. type ErrResourceNotFound struct {
  204. BaseError
  205. Name string
  206. ResourceType string
  207. }
  208. func (e ErrResourceNotFound) Error() string {
  209. e.DefaultErrString = fmt.Sprintf("Unable to find %s with name %s", e.ResourceType, e.Name)
  210. return e.choseErrString()
  211. }
  212. // ErrMultipleResourcesFound is the error when trying to retrieve a resource's
  213. // ID by name and multiple resources have the user-provided name.
  214. type ErrMultipleResourcesFound struct {
  215. BaseError
  216. Name string
  217. Count int
  218. ResourceType string
  219. }
  220. func (e ErrMultipleResourcesFound) Error() string {
  221. e.DefaultErrString = fmt.Sprintf("Found %d %ss matching %s", e.Count, e.ResourceType, e.Name)
  222. return e.choseErrString()
  223. }
  224. // ErrUnexpectedType is the error when an unexpected type is encountered
  225. type ErrUnexpectedType struct {
  226. BaseError
  227. Expected string
  228. Actual string
  229. }
  230. func (e ErrUnexpectedType) Error() string {
  231. e.DefaultErrString = fmt.Sprintf("Expected %s but got %s", e.Expected, e.Actual)
  232. return e.choseErrString()
  233. }
  234. func unacceptedAttributeErr(attribute string) string {
  235. return fmt.Sprintf("The base Identity V3 API does not accept authentication by %s", attribute)
  236. }
  237. func redundantWithTokenErr(attribute string) string {
  238. return fmt.Sprintf("%s may not be provided when authenticating with a TokenID", attribute)
  239. }
  240. func redundantWithUserID(attribute string) string {
  241. return fmt.Sprintf("%s may not be provided when authenticating with a UserID", attribute)
  242. }
  243. // ErrAPIKeyProvided indicates that an APIKey was provided but can't be used.
  244. type ErrAPIKeyProvided struct{ BaseError }
  245. func (e ErrAPIKeyProvided) Error() string {
  246. return unacceptedAttributeErr("APIKey")
  247. }
  248. // ErrTenantIDProvided indicates that a TenantID was provided but can't be used.
  249. type ErrTenantIDProvided struct{ BaseError }
  250. func (e ErrTenantIDProvided) Error() string {
  251. return unacceptedAttributeErr("TenantID")
  252. }
  253. // ErrTenantNameProvided indicates that a TenantName was provided but can't be used.
  254. type ErrTenantNameProvided struct{ BaseError }
  255. func (e ErrTenantNameProvided) Error() string {
  256. return unacceptedAttributeErr("TenantName")
  257. }
  258. // ErrUsernameWithToken indicates that a Username was provided, but token authentication is being used instead.
  259. type ErrUsernameWithToken struct{ BaseError }
  260. func (e ErrUsernameWithToken) Error() string {
  261. return redundantWithTokenErr("Username")
  262. }
  263. // ErrUserIDWithToken indicates that a UserID was provided, but token authentication is being used instead.
  264. type ErrUserIDWithToken struct{ BaseError }
  265. func (e ErrUserIDWithToken) Error() string {
  266. return redundantWithTokenErr("UserID")
  267. }
  268. // ErrDomainIDWithToken indicates that a DomainID was provided, but token authentication is being used instead.
  269. type ErrDomainIDWithToken struct{ BaseError }
  270. func (e ErrDomainIDWithToken) Error() string {
  271. return redundantWithTokenErr("DomainID")
  272. }
  273. // ErrDomainNameWithToken indicates that a DomainName was provided, but token authentication is being used instead.s
  274. type ErrDomainNameWithToken struct{ BaseError }
  275. func (e ErrDomainNameWithToken) Error() string {
  276. return redundantWithTokenErr("DomainName")
  277. }
  278. // ErrUsernameOrUserID indicates that neither username nor userID are specified, or both are at once.
  279. type ErrUsernameOrUserID struct{ BaseError }
  280. func (e ErrUsernameOrUserID) Error() string {
  281. return "Exactly one of Username and UserID must be provided for password authentication"
  282. }
  283. // ErrDomainIDWithUserID indicates that a DomainID was provided, but unnecessary because a UserID is being used.
  284. type ErrDomainIDWithUserID struct{ BaseError }
  285. func (e ErrDomainIDWithUserID) Error() string {
  286. return redundantWithUserID("DomainID")
  287. }
  288. // ErrDomainNameWithUserID indicates that a DomainName was provided, but unnecessary because a UserID is being used.
  289. type ErrDomainNameWithUserID struct{ BaseError }
  290. func (e ErrDomainNameWithUserID) Error() string {
  291. return redundantWithUserID("DomainName")
  292. }
  293. // ErrDomainIDOrDomainName indicates that a username was provided, but no domain to scope it.
  294. // It may also indicate that both a DomainID and a DomainName were provided at once.
  295. type ErrDomainIDOrDomainName struct{ BaseError }
  296. func (e ErrDomainIDOrDomainName) Error() string {
  297. return "You must provide exactly one of DomainID or DomainName to authenticate by Username"
  298. }
  299. // ErrMissingPassword indicates that no password was provided and no token is available.
  300. type ErrMissingPassword struct{ BaseError }
  301. func (e ErrMissingPassword) Error() string {
  302. return "You must provide a password to authenticate"
  303. }
  304. // ErrScopeDomainIDOrDomainName indicates that a domain ID or Name was required in a Scope, but not present.
  305. type ErrScopeDomainIDOrDomainName struct{ BaseError }
  306. func (e ErrScopeDomainIDOrDomainName) Error() string {
  307. return "You must provide exactly one of DomainID or DomainName in a Scope with ProjectName"
  308. }
  309. // ErrScopeProjectIDOrProjectName indicates that both a ProjectID and a ProjectName were provided in a Scope.
  310. type ErrScopeProjectIDOrProjectName struct{ BaseError }
  311. func (e ErrScopeProjectIDOrProjectName) Error() string {
  312. return "You must provide at most one of ProjectID or ProjectName in a Scope"
  313. }
  314. // ErrScopeProjectIDAlone indicates that a ProjectID was provided with other constraints in a Scope.
  315. type ErrScopeProjectIDAlone struct{ BaseError }
  316. func (e ErrScopeProjectIDAlone) Error() string {
  317. return "ProjectID must be supplied alone in a Scope"
  318. }
  319. // ErrScopeDomainName indicates that a DomainName was provided alone in a Scope.
  320. type ErrScopeDomainName struct{ BaseError }
  321. func (e ErrScopeDomainName) Error() string {
  322. return "DomainName must be supplied with a ProjectName or ProjectID in a Scope"
  323. }
  324. // ErrScopeEmpty indicates that no credentials were provided in a Scope.
  325. type ErrScopeEmpty struct{ BaseError }
  326. func (e ErrScopeEmpty) Error() string {
  327. return "You must provide either a Project or Domain in a Scope"
  328. }