blob: 978fcb55e6be5a0102ebc9931b80117a22beafa7 [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}