blob: e0fe7c1e086328b248ef1e382ea22eaa1ee50b74 [file] [log] [blame]
jrperrittb1013232016-02-10 19:01:53 -06001package gophercloud
2
3import "fmt"
4
5// BaseError is an error type that all other error types embed.
6type BaseError struct {
Jon Perritt2be387a2016-03-31 09:31:58 -05007 DefaultErrString string
8 Info string
jrperrittb1013232016-02-10 19:01:53 -06009}
10
Jon Perritta33da232016-03-02 04:43:08 -060011func (e BaseError) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -050012 e.DefaultErrString = "An error occurred while executing a Gophercloud request."
13 return e.choseErrString()
14}
15
16func (e BaseError) choseErrString() string {
17 if e.Info != "" {
18 return e.Info
19 }
20 return e.DefaultErrString
jrperrittb1013232016-02-10 19:01:53 -060021}
22
Jon Perritt256208d2016-02-28 23:38:03 -060023// ErrMissingInput is the error when input is required in a particular
24// situation but not provided by the user
25type ErrMissingInput struct {
Jon Perritta33da232016-03-02 04:43:08 -060026 BaseError
Jon Perritt256208d2016-02-28 23:38:03 -060027 Argument string
28}
29
Jon Perrittf094fef2016-03-07 01:41:59 -060030func (e ErrMissingInput) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -050031 e.DefaultErrString = fmt.Sprintf("Missing input for argument [%s]", e.Argument)
32 return e.choseErrString()
Jon Perritt256208d2016-02-28 23:38:03 -060033}
34
Jon Perrittf094fef2016-03-07 01:41:59 -060035// ErrInvalidInput is an error type used for most non-HTTP Gophercloud errors.
36type ErrInvalidInput struct {
37 ErrMissingInput
38 Value interface{}
39}
40
41func (e ErrInvalidInput) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -050042 e.DefaultErrString = fmt.Sprintf("Invalid input provided for argument [%s]: [%+v]", e.Argument, e.Value)
43 return e.choseErrString()
Jon Perrittf094fef2016-03-07 01:41:59 -060044}
45
jrperrittb1013232016-02-10 19:01:53 -060046// ErrUnexpectedResponseCode is returned by the Request method when a response code other than
47// those listed in OkCodes is encountered.
48type ErrUnexpectedResponseCode struct {
Jon Perritta33da232016-03-02 04:43:08 -060049 BaseError
jrperrittb1013232016-02-10 19:01:53 -060050 URL string
51 Method string
52 Expected []int
53 Actual int
54 Body []byte
55}
56
Jon Perritt2be387a2016-03-31 09:31:58 -050057func (e ErrUnexpectedResponseCode) Error() string {
58 e.DefaultErrString = fmt.Sprintf(
jrperrittb1013232016-02-10 19:01:53 -060059 "Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s",
Jon Perritt2be387a2016-03-31 09:31:58 -050060 e.Expected, e.Method, e.URL, e.Actual, e.Body,
jrperrittb1013232016-02-10 19:01:53 -060061 )
Jon Perritt2be387a2016-03-31 09:31:58 -050062 return e.choseErrString()
jrperrittb1013232016-02-10 19:01:53 -060063}
64
Jon Perritte0f9e4f2016-02-21 21:41:03 -060065// ErrDefault400 is the default error type returned on a 400 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060066type ErrDefault400 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060067 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060068}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060069
70// ErrDefault401 is the default error type returned on a 401 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060071type ErrDefault401 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060072 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060073}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060074
75// ErrDefault404 is the default error type returned on a 404 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060076type ErrDefault404 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060077 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060078}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060079
80// ErrDefault405 is the default error type returned on a 405 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060081type ErrDefault405 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060082 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060083}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060084
85// ErrDefault408 is the default error type returned on a 408 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060086type ErrDefault408 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060087 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060088}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060089
90// ErrDefault429 is the default error type returned on a 429 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060091type ErrDefault429 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060092 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060093}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060094
95// ErrDefault500 is the default error type returned on a 500 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060096type ErrDefault500 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060097 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060098}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060099
100// ErrDefault503 is the default error type returned on a 503 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -0600101type ErrDefault503 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -0600102 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -0600103}
104
105func (e ErrDefault400) Error() string {
106 return "Invalid request due to incorrect syntax or missing required parameters."
107}
108func (e ErrDefault401) Error() string {
109 return "Authentication failed"
110}
111func (e ErrDefault404) Error() string {
112 return "Resource not found"
113}
114func (e ErrDefault405) Error() string {
115 return "Method not allowed"
116}
117func (e ErrDefault408) Error() string {
118 return "The server timed out waiting for the request"
119}
120func (e ErrDefault429) Error() string {
Jon Perritt256208d2016-02-28 23:38:03 -0600121 return "Too many requests have been sent in a given amount of time. Pause" +
122 " requests, wait up to one minute, and try again."
jrperrittb1013232016-02-10 19:01:53 -0600123}
124func (e ErrDefault500) Error() string {
125 return "Internal Server Error"
126}
127func (e ErrDefault503) Error() string {
Jon Perritt256208d2016-02-28 23:38:03 -0600128 return "The service is currently unable to handle the request due to a temporary" +
129 " overloading or maintenance. This is a temporary condition. Try again later."
jrperrittb1013232016-02-10 19:01:53 -0600130}
131
132// Err400er is the interface resource error types implement to override the error message
133// from a 400 error.
134type Err400er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600135 Error400(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600136}
137
138// Err401er is the interface resource error types implement to override the error message
139// from a 401 error.
140type Err401er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600141 Error401(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600142}
143
144// Err404er is the interface resource error types implement to override the error message
145// from a 404 error.
146type Err404er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600147 Error404(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600148}
149
150// Err405er is the interface resource error types implement to override the error message
151// from a 405 error.
152type Err405er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600153 Error405(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600154}
155
156// Err408er is the interface resource error types implement to override the error message
157// from a 408 error.
158type Err408er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600159 Error408(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600160}
161
162// Err429er is the interface resource error types implement to override the error message
163// from a 429 error.
164type Err429er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600165 Error429(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600166}
167
168// Err500er is the interface resource error types implement to override the error message
169// from a 500 error.
170type Err500er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600171 Error500(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600172}
173
174// Err503er is the interface resource error types implement to override the error message
175// from a 503 error.
176type Err503er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600177 Error503(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600178}
179
Jon Perritte0f9e4f2016-02-21 21:41:03 -0600180// ErrTimeOut is the error type returned when an operations times out.
jrperrittb1013232016-02-10 19:01:53 -0600181type ErrTimeOut struct {
Jon Perritta33da232016-03-02 04:43:08 -0600182 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600183}
184
Jon Perrittf094fef2016-03-07 01:41:59 -0600185func (e ErrTimeOut) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -0500186 e.DefaultErrString = "A time out occurred"
187 return e.choseErrString()
jrperrittb1013232016-02-10 19:01:53 -0600188}
189
Jon Perritte0f9e4f2016-02-21 21:41:03 -0600190// ErrUnableToReauthenticate is the error type returned when reauthentication fails.
jrperrittb1013232016-02-10 19:01:53 -0600191type ErrUnableToReauthenticate struct {
Jon Perritta33da232016-03-02 04:43:08 -0600192 BaseError
Jon Perrittf094fef2016-03-07 01:41:59 -0600193 ErrOriginal error
jrperrittb1013232016-02-10 19:01:53 -0600194}
195
Jon Perrittf094fef2016-03-07 01:41:59 -0600196func (e ErrUnableToReauthenticate) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -0500197 e.DefaultErrString = fmt.Sprintf("Unable to re-authenticate: %s", e.ErrOriginal)
198 return e.choseErrString()
jrperrittb1013232016-02-10 19:01:53 -0600199}
200
Jon Perritte0f9e4f2016-02-21 21:41:03 -0600201// ErrErrorAfterReauthentication is the error type returned when reauthentication
202// succeeds, but an error occurs afterword (usually an HTTP error).
jrperrittb1013232016-02-10 19:01:53 -0600203type ErrErrorAfterReauthentication struct {
Jon Perritta33da232016-03-02 04:43:08 -0600204 BaseError
Jon Perrittf094fef2016-03-07 01:41:59 -0600205 ErrOriginal error
jrperrittb1013232016-02-10 19:01:53 -0600206}
207
Jon Perrittf094fef2016-03-07 01:41:59 -0600208func (e ErrErrorAfterReauthentication) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -0500209 e.DefaultErrString = fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.ErrOriginal)
210 return e.choseErrString()
jrperrittb1013232016-02-10 19:01:53 -0600211}
212
213// ErrServiceNotFound is returned when no service in a service catalog matches
214// the provided EndpointOpts. This is generally returned by provider service
215// factory methods like "NewComputeV2()" and can mean that a service is not
216// enabled for your account.
217type ErrServiceNotFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600218 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600219}
220
Jon Perrittf094fef2016-03-07 01:41:59 -0600221func (e ErrServiceNotFound) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -0500222 e.DefaultErrString = "No suitable service could be found in the service catalog."
223 return e.choseErrString()
jrperrittb1013232016-02-10 19:01:53 -0600224}
225
226// ErrEndpointNotFound is returned when no available endpoints match the
227// provided EndpointOpts. This is also generally returned by provider service
228// factory methods, and usually indicates that a region was specified
229// incorrectly.
230type ErrEndpointNotFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600231 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600232}
233
Jon Perrittf094fef2016-03-07 01:41:59 -0600234func (e ErrEndpointNotFound) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -0500235 e.DefaultErrString = "No suitable endpoint could be found in the service catalog."
236 return e.choseErrString()
jrperrittb1013232016-02-10 19:01:53 -0600237}
Jon Perritt256208d2016-02-28 23:38:03 -0600238
239// ErrResourceNotFound is the error when trying to retrieve a resource's
240// ID by name and the resource doesn't exist.
241type ErrResourceNotFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600242 BaseError
Jon Perrittf094fef2016-03-07 01:41:59 -0600243 Name string
Jon Perritt256208d2016-02-28 23:38:03 -0600244 ResourceType string
245}
246
Jon Perrittf094fef2016-03-07 01:41:59 -0600247func (e ErrResourceNotFound) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -0500248 e.DefaultErrString = fmt.Sprintf("Unable to find %s with name %s", e.ResourceType, e.Name)
249 return e.choseErrString()
Jon Perritt256208d2016-02-28 23:38:03 -0600250}
251
252// ErrMultipleResourcesFound is the error when trying to retrieve a resource's
253// ID by name and multiple resources have the user-provided name.
254type ErrMultipleResourcesFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600255 BaseError
Jon Perritt256208d2016-02-28 23:38:03 -0600256 Name string
257 Count int
258 ResourceType string
259}
260
Jon Perrittf094fef2016-03-07 01:41:59 -0600261func (e ErrMultipleResourcesFound) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -0500262 e.DefaultErrString = fmt.Sprintf("Found %d %ss matching %s", e.Count, e.ResourceType, e.Name)
263 return e.choseErrString()
Jon Perritt256208d2016-02-28 23:38:03 -0600264}
Jon Perritt80251972016-03-09 00:32:30 -0600265
266// ErrUnexpectedType is the error when an unexpected type is encountered
267type ErrUnexpectedType struct {
268 BaseError
269 Expected string
270 Actual string
271}
272
273func (e ErrUnexpectedType) Error() string {
Jon Perritt2be387a2016-03-31 09:31:58 -0500274 e.DefaultErrString = fmt.Sprintf("Expected %s but got %s", e.Expected, e.Actual)
275 return e.choseErrString()
Jon Perritt80251972016-03-09 00:32:30 -0600276}
jrperritt0bc55782016-07-27 13:50:14 -0500277
278func unacceptedAttributeErr(attribute string) string {
279 return fmt.Sprintf("The base Identity V3 API does not accept authentication by %s", attribute)
280}
281
282func redundantWithTokenErr(attribute string) string {
283 return fmt.Sprintf("%s may not be provided when authenticating with a TokenID", attribute)
284}
285
286func redundantWithUserID(attribute string) string {
287 return fmt.Sprintf("%s may not be provided when authenticating with a UserID", attribute)
288}
289
290// ErrAPIKeyProvided indicates that an APIKey was provided but can't be used.
291type ErrAPIKeyProvided struct{ BaseError }
292
293func (e ErrAPIKeyProvided) Error() string {
294 return unacceptedAttributeErr("APIKey")
295}
296
297// ErrTenantIDProvided indicates that a TenantID was provided but can't be used.
298type ErrTenantIDProvided struct{ BaseError }
299
300func (e ErrTenantIDProvided) Error() string {
301 return unacceptedAttributeErr("TenantID")
302}
303
304// ErrTenantNameProvided indicates that a TenantName was provided but can't be used.
305type ErrTenantNameProvided struct{ BaseError }
306
307func (e ErrTenantNameProvided) Error() string {
308 return unacceptedAttributeErr("TenantName")
309}
310
311// ErrUsernameWithToken indicates that a Username was provided, but token authentication is being used instead.
312type ErrUsernameWithToken struct{ BaseError }
313
314func (e ErrUsernameWithToken) Error() string {
315 return redundantWithTokenErr("Username")
316}
317
318// ErrUserIDWithToken indicates that a UserID was provided, but token authentication is being used instead.
319type ErrUserIDWithToken struct{ BaseError }
320
321func (e ErrUserIDWithToken) Error() string {
322 return redundantWithTokenErr("UserID")
323}
324
325// ErrDomainIDWithToken indicates that a DomainID was provided, but token authentication is being used instead.
326type ErrDomainIDWithToken struct{ BaseError }
327
328func (e ErrDomainIDWithToken) Error() string {
329 return redundantWithTokenErr("DomainID")
330}
331
332// ErrDomainNameWithToken indicates that a DomainName was provided, but token authentication is being used instead.s
333type ErrDomainNameWithToken struct{ BaseError }
334
335func (e ErrDomainNameWithToken) Error() string {
336 return redundantWithTokenErr("DomainName")
337}
338
339// ErrUsernameOrUserID indicates that neither username nor userID are specified, or both are at once.
340type ErrUsernameOrUserID struct{ BaseError }
341
342func (e ErrUsernameOrUserID) Error() string {
343 return "Exactly one of Username and UserID must be provided for password authentication"
344}
345
346// ErrDomainIDWithUserID indicates that a DomainID was provided, but unnecessary because a UserID is being used.
347type ErrDomainIDWithUserID struct{ BaseError }
348
349func (e ErrDomainIDWithUserID) Error() string {
350 return redundantWithUserID("DomainID")
351}
352
353// ErrDomainNameWithUserID indicates that a DomainName was provided, but unnecessary because a UserID is being used.
354type ErrDomainNameWithUserID struct{ BaseError }
355
356func (e ErrDomainNameWithUserID) Error() string {
357 return redundantWithUserID("DomainName")
358}
359
360// ErrDomainIDOrDomainName indicates that a username was provided, but no domain to scope it.
361// It may also indicate that both a DomainID and a DomainName were provided at once.
362type ErrDomainIDOrDomainName struct{ BaseError }
363
364func (e ErrDomainIDOrDomainName) Error() string {
365 return "You must provide exactly one of DomainID or DomainName to authenticate by Username"
366}
367
368// ErrMissingPassword indicates that no password was provided and no token is available.
369type ErrMissingPassword struct{ BaseError }
370
371func (e ErrMissingPassword) Error() string {
372 return "You must provide a password to authenticate"
373}
374
375// ErrScopeDomainIDOrDomainName indicates that a domain ID or Name was required in a Scope, but not present.
376type ErrScopeDomainIDOrDomainName struct{ BaseError }
377
378func (e ErrScopeDomainIDOrDomainName) Error() string {
379 return "You must provide exactly one of DomainID or DomainName in a Scope with ProjectName"
380}
381
382// ErrScopeProjectIDOrProjectName indicates that both a ProjectID and a ProjectName were provided in a Scope.
383type ErrScopeProjectIDOrProjectName struct{ BaseError }
384
385func (e ErrScopeProjectIDOrProjectName) Error() string {
386 return "You must provide at most one of ProjectID or ProjectName in a Scope"
387}
388
389// ErrScopeProjectIDAlone indicates that a ProjectID was provided with other constraints in a Scope.
390type ErrScopeProjectIDAlone struct{ BaseError }
391
392func (e ErrScopeProjectIDAlone) Error() string {
393 return "ProjectID must be supplied alone in a Scope"
394}
395
396// ErrScopeDomainName indicates that a DomainName was provided alone in a Scope.
397type ErrScopeDomainName struct{ BaseError }
398
399func (e ErrScopeDomainName) Error() string {
400 return "DomainName must be supplied with a ProjectName or ProjectID in a Scope"
401}
402
403// ErrScopeEmpty indicates that no credentials were provided in a Scope.
404type ErrScopeEmpty struct{ BaseError }
405
406func (e ErrScopeEmpty) Error() string {
407 return "You must provide either a Project or Domain in a Scope"
408}