mirror of https://github.com/k3s-io/k3s
Merge pull request #42253 from liggitt/nil-invalid-field-error
Automatic merge from submit-queue (batch tested with PRs 42128, 42064, 42253, 42309, 42322) Fix panic on nil invalid field error bug fix for validation panic if a field.Invalid is constructed with a nil badvalue, the Error() method panics, since reflect.TypeOf() returns nilpull/6/head
commit
3d868401e2
|
@ -50,7 +50,10 @@ func (v *Error) ErrorBody() string {
|
||||||
s = fmt.Sprintf("%s", v.Type)
|
s = fmt.Sprintf("%s", v.Type)
|
||||||
default:
|
default:
|
||||||
value := v.BadValue
|
value := v.BadValue
|
||||||
if reflect.TypeOf(value).Kind() == reflect.Ptr {
|
valueType := reflect.TypeOf(value)
|
||||||
|
if value == nil || valueType == nil {
|
||||||
|
value = "null"
|
||||||
|
} else if valueType.Kind() == reflect.Ptr {
|
||||||
if reflectValue := reflect.ValueOf(value); reflectValue.IsNil() {
|
if reflectValue := reflect.ValueOf(value); reflectValue.IsNil() {
|
||||||
value = "null"
|
value = "null"
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -62,6 +62,14 @@ func TestMakeFuncs(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorUsefulMessage(t *testing.T) {
|
func TestErrorUsefulMessage(t *testing.T) {
|
||||||
|
{
|
||||||
|
s := Invalid(nil, nil, "").Error()
|
||||||
|
t.Logf("message: %v", s)
|
||||||
|
if !strings.Contains(s, "null") {
|
||||||
|
t.Errorf("error message did not contain 'null': %s", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
s := Invalid(NewPath("foo"), "bar", "deet").Error()
|
s := Invalid(NewPath("foo"), "bar", "deet").Error()
|
||||||
t.Logf("message: %v", s)
|
t.Logf("message: %v", s)
|
||||||
for _, part := range []string{"foo", "bar", "deet", ErrorTypeInvalid.String()} {
|
for _, part := range []string{"foo", "bar", "deet", ErrorTypeInvalid.String()} {
|
||||||
|
|
Loading…
Reference in New Issue