-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from heartlock/add-errortype
add error type and fix some compile error
- Loading branch information
Showing
3 changed files
with
287 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
package common | ||
|
||
import "fmt" | ||
|
||
// BaseError is an error type that all other error types embed. | ||
type BaseError struct { | ||
DefaultErrString string | ||
Info string | ||
} | ||
|
||
func (e BaseError) Error() string { | ||
e.DefaultErrString = "An error occurred while executing a Gophercloud request." | ||
return e.choseErrString() | ||
} | ||
|
||
func (e BaseError) choseErrString() string { | ||
if e.Info != "" { | ||
return e.Info | ||
} | ||
return e.DefaultErrString | ||
} | ||
|
||
// ErrMissingInput is the error when input is required in a particular | ||
// situation but not provided by the user | ||
type ErrMissingInput struct { | ||
BaseError | ||
Argument string | ||
} | ||
|
||
func (e ErrMissingInput) Error() string { | ||
e.DefaultErrString = fmt.Sprintf("Missing input for argument [%s]", e.Argument) | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrInvalidInput is an error type used for most non-HTTP Gophercloud errors. | ||
type ErrInvalidInput struct { | ||
ErrMissingInput | ||
Value interface{} | ||
} | ||
|
||
func (e ErrInvalidInput) Error() string { | ||
e.DefaultErrString = fmt.Sprintf("Invalid input provided for argument [%s]: [%+v]", e.Argument, e.Value) | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrUnexpectedResponseCode is returned by the Request method when a response code other than | ||
// those listed in OkCodes is encountered. | ||
type ErrUnexpectedResponseCode struct { | ||
BaseError | ||
URL string | ||
Method string | ||
Expected []int | ||
Actual int | ||
Body []byte | ||
} | ||
|
||
func (e ErrUnexpectedResponseCode) Error() string { | ||
e.DefaultErrString = fmt.Sprintf( | ||
"Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s", | ||
e.Expected, e.Method, e.URL, e.Actual, e.Body, | ||
) | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrDefault400 is the default error type returned on a 400 HTTP response code. | ||
type ErrDefault400 struct { | ||
ErrUnexpectedResponseCode | ||
} | ||
|
||
// ErrDefault401 is the default error type returned on a 401 HTTP response code. | ||
type ErrDefault401 struct { | ||
ErrUnexpectedResponseCode | ||
} | ||
|
||
// ErrDefault404 is the default error type returned on a 404 HTTP response code. | ||
type ErrDefault404 struct { | ||
ErrUnexpectedResponseCode | ||
} | ||
|
||
// ErrDefault405 is the default error type returned on a 405 HTTP response code. | ||
type ErrDefault405 struct { | ||
ErrUnexpectedResponseCode | ||
} | ||
|
||
// ErrDefault408 is the default error type returned on a 408 HTTP response code. | ||
type ErrDefault408 struct { | ||
ErrUnexpectedResponseCode | ||
} | ||
|
||
// ErrDefault429 is the default error type returned on a 429 HTTP response code. | ||
type ErrDefault429 struct { | ||
ErrUnexpectedResponseCode | ||
} | ||
|
||
// ErrDefault500 is the default error type returned on a 500 HTTP response code. | ||
type ErrDefault500 struct { | ||
ErrUnexpectedResponseCode | ||
} | ||
|
||
// ErrDefault503 is the default error type returned on a 503 HTTP response code. | ||
type ErrDefault503 struct { | ||
ErrUnexpectedResponseCode | ||
} | ||
|
||
func (e ErrDefault400) Error() string { | ||
return "Invalid request due to incorrect syntax or missing required parameters." | ||
} | ||
func (e ErrDefault401) Error() string { | ||
return "Authentication failed" | ||
} | ||
func (e ErrDefault404) Error() string { | ||
return "Resource not found" | ||
} | ||
func (e ErrDefault405) Error() string { | ||
return "Method not allowed" | ||
} | ||
func (e ErrDefault408) Error() string { | ||
return "The server timed out waiting for the request" | ||
} | ||
func (e ErrDefault429) Error() string { | ||
return "Too many requests have been sent in a given amount of time. Pause" + | ||
" requests, wait up to one minute, and try again." | ||
} | ||
func (e ErrDefault500) Error() string { | ||
return "Internal Server Error" | ||
} | ||
func (e ErrDefault503) Error() string { | ||
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." | ||
} | ||
|
||
// Err400er is the interface resource error types implement to override the error message | ||
// from a 400 error. | ||
type Err400er interface { | ||
Error400(ErrUnexpectedResponseCode) error | ||
} | ||
|
||
// Err401er is the interface resource error types implement to override the error message | ||
// from a 401 error. | ||
type Err401er interface { | ||
Error401(ErrUnexpectedResponseCode) error | ||
} | ||
|
||
// Err404er is the interface resource error types implement to override the error message | ||
// from a 404 error. | ||
type Err404er interface { | ||
Error404(ErrUnexpectedResponseCode) error | ||
} | ||
|
||
// Err405er is the interface resource error types implement to override the error message | ||
// from a 405 error. | ||
type Err405er interface { | ||
Error405(ErrUnexpectedResponseCode) error | ||
} | ||
|
||
// Err408er is the interface resource error types implement to override the error message | ||
// from a 408 error. | ||
type Err408er interface { | ||
Error408(ErrUnexpectedResponseCode) error | ||
} | ||
|
||
// Err429er is the interface resource error types implement to override the error message | ||
// from a 429 error. | ||
type Err429er interface { | ||
Error429(ErrUnexpectedResponseCode) error | ||
} | ||
|
||
// Err500er is the interface resource error types implement to override the error message | ||
// from a 500 error. | ||
type Err500er interface { | ||
Error500(ErrUnexpectedResponseCode) error | ||
} | ||
|
||
// Err503er is the interface resource error types implement to override the error message | ||
// from a 503 error. | ||
type Err503er interface { | ||
Error503(ErrUnexpectedResponseCode) error | ||
} | ||
|
||
// ErrTimeOut is the error type returned when an operations times out. | ||
type ErrTimeOut struct { | ||
BaseError | ||
} | ||
|
||
func (e ErrTimeOut) Error() string { | ||
e.DefaultErrString = "A time out occurred" | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrUnableToReauthenticate is the error type returned when reauthentication fails. | ||
type ErrUnableToReauthenticate struct { | ||
BaseError | ||
ErrOriginal error | ||
} | ||
|
||
func (e ErrUnableToReauthenticate) Error() string { | ||
e.DefaultErrString = fmt.Sprintf("Unable to re-authenticate: %s", e.ErrOriginal) | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrErrorAfterReauthentication is the error type returned when reauthentication | ||
// succeeds, but an error occurs afterword (usually an HTTP error). | ||
type ErrErrorAfterReauthentication struct { | ||
BaseError | ||
ErrOriginal error | ||
} | ||
|
||
func (e ErrErrorAfterReauthentication) Error() string { | ||
e.DefaultErrString = fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.ErrOriginal) | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrServiceNotFound is returned when no service in a service catalog matches | ||
// the provided EndpointOpts. This is generally returned by provider service | ||
// factory methods like "NewComputeV2()" and can mean that a service is not | ||
// enabled for your account. | ||
type ErrServiceNotFound struct { | ||
BaseError | ||
} | ||
|
||
func (e ErrServiceNotFound) Error() string { | ||
e.DefaultErrString = "No suitable service could be found in the service catalog." | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrEndpointNotFound is returned when no available endpoints match the | ||
// provided EndpointOpts. This is also generally returned by provider service | ||
// factory methods, and usually indicates that a region was specified | ||
// incorrectly. | ||
type ErrEndpointNotFound struct { | ||
BaseError | ||
} | ||
|
||
func (e ErrEndpointNotFound) Error() string { | ||
e.DefaultErrString = "No suitable endpoint could be found in the service catalog." | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrResourceNotFound is the error when trying to retrieve a resource's | ||
// ID by name and the resource doesn't exist. | ||
type ErrResourceNotFound struct { | ||
BaseError | ||
Name string | ||
ResourceType string | ||
} | ||
|
||
func (e ErrResourceNotFound) Error() string { | ||
e.DefaultErrString = fmt.Sprintf("Unable to find %s with name %s", e.ResourceType, e.Name) | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrMultipleResourcesFound is the error when trying to retrieve a resource's | ||
// ID by name and multiple resources have the user-provided name. | ||
type ErrMultipleResourcesFound struct { | ||
BaseError | ||
Name string | ||
Count int | ||
ResourceType string | ||
} | ||
|
||
func (e ErrMultipleResourcesFound) Error() string { | ||
e.DefaultErrString = fmt.Sprintf("Found %d %ss matching %s", e.Count, e.ResourceType, e.Name) | ||
return e.choseErrString() | ||
} | ||
|
||
// ErrUnexpectedType is the error when an unexpected type is encountered | ||
type ErrUnexpectedType struct { | ||
BaseError | ||
Expected string | ||
Actual string | ||
} | ||
|
||
func (e ErrUnexpectedType) Error() string { | ||
e.DefaultErrString = fmt.Sprintf("Expected %s but got %s", e.Expected, e.Actual) | ||
return e.choseErrString() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters