Fix panic on nil invalid field error

This commit is contained in:
Jordan Liggitt
2017-02-28 10:46:17 -05:00
parent 49d1814b3a
commit 0cf9b0df56
2 changed files with 12 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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()} {