Skip to content

Commit

Permalink
Merge pull request #201 from northvolt/value-in-error-message
Browse files Browse the repository at this point in the history
Print value in error messages
  • Loading branch information
mitchellh committed Nov 26, 2020
2 parents d5b1da7 + 130efaf commit 07998cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
24 changes: 12 additions & 12 deletions mapstructure.go
Expand Up @@ -562,8 +562,8 @@ func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value)

if !converted {
return fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
name, val.Type(), dataVal.Type())
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
name, val.Type(), dataVal.Type(), data)
}

return nil
Expand Down Expand Up @@ -604,8 +604,8 @@ func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) er
val.SetInt(i)
default:
return fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
name, val.Type(), dataVal.Type())
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
name, val.Type(), dataVal.Type(), data)
}

return nil
Expand Down Expand Up @@ -660,8 +660,8 @@ func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) e
val.SetUint(uint64(i))
default:
return fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
name, val.Type(), dataVal.Type())
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
name, val.Type(), dataVal.Type(), data)
}

return nil
Expand Down Expand Up @@ -691,8 +691,8 @@ func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) e
}
default:
return fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
name, val.Type(), dataVal.Type())
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
name, val.Type(), dataVal.Type(), data)
}

return nil
Expand Down Expand Up @@ -733,8 +733,8 @@ func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value)
val.SetFloat(i)
default:
return fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
name, val.Type(), dataVal.Type())
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
name, val.Type(), dataVal.Type(), data)
}

return nil
Expand Down Expand Up @@ -999,8 +999,8 @@ func (d *Decoder) decodeFunc(name string, data interface{}, val reflect.Value) e
dataVal := reflect.Indirect(reflect.ValueOf(data))
if val.Type() != dataVal.Type() {
return fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
name, val.Type(), dataVal.Type())
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
name, val.Type(), dataVal.Type(), data)
}
val.Set(dataVal)
return nil
Expand Down
10 changes: 5 additions & 5 deletions mapstructure_examples_test.go
Expand Up @@ -62,11 +62,11 @@ func ExampleDecode_errors() {
// Output:
// 5 error(s) decoding:
//
// * 'Age' expected type 'int', got unconvertible type 'string'
// * 'Emails[0]' expected type 'string', got unconvertible type 'int'
// * 'Emails[1]' expected type 'string', got unconvertible type 'int'
// * 'Emails[2]' expected type 'string', got unconvertible type 'int'
// * 'Name' expected type 'string', got unconvertible type 'int'
// * 'Age' expected type 'int', got unconvertible type 'string', value: 'bad value'
// * 'Emails[0]' expected type 'string', got unconvertible type 'int', value: '1'
// * 'Emails[1]' expected type 'string', got unconvertible type 'int', value: '2'
// * 'Emails[2]' expected type 'string', got unconvertible type 'int', value: '3'
// * 'Name' expected type 'string', got unconvertible type 'int', value: '123'
}

func ExampleDecode_metadata() {
Expand Down
3 changes: 2 additions & 1 deletion mapstructure_test.go
Expand Up @@ -2068,7 +2068,8 @@ func TestInvalidType(t *testing.T) {
t.Fatalf("error should be kind of Error, instead: %#v", err)
}

if derr.Errors[0] != "'Vstring' expected type 'string', got unconvertible type 'int'" {
if derr.Errors[0] !=
"'Vstring' expected type 'string', got unconvertible type 'int', value: '42'" {
t.Errorf("got unexpected error: %s", err)
}

Expand Down

0 comments on commit 07998cf

Please sign in to comment.