Add MethodNotSupported error

This commit is contained in:
Clayton Coleman
2015-01-12 00:12:49 -05:00
parent 6cd37637f5
commit a52b216324
3 changed files with 36 additions and 0 deletions

View File

@@ -159,6 +159,19 @@ func NewBadRequest(reason string) error {
}} }}
} }
// NewMethodNotSupported returns an error indicating the requested action is not supported on this kind.
func NewMethodNotSupported(kind, action string) error {
return &StatusError{api.Status{
Status: api.StatusFailure,
Code: http.StatusMethodNotAllowed,
Reason: api.StatusReasonMethodNotAllowed,
Details: &api.StatusDetails{
Kind: kind,
},
Message: fmt.Sprintf("%s is not supported on resources of kind %q", action, kind),
}}
}
// NewInternalError returns an error indicating the item is invalid and cannot be processed. // NewInternalError returns an error indicating the item is invalid and cannot be processed.
func NewInternalError(err error) error { func NewInternalError(err error) error {
return &StatusError{api.Status{ return &StatusError{api.Status{
@@ -192,6 +205,12 @@ func IsInvalid(err error) bool {
return reasonForError(err) == api.StatusReasonInvalid return reasonForError(err) == api.StatusReasonInvalid
} }
// IsMethodNotSupported determines if the err is an error which indicates the provided action could not
// be performed because it is not supported by the server.
func IsMethodNotSupported(err error) bool {
return reasonForError(err) == api.StatusReasonMethodNotAllowed
}
// IsBadRequest determines if err is an error which indicates that the request is invalid. // IsBadRequest determines if err is an error which indicates that the request is invalid.
func IsBadRequest(err error) bool { func IsBadRequest(err error) bool {
return reasonForError(err) == api.StatusReasonBadRequest return reasonForError(err) == api.StatusReasonBadRequest

View File

@@ -40,6 +40,12 @@ func TestErrorNew(t *testing.T) {
if IsInvalid(err) { if IsInvalid(err) {
t.Errorf("expected to not be %s", api.StatusReasonInvalid) t.Errorf("expected to not be %s", api.StatusReasonInvalid)
} }
if IsBadRequest(err) {
t.Errorf("expected to not be %s", api.StatusReasonBadRequest)
}
if IsMethodNotSupported(err) {
t.Errorf("expected to not be %s", api.StatusReasonMethodNotAllowed)
}
if !IsConflict(NewConflict("test", "2", errors.New("message"))) { if !IsConflict(NewConflict("test", "2", errors.New("message"))) {
t.Errorf("expected to be conflict") t.Errorf("expected to be conflict")
@@ -50,6 +56,12 @@ func TestErrorNew(t *testing.T) {
if !IsInvalid(NewInvalid("test", "2", nil)) { if !IsInvalid(NewInvalid("test", "2", nil)) {
t.Errorf("expected to be %s", api.StatusReasonInvalid) t.Errorf("expected to be %s", api.StatusReasonInvalid)
} }
if !IsBadRequest(NewBadRequest("reason")) {
t.Errorf("expected to be %s", api.StatusReasonBadRequest)
}
if !IsMethodNotSupported(NewMethodNotSupported("foo", "delete")) {
t.Errorf("expected to be %s", api.StatusReasonMethodNotAllowed)
}
} }
func TestNewInvalid(t *testing.T) { func TestNewInvalid(t *testing.T) {

View File

@@ -931,6 +931,11 @@ const (
// data was invalid. API calls that return BadRequest can never succeed. // data was invalid. API calls that return BadRequest can never succeed.
StatusReasonBadRequest StatusReason = "BadRequest" StatusReasonBadRequest StatusReason = "BadRequest"
// StatusReasonMethodNotAllowed means that the action the client attempted to perform on the
// resource was not supported by the code - for instance, attempting to delete a resource that
// can only be created. API calls that return MethodNotAllowed can never succeed.
StatusReasonMethodNotAllowed StatusReason = "MethodNotAllowed"
// StatusReasonInternalError indicates that an internal error occurred, it is unexpected // StatusReasonInternalError indicates that an internal error occurred, it is unexpected
// and the outcome of the call is unknown. // and the outcome of the call is unknown.
// Details (optional): // Details (optional):