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