mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-06 07:57:35 +00:00
Errors should be part of api/errors, not apiserver
Renames constructor methods to be errors.New<name> which changed a few places.
This commit is contained in:
134
pkg/api/errors/errors.go
Normal file
134
pkg/api/errors/errors.go
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
// statusError is an error intended for consumption by a REST API server.
|
||||
type statusError struct {
|
||||
status api.Status
|
||||
}
|
||||
|
||||
// Error implements the Error interface.
|
||||
func (e *statusError) Error() string {
|
||||
return e.status.Message
|
||||
}
|
||||
|
||||
// Status converts this error into an api.Status object.
|
||||
func (e *statusError) Status() api.Status {
|
||||
return e.status
|
||||
}
|
||||
|
||||
// NewNotFound returns a new error which indicates that the resource of the kind and the name was not found.
|
||||
func NewNotFound(kind, name string) error {
|
||||
return &statusError{api.Status{
|
||||
Status: api.StatusFailure,
|
||||
Code: http.StatusNotFound,
|
||||
Reason: api.StatusReasonNotFound,
|
||||
Details: &api.StatusDetails{
|
||||
Kind: kind,
|
||||
ID: name,
|
||||
},
|
||||
Message: fmt.Sprintf("%s %q not found", kind, name),
|
||||
}}
|
||||
}
|
||||
|
||||
// NewAlreadyExists returns an error indicating the item requested exists by that identifier.
|
||||
func NewAlreadyExists(kind, name string) error {
|
||||
return &statusError{api.Status{
|
||||
Status: api.StatusFailure,
|
||||
Code: http.StatusConflict,
|
||||
Reason: api.StatusReasonAlreadyExists,
|
||||
Details: &api.StatusDetails{
|
||||
Kind: kind,
|
||||
ID: name,
|
||||
},
|
||||
Message: fmt.Sprintf("%s %q already exists", kind, name),
|
||||
}}
|
||||
}
|
||||
|
||||
// NewConflict returns an error indicating the item can't be updated as provided.
|
||||
func NewConflict(kind, name string, err error) error {
|
||||
return &statusError{api.Status{
|
||||
Status: api.StatusFailure,
|
||||
Code: http.StatusConflict,
|
||||
Reason: api.StatusReasonConflict,
|
||||
Details: &api.StatusDetails{
|
||||
Kind: kind,
|
||||
ID: name,
|
||||
},
|
||||
Message: fmt.Sprintf("%s %q cannot be updated: %s", kind, name, err),
|
||||
}}
|
||||
}
|
||||
|
||||
// NewInvalid returns an error indicating the item is invalid and cannot be processed.
|
||||
func NewInvalid(kind, name string, errs ErrorList) error {
|
||||
causes := make([]api.StatusCause, 0, len(errs))
|
||||
for i := range errs {
|
||||
if err, ok := errs[i].(ValidationError); ok {
|
||||
causes = append(causes, api.StatusCause{
|
||||
Type: api.CauseType(err.Type),
|
||||
Message: err.Error(),
|
||||
Field: err.Field,
|
||||
})
|
||||
}
|
||||
}
|
||||
return &statusError{api.Status{
|
||||
Status: api.StatusFailure,
|
||||
Code: 422, // RFC 4918
|
||||
Reason: api.StatusReasonInvalid,
|
||||
Details: &api.StatusDetails{
|
||||
Kind: kind,
|
||||
ID: name,
|
||||
Causes: causes,
|
||||
},
|
||||
Message: fmt.Sprintf("%s %q is invalid: %s", kind, name, errs.ToError()),
|
||||
}}
|
||||
}
|
||||
|
||||
// IsNotFound returns true if the specified error was created by NewNotFoundErr.
|
||||
func IsNotFound(err error) bool {
|
||||
return reasonForError(err) == api.StatusReasonNotFound
|
||||
}
|
||||
|
||||
// IsAlreadyExists determines if the err is an error which indicates that a specified resource already exists.
|
||||
func IsAlreadyExists(err error) bool {
|
||||
return reasonForError(err) == api.StatusReasonAlreadyExists
|
||||
}
|
||||
|
||||
// IsConflict determines if the err is an error which indicates the provided update conflicts.
|
||||
func IsConflict(err error) bool {
|
||||
return reasonForError(err) == api.StatusReasonConflict
|
||||
}
|
||||
|
||||
// IsInvalid determines if the err is an error which indicates the provided resource is not valid.
|
||||
func IsInvalid(err error) bool {
|
||||
return reasonForError(err) == api.StatusReasonInvalid
|
||||
}
|
||||
|
||||
func reasonForError(err error) api.StatusReason {
|
||||
switch t := err.(type) {
|
||||
case *statusError:
|
||||
return t.status.Reason
|
||||
}
|
||||
return api.StatusReasonUnknown
|
||||
}
|
||||
133
pkg/api/errors/errors_test.go
Normal file
133
pkg/api/errors/errors_test.go
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
func TestErrorNew(t *testing.T) {
|
||||
err := NewAlreadyExists("test", "1")
|
||||
if !IsAlreadyExists(err) {
|
||||
t.Errorf("expected to be already_exists")
|
||||
}
|
||||
if IsConflict(err) {
|
||||
t.Errorf("expected to not be confict")
|
||||
}
|
||||
if IsNotFound(err) {
|
||||
t.Errorf(fmt.Sprintf("expected to not be %s", api.StatusReasonNotFound))
|
||||
}
|
||||
if IsInvalid(err) {
|
||||
t.Errorf("expected to not be invalid")
|
||||
}
|
||||
|
||||
if !IsConflict(NewConflict("test", "2", errors.New("message"))) {
|
||||
t.Errorf("expected to be conflict")
|
||||
}
|
||||
if !IsNotFound(NewNotFound("test", "3")) {
|
||||
t.Errorf("expected to be not found")
|
||||
}
|
||||
if !IsInvalid(NewInvalid("test", "2", nil)) {
|
||||
t.Errorf("expected to be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInvalid(t *testing.T) {
|
||||
testCases := []struct {
|
||||
Err ValidationError
|
||||
Details *api.StatusDetails
|
||||
}{
|
||||
{
|
||||
NewFieldDuplicate("field[0].name", "bar"),
|
||||
&api.StatusDetails{
|
||||
Kind: "kind",
|
||||
ID: "name",
|
||||
Causes: []api.StatusCause{{
|
||||
Type: api.CauseTypeFieldValueDuplicate,
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
NewFieldInvalid("field[0].name", "bar"),
|
||||
&api.StatusDetails{
|
||||
Kind: "kind",
|
||||
ID: "name",
|
||||
Causes: []api.StatusCause{{
|
||||
Type: api.CauseTypeFieldValueInvalid,
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
NewFieldNotFound("field[0].name", "bar"),
|
||||
&api.StatusDetails{
|
||||
Kind: "kind",
|
||||
ID: "name",
|
||||
Causes: []api.StatusCause{{
|
||||
Type: api.CauseTypeFieldValueNotFound,
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
NewFieldNotSupported("field[0].name", "bar"),
|
||||
&api.StatusDetails{
|
||||
Kind: "kind",
|
||||
ID: "name",
|
||||
Causes: []api.StatusCause{{
|
||||
Type: api.CauseTypeFieldValueNotSupported,
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
},
|
||||
{
|
||||
NewFieldRequired("field[0].name", "bar"),
|
||||
&api.StatusDetails{
|
||||
Kind: "kind",
|
||||
ID: "name",
|
||||
Causes: []api.StatusCause{{
|
||||
Type: api.CauseTypeFieldValueRequired,
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
},
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
vErr, expected := testCase.Err, testCase.Details
|
||||
expected.Causes[0].Message = vErr.Error()
|
||||
err := NewInvalid("kind", "name", ErrorList{vErr})
|
||||
status := err.(*statusError).Status()
|
||||
if status.Code != 422 || status.Reason != api.StatusReasonInvalid {
|
||||
t.Errorf("%d: unexpected status: %#v", i, status)
|
||||
}
|
||||
if !reflect.DeepEqual(expected, status.Details) {
|
||||
t.Errorf("%d: expected %#v, got %#v", expected, status.Details)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_reasonForError(t *testing.T) {
|
||||
if e, a := api.StatusReasonUnknown, reasonForError(nil); e != a {
|
||||
t.Errorf("unexpected reason type: %#v", a)
|
||||
}
|
||||
}
|
||||
@@ -75,28 +75,28 @@ func (v ValidationError) Error() string {
|
||||
return fmt.Sprintf("%s: %v '%v'", v.Field, ValueOf(v.Type), v.BadValue)
|
||||
}
|
||||
|
||||
// NewInvalid returns a ValidationError indicating "value required"
|
||||
func NewRequired(field string, value interface{}) ValidationError {
|
||||
// NewFieldRequired returns a ValidationError indicating "value required"
|
||||
func NewFieldRequired(field string, value interface{}) ValidationError {
|
||||
return ValidationError{ValidationErrorTypeRequired, field, value}
|
||||
}
|
||||
|
||||
// NewInvalid returns a ValidationError indicating "invalid value"
|
||||
func NewInvalid(field string, value interface{}) ValidationError {
|
||||
// NewFieldInvalid returns a ValidationError indicating "invalid value"
|
||||
func NewFieldInvalid(field string, value interface{}) ValidationError {
|
||||
return ValidationError{ValidationErrorTypeInvalid, field, value}
|
||||
}
|
||||
|
||||
// NewNotSupported returns a ValidationError indicating "unsupported value"
|
||||
func NewNotSupported(field string, value interface{}) ValidationError {
|
||||
// NewFieldNotSupported returns a ValidationError indicating "unsupported value"
|
||||
func NewFieldNotSupported(field string, value interface{}) ValidationError {
|
||||
return ValidationError{ValidationErrorTypeNotSupported, field, value}
|
||||
}
|
||||
|
||||
// NewDuplicate returns a ValidationError indicating "duplicate value"
|
||||
func NewDuplicate(field string, value interface{}) ValidationError {
|
||||
// NewFieldDuplicate returns a ValidationError indicating "duplicate value"
|
||||
func NewFieldDuplicate(field string, value interface{}) ValidationError {
|
||||
return ValidationError{ValidationErrorTypeDuplicate, field, value}
|
||||
}
|
||||
|
||||
// NewNotFound returns a ValidationError indicating "value not found"
|
||||
func NewNotFound(field string, value interface{}) ValidationError {
|
||||
// NewFieldNotFound returns a ValidationError indicating "value not found"
|
||||
func NewFieldNotFound(field string, value interface{}) ValidationError {
|
||||
return ValidationError{ValidationErrorTypeNotFound, field, value}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,23 +28,23 @@ func TestMakeFuncs(t *testing.T) {
|
||||
expected ValidationErrorType
|
||||
}{
|
||||
{
|
||||
func() ValidationError { return NewInvalid("f", "v") },
|
||||
func() ValidationError { return NewFieldInvalid("f", "v") },
|
||||
ValidationErrorTypeInvalid,
|
||||
},
|
||||
{
|
||||
func() ValidationError { return NewNotSupported("f", "v") },
|
||||
func() ValidationError { return NewFieldNotSupported("f", "v") },
|
||||
ValidationErrorTypeNotSupported,
|
||||
},
|
||||
{
|
||||
func() ValidationError { return NewDuplicate("f", "v") },
|
||||
func() ValidationError { return NewFieldDuplicate("f", "v") },
|
||||
ValidationErrorTypeDuplicate,
|
||||
},
|
||||
{
|
||||
func() ValidationError { return NewNotFound("f", "v") },
|
||||
func() ValidationError { return NewFieldNotFound("f", "v") },
|
||||
ValidationErrorTypeNotFound,
|
||||
},
|
||||
{
|
||||
func() ValidationError { return NewRequired("f", "v") },
|
||||
func() ValidationError { return NewFieldRequired("f", "v") },
|
||||
ValidationErrorTypeRequired,
|
||||
},
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func TestMakeFuncs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidationError(t *testing.T) {
|
||||
s := NewInvalid("foo", "bar").Error()
|
||||
s := NewFieldInvalid("foo", "bar").Error()
|
||||
if !strings.Contains(s, "foo") || !strings.Contains(s, "bar") || !strings.Contains(s, ValueOf(ValidationErrorTypeInvalid)) {
|
||||
t.Errorf("error message did not contain expected values, got %s", s)
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func TestErrorList(t *testing.T) {
|
||||
if a := errorListInternal(errList).Error(); a != "" {
|
||||
t.Errorf("expected empty string, got %v", a)
|
||||
}
|
||||
errList = append(errList, NewInvalid("field", "value"))
|
||||
errList = append(errList, NewFieldInvalid("field", "value"))
|
||||
// The fact that this compiles is the test.
|
||||
}
|
||||
|
||||
@@ -108,15 +108,15 @@ func TestErrListPrefix(t *testing.T) {
|
||||
Expected string
|
||||
}{
|
||||
{
|
||||
NewNotFound("[0].bar", "value"),
|
||||
NewFieldNotFound("[0].bar", "value"),
|
||||
"foo[0].bar",
|
||||
},
|
||||
{
|
||||
NewInvalid("field", "value"),
|
||||
NewFieldInvalid("field", "value"),
|
||||
"foo.field",
|
||||
},
|
||||
{
|
||||
NewDuplicate("", "value"),
|
||||
NewFieldDuplicate("", "value"),
|
||||
"foo",
|
||||
},
|
||||
}
|
||||
@@ -138,15 +138,15 @@ func TestErrListPrefixIndex(t *testing.T) {
|
||||
Expected string
|
||||
}{
|
||||
{
|
||||
NewNotFound("[0].bar", "value"),
|
||||
NewFieldNotFound("[0].bar", "value"),
|
||||
"[1][0].bar",
|
||||
},
|
||||
{
|
||||
NewInvalid("field", "value"),
|
||||
NewFieldInvalid("field", "value"),
|
||||
"[1].field",
|
||||
},
|
||||
{
|
||||
NewDuplicate("", "value"),
|
||||
NewFieldDuplicate("", "value"),
|
||||
"[1]",
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user