diff --git a/pkg/api/errors/validation.go b/pkg/api/errors/validation.go index 16edad83ec8..a204e348cec 100644 --- a/pkg/api/errors/validation.go +++ b/pkg/api/errors/validation.go @@ -20,6 +20,7 @@ import ( "fmt" "strings" + "github.com/davecgh/go-spew/spew" "github.com/golang/glog" ) @@ -51,8 +52,8 @@ const ( ValidationErrorTypeForbidden ValidationErrorType = "FieldValueForbidden" ) -// ValueOf converts a ValidationErrorType into its corresponding error message. -func ValueOf(t ValidationErrorType) string { +// String converts a ValidationErrorType into its corresponding error message. +func (t ValidationErrorType) String() string { switch t { case ValidationErrorTypeNotFound: return "not found" @@ -83,7 +84,7 @@ type ValidationError struct { var _ error = &ValidationError{} func (v *ValidationError) Error() string { - s := fmt.Sprintf("%s: %s '%v'", v.Field, ValueOf(v.Type), v.BadValue) + s := spew.Sprintf("%s: %s '%+v'", v.Field, v.Type, v.BadValue) if v.Detail != "" { s += fmt.Sprintf(": %s", v.Detail) } diff --git a/pkg/api/errors/validation_test.go b/pkg/api/errors/validation_test.go index 3b2493e8426..cc643d0236a 100644 --- a/pkg/api/errors/validation_test.go +++ b/pkg/api/errors/validation_test.go @@ -56,10 +56,39 @@ func TestMakeFuncs(t *testing.T) { } } -func TestValidationError(t *testing.T) { +func TestValidationErrorUsefulMessage(t *testing.T) { s := NewFieldInvalid("foo", "bar", "deet").Error() - if !strings.Contains(s, "foo") || !strings.Contains(s, "bar") || !strings.Contains(s, "deet") || !strings.Contains(s, ValueOf(ValidationErrorTypeInvalid)) { - t.Errorf("error message did not contain expected values, got %s", s) + t.Logf("message: %v", s) + for _, part := range []string{"foo", "bar", "deet", ValidationErrorTypeInvalid.String()} { + if !strings.Contains(s, part) { + t.Errorf("error message did not contain expected part '%v'", part) + } + } + + type complicated struct { + Baz int + Qux string + Inner interface{} + KV map[string]int + } + s = NewFieldRequired( + "foo", + &complicated{ + Baz: 1, + Qux: "aoeu", + Inner: &complicated{Qux: "asdf"}, + KV: map[string]int{"Billy": 2}, + }, + ).Error() + t.Logf("message: %v", s) + for _, part := range []string{ + "foo", ValidationErrorTypeRequired.String(), + "Baz", "Qux", "Inner", "KV", + "1", "aoeu", "asdf", "Billy", "2", + } { + if !strings.Contains(s, part) { + t.Errorf("error message did not contain expected part '%v'", part) + } } }