mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
Merge pull request #2124 from brendandburns/fix
Make endpoints return 400 instead of 500
This commit is contained in:
commit
81785d8cde
@ -116,6 +116,22 @@ func NewInvalid(kind, name string, errs ValidationErrorList) error {
|
|||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBadRequest creates an error that indicates that the request is invalid and can not be processed.
|
||||||
|
func NewBadRequest(reason string) error {
|
||||||
|
return &statusError{
|
||||||
|
api.Status{
|
||||||
|
Status: api.StatusFailure,
|
||||||
|
Code: 400,
|
||||||
|
Reason: api.StatusReasonBadRequest,
|
||||||
|
Details: &api.StatusDetails{
|
||||||
|
Causes: []api.StatusCause{
|
||||||
|
{Message: reason},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// IsNotFound returns true if the specified error was created by NewNotFoundErr.
|
// IsNotFound returns true if the specified error was created by NewNotFoundErr.
|
||||||
func IsNotFound(err error) bool {
|
func IsNotFound(err error) bool {
|
||||||
return reasonForError(err) == api.StatusReasonNotFound
|
return reasonForError(err) == api.StatusReasonNotFound
|
||||||
@ -136,6 +152,11 @@ func IsInvalid(err error) bool {
|
|||||||
return reasonForError(err) == api.StatusReasonInvalid
|
return reasonForError(err) == api.StatusReasonInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsBadRequest determines if err is an error which indicates that the request is invalid.
|
||||||
|
func IsBadRequest(err error) bool {
|
||||||
|
return reasonForError(err) == api.StatusReasonBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
func reasonForError(err error) api.StatusReason {
|
func reasonForError(err error) api.StatusReason {
|
||||||
switch t := err.(type) {
|
switch t := err.(type) {
|
||||||
case *statusError:
|
case *statusError:
|
||||||
|
@ -680,6 +680,12 @@ const (
|
|||||||
// field attributes will be set.
|
// field attributes will be set.
|
||||||
// Status code 422
|
// Status code 422
|
||||||
StatusReasonInvalid StatusReason = "Invalid"
|
StatusReasonInvalid StatusReason = "Invalid"
|
||||||
|
|
||||||
|
// StatusReasonBadRequest means that the request itself was invalid, because the request
|
||||||
|
// doesn't make any sense, for example deleting a read-only object. This is different than
|
||||||
|
// StatusReasonInvalid above which indicates that the API call could possibly succeed, but the
|
||||||
|
// data was invalid. API calls that return BadRequest can never succeed.
|
||||||
|
StatusReasonBadRequest StatusReason = "BadRequest"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StatusCause provides more information about an api.Status failure, including
|
// StatusCause provides more information about an api.Status failure, including
|
||||||
|
@ -17,10 +17,10 @@ limitations under the License.
|
|||||||
package endpoint
|
package endpoint
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
@ -48,7 +48,7 @@ func (rs *REST) Get(ctx api.Context, id string) (runtime.Object, error) {
|
|||||||
// List satisfies the RESTStorage interface.
|
// List satisfies the RESTStorage interface.
|
||||||
func (rs *REST) List(ctx api.Context, label, field labels.Selector) (runtime.Object, error) {
|
func (rs *REST) List(ctx api.Context, label, field labels.Selector) (runtime.Object, error) {
|
||||||
if !label.Empty() || !field.Empty() {
|
if !label.Empty() || !field.Empty() {
|
||||||
return nil, errors.New("label/field selectors are not supported on endpoints")
|
return nil, errors.NewBadRequest("label/field selectors are not supported on endpoints")
|
||||||
}
|
}
|
||||||
return rs.registry.ListEndpoints(ctx)
|
return rs.registry.ListEndpoints(ctx)
|
||||||
}
|
}
|
||||||
@ -95,7 +95,7 @@ func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan apiserver.RE
|
|||||||
|
|
||||||
// Delete satisfies the RESTStorage interface but is unimplemented.
|
// Delete satisfies the RESTStorage interface but is unimplemented.
|
||||||
func (rs *REST) Delete(ctx api.Context, id string) (<-chan apiserver.RESTResult, error) {
|
func (rs *REST) Delete(ctx api.Context, id string) (<-chan apiserver.RESTResult, error) {
|
||||||
return nil, errors.New("unimplemented")
|
return nil, errors.NewBadRequest("Endpoints are read-only")
|
||||||
}
|
}
|
||||||
|
|
||||||
// New implements the RESTStorage interface.
|
// New implements the RESTStorage interface.
|
||||||
|
@ -96,3 +96,14 @@ func TestEndpointsRegistryList(t *testing.T) {
|
|||||||
t.Errorf("Unexpected resource version: %#v", sl)
|
t.Errorf("Unexpected resource version: %#v", sl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEndpointsRegistryDelete(t *testing.T) {
|
||||||
|
registry := registrytest.NewServiceRegistry()
|
||||||
|
storage := NewREST(registry)
|
||||||
|
_, err := storage.Delete(api.NewContext(), "n/a")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("unexpected non-error")
|
||||||
|
} else if !errors.IsBadRequest(err) {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user