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 | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 7 | OriginalError error |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 8 | Function string |
| 9 | } |
| 10 | |
| 11 | func (e *BaseError) Error() string { |
| 12 | return "An error occurred while executing a Gophercloud request." |
| 13 | } |
| 14 | |
| 15 | // ErrInvalidInput is an error type used for most non-HTTP Gophercloud errors. |
| 16 | type ErrInvalidInput struct { |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 17 | *BaseError |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 18 | Argument string |
| 19 | Value interface{} |
| 20 | } |
| 21 | |
| 22 | func (e *ErrInvalidInput) Error() string { |
| 23 | return fmt.Sprintf("Invalid input provided for argument [%s]: [%+v]", e.Argument, e.Value) |
| 24 | } |
| 25 | |
| 26 | // ErrUnexpectedResponseCode is returned by the Request method when a response code other than |
| 27 | // those listed in OkCodes is encountered. |
| 28 | type ErrUnexpectedResponseCode struct { |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 29 | *BaseError |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 30 | URL string |
| 31 | Method string |
| 32 | Expected []int |
| 33 | Actual int |
| 34 | Body []byte |
| 35 | } |
| 36 | |
| 37 | func (err *ErrUnexpectedResponseCode) Error() string { |
| 38 | return fmt.Sprintf( |
| 39 | "Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s", |
| 40 | err.Expected, err.Method, err.URL, err.Actual, err.Body, |
| 41 | ) |
| 42 | } |
| 43 | |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 44 | // ErrDefault400 is the default error type returned on a 400 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 45 | type ErrDefault400 struct { |
| 46 | *ErrUnexpectedResponseCode |
| 47 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 48 | |
| 49 | // ErrDefault401 is the default error type returned on a 401 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 50 | type ErrDefault401 struct { |
| 51 | *ErrUnexpectedResponseCode |
| 52 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 53 | |
| 54 | // ErrDefault404 is the default error type returned on a 404 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 55 | type ErrDefault404 struct { |
| 56 | *ErrUnexpectedResponseCode |
| 57 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 58 | |
| 59 | // ErrDefault405 is the default error type returned on a 405 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 60 | type ErrDefault405 struct { |
| 61 | *ErrUnexpectedResponseCode |
| 62 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 63 | |
| 64 | // ErrDefault408 is the default error type returned on a 408 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 65 | type ErrDefault408 struct { |
| 66 | *ErrUnexpectedResponseCode |
| 67 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 68 | |
| 69 | // ErrDefault429 is the default error type returned on a 429 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 70 | type ErrDefault429 struct { |
| 71 | *ErrUnexpectedResponseCode |
| 72 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 73 | |
| 74 | // ErrDefault500 is the default error type returned on a 500 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 75 | type ErrDefault500 struct { |
| 76 | *ErrUnexpectedResponseCode |
| 77 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 78 | |
| 79 | // ErrDefault503 is the default error type returned on a 503 HTTP response code. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 80 | type ErrDefault503 struct { |
| 81 | *ErrUnexpectedResponseCode |
| 82 | } |
| 83 | |
| 84 | func (e ErrDefault400) Error() string { |
| 85 | return "Invalid request due to incorrect syntax or missing required parameters." |
| 86 | } |
| 87 | func (e ErrDefault401) Error() string { |
| 88 | return "Authentication failed" |
| 89 | } |
| 90 | func (e ErrDefault404) Error() string { |
| 91 | return "Resource not found" |
| 92 | } |
| 93 | func (e ErrDefault405) Error() string { |
| 94 | return "Method not allowed" |
| 95 | } |
| 96 | func (e ErrDefault408) Error() string { |
| 97 | return "The server timed out waiting for the request" |
| 98 | } |
| 99 | func (e ErrDefault429) Error() string { |
| 100 | return "Too many requests have been sent in a given amount of time. Pause requests, wait up to one minute, and try again." |
| 101 | } |
| 102 | func (e ErrDefault500) Error() string { |
| 103 | return "Internal Server Error" |
| 104 | } |
| 105 | func (e ErrDefault503) Error() string { |
| 106 | return "The service is currently unable to handle the request due to a temporary overloading or maintenance. This is a temporary condition. Try again later." |
| 107 | } |
| 108 | |
| 109 | // Err400er is the interface resource error types implement to override the error message |
| 110 | // from a 400 error. |
| 111 | type Err400er interface { |
| 112 | Error400(*ErrUnexpectedResponseCode) error |
| 113 | } |
| 114 | |
| 115 | // Err401er is the interface resource error types implement to override the error message |
| 116 | // from a 401 error. |
| 117 | type Err401er interface { |
| 118 | Error401(*ErrUnexpectedResponseCode) error |
| 119 | } |
| 120 | |
| 121 | // Err404er is the interface resource error types implement to override the error message |
| 122 | // from a 404 error. |
| 123 | type Err404er interface { |
| 124 | Error404(*ErrUnexpectedResponseCode) error |
| 125 | } |
| 126 | |
| 127 | // Err405er is the interface resource error types implement to override the error message |
| 128 | // from a 405 error. |
| 129 | type Err405er interface { |
| 130 | Error405(*ErrUnexpectedResponseCode) error |
| 131 | } |
| 132 | |
| 133 | // Err408er is the interface resource error types implement to override the error message |
| 134 | // from a 408 error. |
| 135 | type Err408er interface { |
| 136 | Error408(*ErrUnexpectedResponseCode) error |
| 137 | } |
| 138 | |
| 139 | // Err429er is the interface resource error types implement to override the error message |
| 140 | // from a 429 error. |
| 141 | type Err429er interface { |
| 142 | Error429(*ErrUnexpectedResponseCode) error |
| 143 | } |
| 144 | |
| 145 | // Err500er is the interface resource error types implement to override the error message |
| 146 | // from a 500 error. |
| 147 | type Err500er interface { |
| 148 | Error500(*ErrUnexpectedResponseCode) error |
| 149 | } |
| 150 | |
| 151 | // Err503er is the interface resource error types implement to override the error message |
| 152 | // from a 503 error. |
| 153 | type Err503er interface { |
| 154 | Error503(*ErrUnexpectedResponseCode) error |
| 155 | } |
| 156 | |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 157 | // ErrTimeOut is the error type returned when an operations times out. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 158 | type ErrTimeOut struct { |
| 159 | *BaseError |
| 160 | } |
| 161 | |
| 162 | func (e *ErrTimeOut) Error() string { |
| 163 | return "A time out occurred" |
| 164 | } |
| 165 | |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 166 | // ErrUnableToReauthenticate is the error type returned when reauthentication fails. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 167 | type ErrUnableToReauthenticate struct { |
| 168 | *BaseError |
| 169 | } |
| 170 | |
| 171 | func (e *ErrUnableToReauthenticate) Error() string { |
| 172 | return fmt.Sprintf("Unable to re-authenticate: %s", e.OriginalError) |
| 173 | } |
| 174 | |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 175 | // ErrErrorAfterReauthentication is the error type returned when reauthentication |
| 176 | // succeeds, but an error occurs afterword (usually an HTTP error). |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 177 | type ErrErrorAfterReauthentication struct { |
| 178 | *BaseError |
| 179 | } |
| 180 | |
| 181 | func (e *ErrErrorAfterReauthentication) Error() string { |
| 182 | return fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.OriginalError) |
| 183 | } |
| 184 | |
| 185 | // ErrServiceNotFound is returned when no service in a service catalog matches |
| 186 | // the provided EndpointOpts. This is generally returned by provider service |
| 187 | // factory methods like "NewComputeV2()" and can mean that a service is not |
| 188 | // enabled for your account. |
| 189 | type ErrServiceNotFound struct { |
| 190 | *BaseError |
| 191 | } |
| 192 | |
| 193 | func (e *ErrServiceNotFound) Error() string { |
| 194 | return "No suitable service could be found in the service catalog." |
| 195 | } |
| 196 | |
| 197 | // ErrEndpointNotFound is returned when no available endpoints match the |
| 198 | // provided EndpointOpts. This is also generally returned by provider service |
| 199 | // factory methods, and usually indicates that a region was specified |
| 200 | // incorrectly. |
| 201 | type ErrEndpointNotFound struct { |
| 202 | *BaseError |
| 203 | } |
| 204 | |
| 205 | func (e *ErrEndpointNotFound) Error() string { |
| 206 | return "No suitable endpoint could be found in the service catalog." |
| 207 | } |