mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
apierrors: Avoid spurious <nil> in invalid error message
This commit is contained in:
parent
87b0412232
commit
57fdd167e4
@ -289,7 +289,7 @@ func NewInvalid(qualifiedKind schema.GroupKind, name string, errs field.ErrorLis
|
||||
Field: err.Field,
|
||||
})
|
||||
}
|
||||
return &StatusError{metav1.Status{
|
||||
err := &StatusError{metav1.Status{
|
||||
Status: metav1.StatusFailure,
|
||||
Code: http.StatusUnprocessableEntity,
|
||||
Reason: metav1.StatusReasonInvalid,
|
||||
@ -299,8 +299,14 @@ func NewInvalid(qualifiedKind schema.GroupKind, name string, errs field.ErrorLis
|
||||
Name: name,
|
||||
Causes: causes,
|
||||
},
|
||||
Message: fmt.Sprintf("%s %q is invalid: %v", qualifiedKind.String(), name, errs.ToAggregate()),
|
||||
}}
|
||||
aggregatedErrs := errs.ToAggregate()
|
||||
if aggregatedErrs == nil {
|
||||
err.ErrStatus.Message = fmt.Sprintf("%s %q is invalid", qualifiedKind.String(), name)
|
||||
} else {
|
||||
err.ErrStatus.Message = fmt.Sprintf("%s %q is invalid: %v", qualifiedKind.String(), name, aggregatedErrs)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// NewBadRequest creates an error that indicates that the request is invalid and can not be processed.
|
||||
|
@ -125,6 +125,7 @@ func TestNewInvalid(t *testing.T) {
|
||||
testCases := []struct {
|
||||
Err *field.Error
|
||||
Details *metav1.StatusDetails
|
||||
Msg string
|
||||
}{
|
||||
{
|
||||
field.Duplicate(field.NewPath("field[0].name"), "bar"),
|
||||
@ -136,6 +137,7 @@ func TestNewInvalid(t *testing.T) {
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
`Kind "name" is invalid: field[0].name: Duplicate value: "bar"`,
|
||||
},
|
||||
{
|
||||
field.Invalid(field.NewPath("field[0].name"), "bar", "detail"),
|
||||
@ -147,6 +149,7 @@ func TestNewInvalid(t *testing.T) {
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
`Kind "name" is invalid: field[0].name: Invalid value: "bar": detail`,
|
||||
},
|
||||
{
|
||||
field.NotFound(field.NewPath("field[0].name"), "bar"),
|
||||
@ -158,6 +161,7 @@ func TestNewInvalid(t *testing.T) {
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
`Kind "name" is invalid: field[0].name: Not found: "bar"`,
|
||||
},
|
||||
{
|
||||
field.NotSupported(field.NewPath("field[0].name"), "bar", nil),
|
||||
@ -169,6 +173,7 @@ func TestNewInvalid(t *testing.T) {
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
`Kind "name" is invalid: field[0].name: Unsupported value: "bar"`,
|
||||
},
|
||||
{
|
||||
field.Required(field.NewPath("field[0].name"), ""),
|
||||
@ -180,12 +185,28 @@ func TestNewInvalid(t *testing.T) {
|
||||
Field: "field[0].name",
|
||||
}},
|
||||
},
|
||||
`Kind "name" is invalid: field[0].name: Required value`,
|
||||
},
|
||||
{
|
||||
nil,
|
||||
&metav1.StatusDetails{
|
||||
Kind: "Kind",
|
||||
Name: "name",
|
||||
Causes: []metav1.StatusCause{},
|
||||
},
|
||||
`Kind "name" is invalid`,
|
||||
},
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
vErr, expected := testCase.Err, testCase.Details
|
||||
if vErr != nil && expected != nil {
|
||||
expected.Causes[0].Message = vErr.ErrorBody()
|
||||
err := NewInvalid(kind("Kind"), "name", field.ErrorList{vErr})
|
||||
}
|
||||
var errList field.ErrorList
|
||||
if vErr != nil {
|
||||
errList = append(errList, vErr)
|
||||
}
|
||||
err := NewInvalid(kind("Kind"), "name", errList)
|
||||
status := err.ErrStatus
|
||||
if status.Code != 422 || status.Reason != metav1.StatusReasonInvalid {
|
||||
t.Errorf("%d: unexpected status: %#v", i, status)
|
||||
@ -193,6 +214,9 @@ func TestNewInvalid(t *testing.T) {
|
||||
if !reflect.DeepEqual(expected, status.Details) {
|
||||
t.Errorf("%d: expected %#v, got %#v", i, expected, status.Details)
|
||||
}
|
||||
if testCase.Msg != status.Message {
|
||||
t.Errorf("%d: expected\n%s\ngot\n%s", i, testCase.Msg, status.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user