blob: e790196e997f1b5a33a295654578bf278ef451df [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 Perrittf094fef2016-03-07 01:41:59 -06007 Info string
8 Function string
jrperrittb1013232016-02-10 19:01:53 -06009}
10
Jon Perritta33da232016-03-02 04:43:08 -060011func (e BaseError) Error() string {
jrperrittb1013232016-02-10 19:01:53 -060012 return "An error occurred while executing a Gophercloud request."
13}
14
Jon Perritt256208d2016-02-28 23:38:03 -060015// ErrMissingInput is the error when input is required in a particular
16// situation but not provided by the user
17type ErrMissingInput struct {
Jon Perritta33da232016-03-02 04:43:08 -060018 BaseError
Jon Perritt256208d2016-02-28 23:38:03 -060019 Argument string
20}
21
Jon Perrittf094fef2016-03-07 01:41:59 -060022func (e ErrMissingInput) Error() string {
Jon Perritt256208d2016-02-28 23:38:03 -060023 return fmt.Sprintf("Missing input for argument [%s]", e.Argument)
24}
25
Jon Perrittf094fef2016-03-07 01:41:59 -060026// ErrInvalidInput is an error type used for most non-HTTP Gophercloud errors.
27type ErrInvalidInput struct {
28 ErrMissingInput
29 Value interface{}
30}
31
32func (e ErrInvalidInput) Error() string {
33 return fmt.Sprintf("Invalid input provided for argument [%s]: [%+v]", e.Argument, e.Value)
34}
35
jrperrittb1013232016-02-10 19:01:53 -060036// ErrUnexpectedResponseCode is returned by the Request method when a response code other than
37// those listed in OkCodes is encountered.
38type ErrUnexpectedResponseCode struct {
Jon Perritta33da232016-03-02 04:43:08 -060039 BaseError
jrperrittb1013232016-02-10 19:01:53 -060040 URL string
41 Method string
42 Expected []int
43 Actual int
44 Body []byte
45}
46
Jon Perrittf094fef2016-03-07 01:41:59 -060047func (err ErrUnexpectedResponseCode) Error() string {
jrperrittb1013232016-02-10 19:01:53 -060048 return fmt.Sprintf(
49 "Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s",
50 err.Expected, err.Method, err.URL, err.Actual, err.Body,
51 )
52}
53
Jon Perritte0f9e4f2016-02-21 21:41:03 -060054// ErrDefault400 is the default error type returned on a 400 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060055type ErrDefault400 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060056 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060057}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060058
59// ErrDefault401 is the default error type returned on a 401 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060060type ErrDefault401 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060061 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060062}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060063
64// ErrDefault404 is the default error type returned on a 404 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060065type ErrDefault404 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060066 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060067}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060068
69// ErrDefault405 is the default error type returned on a 405 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060070type ErrDefault405 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060071 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060072}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060073
74// ErrDefault408 is the default error type returned on a 408 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060075type ErrDefault408 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060076 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060077}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060078
79// ErrDefault429 is the default error type returned on a 429 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060080type ErrDefault429 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060081 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060082}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060083
84// ErrDefault500 is the default error type returned on a 500 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060085type ErrDefault500 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060086 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060087}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060088
89// ErrDefault503 is the default error type returned on a 503 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060090type ErrDefault503 struct {
Jon Perrittf094fef2016-03-07 01:41:59 -060091 ErrUnexpectedResponseCode
jrperrittb1013232016-02-10 19:01:53 -060092}
93
94func (e ErrDefault400) Error() string {
95 return "Invalid request due to incorrect syntax or missing required parameters."
96}
97func (e ErrDefault401) Error() string {
98 return "Authentication failed"
99}
100func (e ErrDefault404) Error() string {
101 return "Resource not found"
102}
103func (e ErrDefault405) Error() string {
104 return "Method not allowed"
105}
106func (e ErrDefault408) Error() string {
107 return "The server timed out waiting for the request"
108}
109func (e ErrDefault429) Error() string {
Jon Perritt256208d2016-02-28 23:38:03 -0600110 return "Too many requests have been sent in a given amount of time. Pause" +
111 " requests, wait up to one minute, and try again."
jrperrittb1013232016-02-10 19:01:53 -0600112}
113func (e ErrDefault500) Error() string {
114 return "Internal Server Error"
115}
116func (e ErrDefault503) Error() string {
Jon Perritt256208d2016-02-28 23:38:03 -0600117 return "The service is currently unable to handle the request due to a temporary" +
118 " overloading or maintenance. This is a temporary condition. Try again later."
jrperrittb1013232016-02-10 19:01:53 -0600119}
120
121// Err400er is the interface resource error types implement to override the error message
122// from a 400 error.
123type Err400er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600124 Error400(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600125}
126
127// Err401er is the interface resource error types implement to override the error message
128// from a 401 error.
129type Err401er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600130 Error401(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600131}
132
133// Err404er is the interface resource error types implement to override the error message
134// from a 404 error.
135type Err404er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600136 Error404(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600137}
138
139// Err405er is the interface resource error types implement to override the error message
140// from a 405 error.
141type Err405er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600142 Error405(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600143}
144
145// Err408er is the interface resource error types implement to override the error message
146// from a 408 error.
147type Err408er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600148 Error408(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600149}
150
151// Err429er is the interface resource error types implement to override the error message
152// from a 429 error.
153type Err429er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600154 Error429(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600155}
156
157// Err500er is the interface resource error types implement to override the error message
158// from a 500 error.
159type Err500er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600160 Error500(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600161}
162
163// Err503er is the interface resource error types implement to override the error message
164// from a 503 error.
165type Err503er interface {
Jon Perrittf094fef2016-03-07 01:41:59 -0600166 Error503(ErrUnexpectedResponseCode) error
jrperrittb1013232016-02-10 19:01:53 -0600167}
168
Jon Perritte0f9e4f2016-02-21 21:41:03 -0600169// ErrTimeOut is the error type returned when an operations times out.
jrperrittb1013232016-02-10 19:01:53 -0600170type ErrTimeOut struct {
Jon Perritta33da232016-03-02 04:43:08 -0600171 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600172}
173
Jon Perrittf094fef2016-03-07 01:41:59 -0600174func (e ErrTimeOut) Error() string {
jrperrittb1013232016-02-10 19:01:53 -0600175 return "A time out occurred"
176}
177
Jon Perritte0f9e4f2016-02-21 21:41:03 -0600178// ErrUnableToReauthenticate is the error type returned when reauthentication fails.
jrperrittb1013232016-02-10 19:01:53 -0600179type ErrUnableToReauthenticate struct {
Jon Perritta33da232016-03-02 04:43:08 -0600180 BaseError
Jon Perrittf094fef2016-03-07 01:41:59 -0600181 ErrOriginal error
jrperrittb1013232016-02-10 19:01:53 -0600182}
183
Jon Perrittf094fef2016-03-07 01:41:59 -0600184func (e ErrUnableToReauthenticate) Error() string {
185 return fmt.Sprintf("Unable to re-authenticate: %s", e.ErrOriginal)
jrperrittb1013232016-02-10 19:01:53 -0600186}
187
Jon Perritte0f9e4f2016-02-21 21:41:03 -0600188// ErrErrorAfterReauthentication is the error type returned when reauthentication
189// succeeds, but an error occurs afterword (usually an HTTP error).
jrperrittb1013232016-02-10 19:01:53 -0600190type ErrErrorAfterReauthentication struct {
Jon Perritta33da232016-03-02 04:43:08 -0600191 BaseError
Jon Perrittf094fef2016-03-07 01:41:59 -0600192 ErrOriginal error
jrperrittb1013232016-02-10 19:01:53 -0600193}
194
Jon Perrittf094fef2016-03-07 01:41:59 -0600195func (e ErrErrorAfterReauthentication) Error() string {
196 return fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.ErrOriginal)
jrperrittb1013232016-02-10 19:01:53 -0600197}
198
199// ErrServiceNotFound is returned when no service in a service catalog matches
200// the provided EndpointOpts. This is generally returned by provider service
201// factory methods like "NewComputeV2()" and can mean that a service is not
202// enabled for your account.
203type ErrServiceNotFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600204 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600205}
206
Jon Perrittf094fef2016-03-07 01:41:59 -0600207func (e ErrServiceNotFound) Error() string {
jrperrittb1013232016-02-10 19:01:53 -0600208 return "No suitable service could be found in the service catalog."
209}
210
211// ErrEndpointNotFound is returned when no available endpoints match the
212// provided EndpointOpts. This is also generally returned by provider service
213// factory methods, and usually indicates that a region was specified
214// incorrectly.
215type ErrEndpointNotFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600216 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600217}
218
Jon Perrittf094fef2016-03-07 01:41:59 -0600219func (e ErrEndpointNotFound) Error() string {
jrperrittb1013232016-02-10 19:01:53 -0600220 return "No suitable endpoint could be found in the service catalog."
221}
Jon Perritt256208d2016-02-28 23:38:03 -0600222
223// ErrResourceNotFound is the error when trying to retrieve a resource's
224// ID by name and the resource doesn't exist.
225type ErrResourceNotFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600226 BaseError
Jon Perrittf094fef2016-03-07 01:41:59 -0600227 Name string
Jon Perritt256208d2016-02-28 23:38:03 -0600228 ResourceType string
229}
230
Jon Perrittf094fef2016-03-07 01:41:59 -0600231func (e ErrResourceNotFound) Error() string {
232 return fmt.Sprintf("Unable to find %s with name %s", e.ResourceType, e.Name)
Jon Perritt256208d2016-02-28 23:38:03 -0600233}
234
235// ErrMultipleResourcesFound is the error when trying to retrieve a resource's
236// ID by name and multiple resources have the user-provided name.
237type ErrMultipleResourcesFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600238 BaseError
Jon Perritt256208d2016-02-28 23:38:03 -0600239 Name string
240 Count int
241 ResourceType string
242}
243
Jon Perrittf094fef2016-03-07 01:41:59 -0600244func (e ErrMultipleResourcesFound) Error() string {
Jon Perritta33da232016-03-02 04:43:08 -0600245 return fmt.Sprintf("Found %d %ss matching %s", e.Count, e.ResourceType, e.Name)
Jon Perritt256208d2016-02-28 23:38:03 -0600246}
Jon Perritt80251972016-03-09 00:32:30 -0600247
248// ErrUnexpectedType is the error when an unexpected type is encountered
249type ErrUnexpectedType struct {
250 BaseError
251 Expected string
252 Actual string
253}
254
255func (e ErrUnexpectedType) Error() string {
256 return fmt.Sprintf("Expected %s but got %s", e.Expected, e.Actual)
257}