blob: b28be11b168d90665accf804907da10505e6a727 [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 Perritte0f9e4f2016-02-21 21:41:03 -06007 OriginalError error
jrperrittb1013232016-02-10 19:01:53 -06008 Function string
9}
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
15// ErrInvalidInput is an error type used for most non-HTTP Gophercloud errors.
16type ErrInvalidInput struct {
Jon Perritta33da232016-03-02 04:43:08 -060017 BaseError
jrperrittb1013232016-02-10 19:01:53 -060018 Argument string
19 Value interface{}
20}
21
22func (e *ErrInvalidInput) Error() string {
23 return fmt.Sprintf("Invalid input provided for argument [%s]: [%+v]", e.Argument, e.Value)
24}
25
Jon Perritt256208d2016-02-28 23:38:03 -060026// ErrMissingInput is the error when input is required in a particular
27// situation but not provided by the user
28type ErrMissingInput struct {
Jon Perritta33da232016-03-02 04:43:08 -060029 BaseError
Jon Perritt256208d2016-02-28 23:38:03 -060030 Argument string
31}
32
33func (e *ErrMissingInput) Error() string {
34 return fmt.Sprintf("Missing input for argument [%s]", e.Argument)
35}
36
jrperrittb1013232016-02-10 19:01:53 -060037// ErrUnexpectedResponseCode is returned by the Request method when a response code other than
38// those listed in OkCodes is encountered.
39type ErrUnexpectedResponseCode struct {
Jon Perritta33da232016-03-02 04:43:08 -060040 BaseError
jrperrittb1013232016-02-10 19:01:53 -060041 URL string
42 Method string
43 Expected []int
44 Actual int
45 Body []byte
46}
47
48func (err *ErrUnexpectedResponseCode) Error() string {
49 return fmt.Sprintf(
50 "Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s",
51 err.Expected, err.Method, err.URL, err.Actual, err.Body,
52 )
53}
54
Jon Perritte0f9e4f2016-02-21 21:41:03 -060055// ErrDefault400 is the default error type returned on a 400 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060056type ErrDefault400 struct {
57 *ErrUnexpectedResponseCode
58}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060059
60// ErrDefault401 is the default error type returned on a 401 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060061type ErrDefault401 struct {
62 *ErrUnexpectedResponseCode
63}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060064
65// ErrDefault404 is the default error type returned on a 404 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060066type ErrDefault404 struct {
67 *ErrUnexpectedResponseCode
68}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060069
70// ErrDefault405 is the default error type returned on a 405 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060071type ErrDefault405 struct {
72 *ErrUnexpectedResponseCode
73}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060074
75// ErrDefault408 is the default error type returned on a 408 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060076type ErrDefault408 struct {
77 *ErrUnexpectedResponseCode
78}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060079
80// ErrDefault429 is the default error type returned on a 429 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060081type ErrDefault429 struct {
82 *ErrUnexpectedResponseCode
83}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060084
85// ErrDefault500 is the default error type returned on a 500 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060086type ErrDefault500 struct {
87 *ErrUnexpectedResponseCode
88}
Jon Perritte0f9e4f2016-02-21 21:41:03 -060089
90// ErrDefault503 is the default error type returned on a 503 HTTP response code.
jrperrittb1013232016-02-10 19:01:53 -060091type ErrDefault503 struct {
92 *ErrUnexpectedResponseCode
93}
94
95func (e ErrDefault400) Error() string {
96 return "Invalid request due to incorrect syntax or missing required parameters."
97}
98func (e ErrDefault401) Error() string {
99 return "Authentication failed"
100}
101func (e ErrDefault404) Error() string {
102 return "Resource not found"
103}
104func (e ErrDefault405) Error() string {
105 return "Method not allowed"
106}
107func (e ErrDefault408) Error() string {
108 return "The server timed out waiting for the request"
109}
110func (e ErrDefault429) Error() string {
Jon Perritt256208d2016-02-28 23:38:03 -0600111 return "Too many requests have been sent in a given amount of time. Pause" +
112 " requests, wait up to one minute, and try again."
jrperrittb1013232016-02-10 19:01:53 -0600113}
114func (e ErrDefault500) Error() string {
115 return "Internal Server Error"
116}
117func (e ErrDefault503) Error() string {
Jon Perritt256208d2016-02-28 23:38:03 -0600118 return "The service is currently unable to handle the request due to a temporary" +
119 " overloading or maintenance. This is a temporary condition. Try again later."
jrperrittb1013232016-02-10 19:01:53 -0600120}
121
122// Err400er is the interface resource error types implement to override the error message
123// from a 400 error.
124type Err400er interface {
125 Error400(*ErrUnexpectedResponseCode) error
126}
127
128// Err401er is the interface resource error types implement to override the error message
129// from a 401 error.
130type Err401er interface {
131 Error401(*ErrUnexpectedResponseCode) error
132}
133
134// Err404er is the interface resource error types implement to override the error message
135// from a 404 error.
136type Err404er interface {
137 Error404(*ErrUnexpectedResponseCode) error
138}
139
140// Err405er is the interface resource error types implement to override the error message
141// from a 405 error.
142type Err405er interface {
143 Error405(*ErrUnexpectedResponseCode) error
144}
145
146// Err408er is the interface resource error types implement to override the error message
147// from a 408 error.
148type Err408er interface {
149 Error408(*ErrUnexpectedResponseCode) error
150}
151
152// Err429er is the interface resource error types implement to override the error message
153// from a 429 error.
154type Err429er interface {
155 Error429(*ErrUnexpectedResponseCode) error
156}
157
158// Err500er is the interface resource error types implement to override the error message
159// from a 500 error.
160type Err500er interface {
161 Error500(*ErrUnexpectedResponseCode) error
162}
163
164// Err503er is the interface resource error types implement to override the error message
165// from a 503 error.
166type Err503er interface {
167 Error503(*ErrUnexpectedResponseCode) error
168}
169
Jon Perritte0f9e4f2016-02-21 21:41:03 -0600170// ErrTimeOut is the error type returned when an operations times out.
jrperrittb1013232016-02-10 19:01:53 -0600171type ErrTimeOut struct {
Jon Perritta33da232016-03-02 04:43:08 -0600172 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600173}
174
175func (e *ErrTimeOut) Error() string {
176 return "A time out occurred"
177}
178
Jon Perritte0f9e4f2016-02-21 21:41:03 -0600179// ErrUnableToReauthenticate is the error type returned when reauthentication fails.
jrperrittb1013232016-02-10 19:01:53 -0600180type ErrUnableToReauthenticate struct {
Jon Perritta33da232016-03-02 04:43:08 -0600181 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600182}
183
184func (e *ErrUnableToReauthenticate) Error() string {
185 return fmt.Sprintf("Unable to re-authenticate: %s", e.OriginalError)
186}
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
jrperrittb1013232016-02-10 19:01:53 -0600192}
193
194func (e *ErrErrorAfterReauthentication) Error() string {
195 return fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.OriginalError)
196}
197
198// ErrServiceNotFound is returned when no service in a service catalog matches
199// the provided EndpointOpts. This is generally returned by provider service
200// factory methods like "NewComputeV2()" and can mean that a service is not
201// enabled for your account.
202type ErrServiceNotFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600203 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600204}
205
206func (e *ErrServiceNotFound) Error() string {
207 return "No suitable service could be found in the service catalog."
208}
209
210// ErrEndpointNotFound is returned when no available endpoints match the
211// provided EndpointOpts. This is also generally returned by provider service
212// factory methods, and usually indicates that a region was specified
213// incorrectly.
214type ErrEndpointNotFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600215 BaseError
jrperrittb1013232016-02-10 19:01:53 -0600216}
217
218func (e *ErrEndpointNotFound) Error() string {
219 return "No suitable endpoint could be found in the service catalog."
220}
Jon Perritt256208d2016-02-28 23:38:03 -0600221
222// ErrResourceNotFound is the error when trying to retrieve a resource's
223// ID by name and the resource doesn't exist.
224type ErrResourceNotFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600225 BaseError
226 ID, Name string
Jon Perritt256208d2016-02-28 23:38:03 -0600227 ResourceType string
228}
229
230func (e *ErrResourceNotFound) Error() string {
Jon Perritta33da232016-03-02 04:43:08 -0600231 if e.Name != "" {
232 return fmt.Sprintf("Unable to find %s with name %s", e.ResourceType, e.Name)
233 }
234 return fmt.Sprintf("Unable to find %s with ID %s", e.ResourceType, e.ID)
Jon Perritt256208d2016-02-28 23:38:03 -0600235}
236
237// ErrMultipleResourcesFound is the error when trying to retrieve a resource's
238// ID by name and multiple resources have the user-provided name.
239type ErrMultipleResourcesFound struct {
Jon Perritta33da232016-03-02 04:43:08 -0600240 BaseError
Jon Perritt256208d2016-02-28 23:38:03 -0600241 Name string
242 Count int
243 ResourceType string
244}
245
246func (e *ErrMultipleResourcesFound) Error() string {
Jon Perritta33da232016-03-02 04:43:08 -0600247 return fmt.Sprintf("Found %d %ss matching %s", e.Count, e.ResourceType, e.Name)
Jon Perritt256208d2016-02-28 23:38:03 -0600248}