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 { |
| 7 | OriginalError string |
| 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 { |
| 17 | BaseError |
| 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 { |
| 29 | BaseError |
| 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 | |
| 44 | type ErrDefault400 struct { |
| 45 | *ErrUnexpectedResponseCode |
| 46 | } |
| 47 | type ErrDefault401 struct { |
| 48 | *ErrUnexpectedResponseCode |
| 49 | } |
| 50 | type ErrDefault404 struct { |
| 51 | *ErrUnexpectedResponseCode |
| 52 | } |
| 53 | type ErrDefault405 struct { |
| 54 | *ErrUnexpectedResponseCode |
| 55 | } |
| 56 | type ErrDefault408 struct { |
| 57 | *ErrUnexpectedResponseCode |
| 58 | } |
| 59 | type ErrDefault429 struct { |
| 60 | *ErrUnexpectedResponseCode |
| 61 | } |
| 62 | type ErrDefault500 struct { |
| 63 | *ErrUnexpectedResponseCode |
| 64 | } |
| 65 | type ErrDefault503 struct { |
| 66 | *ErrUnexpectedResponseCode |
| 67 | } |
| 68 | |
| 69 | func (e ErrDefault400) Error() string { |
| 70 | return "Invalid request due to incorrect syntax or missing required parameters." |
| 71 | } |
| 72 | func (e ErrDefault401) Error() string { |
| 73 | return "Authentication failed" |
| 74 | } |
| 75 | func (e ErrDefault404) Error() string { |
| 76 | return "Resource not found" |
| 77 | } |
| 78 | func (e ErrDefault405) Error() string { |
| 79 | return "Method not allowed" |
| 80 | } |
| 81 | func (e ErrDefault408) Error() string { |
| 82 | return "The server timed out waiting for the request" |
| 83 | } |
| 84 | func (e ErrDefault429) Error() string { |
| 85 | return "Too many requests have been sent in a given amount of time. Pause requests, wait up to one minute, and try again." |
| 86 | } |
| 87 | func (e ErrDefault500) Error() string { |
| 88 | return "Internal Server Error" |
| 89 | } |
| 90 | func (e ErrDefault503) Error() string { |
| 91 | 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." |
| 92 | } |
| 93 | |
| 94 | // Err400er is the interface resource error types implement to override the error message |
| 95 | // from a 400 error. |
| 96 | type Err400er interface { |
| 97 | Error400(*ErrUnexpectedResponseCode) error |
| 98 | } |
| 99 | |
| 100 | // Err401er is the interface resource error types implement to override the error message |
| 101 | // from a 401 error. |
| 102 | type Err401er interface { |
| 103 | Error401(*ErrUnexpectedResponseCode) error |
| 104 | } |
| 105 | |
| 106 | // Err404er is the interface resource error types implement to override the error message |
| 107 | // from a 404 error. |
| 108 | type Err404er interface { |
| 109 | Error404(*ErrUnexpectedResponseCode) error |
| 110 | } |
| 111 | |
| 112 | // Err405er is the interface resource error types implement to override the error message |
| 113 | // from a 405 error. |
| 114 | type Err405er interface { |
| 115 | Error405(*ErrUnexpectedResponseCode) error |
| 116 | } |
| 117 | |
| 118 | // Err408er is the interface resource error types implement to override the error message |
| 119 | // from a 408 error. |
| 120 | type Err408er interface { |
| 121 | Error408(*ErrUnexpectedResponseCode) error |
| 122 | } |
| 123 | |
| 124 | // Err429er is the interface resource error types implement to override the error message |
| 125 | // from a 429 error. |
| 126 | type Err429er interface { |
| 127 | Error429(*ErrUnexpectedResponseCode) error |
| 128 | } |
| 129 | |
| 130 | // Err500er is the interface resource error types implement to override the error message |
| 131 | // from a 500 error. |
| 132 | type Err500er interface { |
| 133 | Error500(*ErrUnexpectedResponseCode) error |
| 134 | } |
| 135 | |
| 136 | // Err503er is the interface resource error types implement to override the error message |
| 137 | // from a 503 error. |
| 138 | type Err503er interface { |
| 139 | Error503(*ErrUnexpectedResponseCode) error |
| 140 | } |
| 141 | |
| 142 | type ErrTimeOut struct { |
| 143 | *BaseError |
| 144 | } |
| 145 | |
| 146 | func (e *ErrTimeOut) Error() string { |
| 147 | return "A time out occurred" |
| 148 | } |
| 149 | |
| 150 | type ErrUnableToReauthenticate struct { |
| 151 | *BaseError |
| 152 | } |
| 153 | |
| 154 | func (e *ErrUnableToReauthenticate) Error() string { |
| 155 | return fmt.Sprintf("Unable to re-authenticate: %s", e.OriginalError) |
| 156 | } |
| 157 | |
| 158 | type ErrErrorAfterReauthentication struct { |
| 159 | *BaseError |
| 160 | } |
| 161 | |
| 162 | func (e *ErrErrorAfterReauthentication) Error() string { |
| 163 | return fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.OriginalError) |
| 164 | } |
| 165 | |
| 166 | // ErrServiceNotFound is returned when no service in a service catalog matches |
| 167 | // the provided EndpointOpts. This is generally returned by provider service |
| 168 | // factory methods like "NewComputeV2()" and can mean that a service is not |
| 169 | // enabled for your account. |
| 170 | type ErrServiceNotFound struct { |
| 171 | *BaseError |
| 172 | } |
| 173 | |
| 174 | func (e *ErrServiceNotFound) Error() string { |
| 175 | return "No suitable service could be found in the service catalog." |
| 176 | } |
| 177 | |
| 178 | // ErrEndpointNotFound is returned when no available endpoints match the |
| 179 | // provided EndpointOpts. This is also generally returned by provider service |
| 180 | // factory methods, and usually indicates that a region was specified |
| 181 | // incorrectly. |
| 182 | type ErrEndpointNotFound struct { |
| 183 | *BaseError |
| 184 | } |
| 185 | |
| 186 | func (e *ErrEndpointNotFound) Error() string { |
| 187 | return "No suitable endpoint could be found in the service catalog." |
| 188 | } |