jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | // BaseError is an error type that all other error types embed. |
| 6 | type BaseError struct { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 7 | DefaultErrString string |
| 8 | Info string |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 9 | } |
| 10 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 11 | func (e BaseError) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 12 | e.DefaultErrString = "An error occurred while executing a Gophercloud request." |
| 13 | return e.choseErrString() |
| 14 | } |
| 15 | |
| 16 | func (e BaseError) choseErrString() string { |
| 17 | if e.Info != "" { |
| 18 | return e.Info |
| 19 | } |
| 20 | return e.DefaultErrString |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 21 | } |
| 22 | |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 23 | // ErrMissingInput is the error when input is required in a particular |
| 24 | // situation but not provided by the user |
| 25 | type ErrMissingInput struct { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 26 | BaseError |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 27 | Argument string |
| 28 | } |
| 29 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 30 | func (e ErrMissingInput) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 31 | e.DefaultErrString = fmt.Sprintf("Missing input for argument [%s]", e.Argument) |
| 32 | return e.choseErrString() |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 33 | } |
| 34 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 35 | // ErrInvalidInput is an error type used for most non-HTTP Gophercloud errors. |
| 36 | type ErrInvalidInput struct { |
| 37 | ErrMissingInput |
| 38 | Value interface{} |
| 39 | } |
| 40 | |
| 41 | func (e ErrInvalidInput) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 42 | e.DefaultErrString = fmt.Sprintf("Invalid input provided for argument [%s]: [%+v]", e.Argument, e.Value) |
| 43 | return e.choseErrString() |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 44 | } |
| 45 | |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 46 | // ErrUnexpectedResponseCode is returned by the Request method when a response code other than |
| 47 | // those listed in OkCodes is encountered. |
| 48 | type ErrUnexpectedResponseCode struct { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 49 | BaseError |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 50 | URL string |
| 51 | Method string |
| 52 | Expected []int |
| 53 | Actual int |
| 54 | Body []byte |
| 55 | } |
| 56 | |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 57 | func (e ErrUnexpectedResponseCode) Error() string { |
| 58 | e.DefaultErrString = fmt.Sprintf( |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 59 | "Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s", |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 60 | e.Expected, e.Method, e.URL, e.Actual, e.Body, |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 61 | ) |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 62 | return e.choseErrString() |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 65 | // ErrDefault400 is the default error type returned on a 400 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 66 | type ErrDefault400 struct { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 67 | ErrUnexpectedResponseCode |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 68 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 69 | |
| 70 | // ErrDefault401 is the default error type returned on a 401 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 71 | type ErrDefault401 struct { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 72 | ErrUnexpectedResponseCode |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 73 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 74 | |
| 75 | // ErrDefault404 is the default error type returned on a 404 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 76 | type ErrDefault404 struct { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 77 | ErrUnexpectedResponseCode |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 78 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 79 | |
| 80 | // ErrDefault405 is the default error type returned on a 405 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 81 | type ErrDefault405 struct { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 82 | ErrUnexpectedResponseCode |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 83 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 84 | |
| 85 | // ErrDefault408 is the default error type returned on a 408 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 86 | type ErrDefault408 struct { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 87 | ErrUnexpectedResponseCode |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 88 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 89 | |
| 90 | // ErrDefault429 is the default error type returned on a 429 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 91 | type ErrDefault429 struct { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 92 | ErrUnexpectedResponseCode |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 93 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 94 | |
| 95 | // ErrDefault500 is the default error type returned on a 500 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 96 | type ErrDefault500 struct { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 97 | ErrUnexpectedResponseCode |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 98 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 99 | |
| 100 | // ErrDefault503 is the default error type returned on a 503 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 101 | type ErrDefault503 struct { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 102 | ErrUnexpectedResponseCode |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func (e ErrDefault400) Error() string { |
| 106 | return "Invalid request due to incorrect syntax or missing required parameters." |
| 107 | } |
| 108 | func (e ErrDefault401) Error() string { |
| 109 | return "Authentication failed" |
| 110 | } |
| 111 | func (e ErrDefault404) Error() string { |
| 112 | return "Resource not found" |
| 113 | } |
| 114 | func (e ErrDefault405) Error() string { |
| 115 | return "Method not allowed" |
| 116 | } |
| 117 | func (e ErrDefault408) Error() string { |
| 118 | return "The server timed out waiting for the request" |
| 119 | } |
| 120 | func (e ErrDefault429) Error() string { |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 121 | return "Too many requests have been sent in a given amount of time. Pause" + |
| 122 | " requests, wait up to one minute, and try again." |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 123 | } |
| 124 | func (e ErrDefault500) Error() string { |
| 125 | return "Internal Server Error" |
| 126 | } |
| 127 | func (e ErrDefault503) Error() string { |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 128 | 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." |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Err400er is the interface resource error types implement to override the error message |
| 133 | // from a 400 error. |
| 134 | type Err400er interface { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 135 | Error400(ErrUnexpectedResponseCode) error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Err401er is the interface resource error types implement to override the error message |
| 139 | // from a 401 error. |
| 140 | type Err401er interface { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 141 | Error401(ErrUnexpectedResponseCode) error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Err404er is the interface resource error types implement to override the error message |
| 145 | // from a 404 error. |
| 146 | type Err404er interface { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 147 | Error404(ErrUnexpectedResponseCode) error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Err405er is the interface resource error types implement to override the error message |
| 151 | // from a 405 error. |
| 152 | type Err405er interface { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 153 | Error405(ErrUnexpectedResponseCode) error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Err408er is the interface resource error types implement to override the error message |
| 157 | // from a 408 error. |
| 158 | type Err408er interface { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 159 | Error408(ErrUnexpectedResponseCode) error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Err429er is the interface resource error types implement to override the error message |
| 163 | // from a 429 error. |
| 164 | type Err429er interface { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 165 | Error429(ErrUnexpectedResponseCode) error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Err500er is the interface resource error types implement to override the error message |
| 169 | // from a 500 error. |
| 170 | type Err500er interface { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 171 | Error500(ErrUnexpectedResponseCode) error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Err503er is the interface resource error types implement to override the error message |
| 175 | // from a 503 error. |
| 176 | type Err503er interface { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 177 | Error503(ErrUnexpectedResponseCode) error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 178 | } |
| 179 | |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 180 | // ErrTimeOut is the error type returned when an operations times out. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 181 | type ErrTimeOut struct { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 182 | BaseError |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 183 | } |
| 184 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 185 | func (e ErrTimeOut) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 186 | e.DefaultErrString = "A time out occurred" |
| 187 | return e.choseErrString() |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 188 | } |
| 189 | |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 190 | // ErrUnableToReauthenticate is the error type returned when reauthentication fails. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 191 | type ErrUnableToReauthenticate struct { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 192 | BaseError |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 193 | ErrOriginal error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 194 | } |
| 195 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 196 | func (e ErrUnableToReauthenticate) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 197 | e.DefaultErrString = fmt.Sprintf("Unable to re-authenticate: %s", e.ErrOriginal) |
| 198 | return e.choseErrString() |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 199 | } |
| 200 | |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame] | 201 | // ErrErrorAfterReauthentication is the error type returned when reauthentication |
| 202 | // succeeds, but an error occurs afterword (usually an HTTP error). |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 203 | type ErrErrorAfterReauthentication struct { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 204 | BaseError |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 205 | ErrOriginal error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 206 | } |
| 207 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 208 | func (e ErrErrorAfterReauthentication) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 209 | e.DefaultErrString = fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.ErrOriginal) |
| 210 | return e.choseErrString() |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 211 | } |
| 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. |
| 217 | type ErrServiceNotFound struct { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 218 | BaseError |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 219 | } |
| 220 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 221 | func (e ErrServiceNotFound) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 222 | e.DefaultErrString = "No suitable service could be found in the service catalog." |
| 223 | return e.choseErrString() |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 224 | } |
| 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. |
| 230 | type ErrEndpointNotFound struct { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 231 | BaseError |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 232 | } |
| 233 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 234 | func (e ErrEndpointNotFound) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 235 | e.DefaultErrString = "No suitable endpoint could be found in the service catalog." |
| 236 | return e.choseErrString() |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 237 | } |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 238 | |
| 239 | // ErrResourceNotFound is the error when trying to retrieve a resource's |
| 240 | // ID by name and the resource doesn't exist. |
| 241 | type ErrResourceNotFound struct { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 242 | BaseError |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 243 | Name string |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 244 | ResourceType string |
| 245 | } |
| 246 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 247 | func (e ErrResourceNotFound) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 248 | e.DefaultErrString = fmt.Sprintf("Unable to find %s with name %s", e.ResourceType, e.Name) |
| 249 | return e.choseErrString() |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 250 | } |
| 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. |
| 254 | type ErrMultipleResourcesFound struct { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 255 | BaseError |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 256 | Name string |
| 257 | Count int |
| 258 | ResourceType string |
| 259 | } |
| 260 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 261 | func (e ErrMultipleResourcesFound) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 262 | e.DefaultErrString = fmt.Sprintf("Found %d %ss matching %s", e.Count, e.ResourceType, e.Name) |
| 263 | return e.choseErrString() |
Jon Perritt | 256208d | 2016-02-28 23:38:03 -0600 | [diff] [blame] | 264 | } |
Jon Perritt | 8025197 | 2016-03-09 00:32:30 -0600 | [diff] [blame] | 265 | |
| 266 | // ErrUnexpectedType is the error when an unexpected type is encountered |
| 267 | type ErrUnexpectedType struct { |
| 268 | BaseError |
| 269 | Expected string |
| 270 | Actual string |
| 271 | } |
| 272 | |
| 273 | func (e ErrUnexpectedType) Error() string { |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 274 | e.DefaultErrString = fmt.Sprintf("Expected %s but got %s", e.Expected, e.Actual) |
| 275 | return e.choseErrString() |
Jon Perritt | 8025197 | 2016-03-09 00:32:30 -0600 | [diff] [blame] | 276 | } |