diff --git a/util.go b/util.go index 3217052..95009a1 100644 --- a/util.go +++ b/util.go @@ -27,12 +27,12 @@ type ConfigParseError struct { } // Error returns the formatted configuration error. -func (pe *ConfigParseError) Error() string { +func (pe ConfigParseError) Error() string { return fmt.Sprintf("While parsing config: %s", pe.err.Error()) } // Unwrap returns the wrapped error. -func (pe *ConfigParseError) Unwrap() error { +func (pe ConfigParseError) Unwrap() error { return pe.err } diff --git a/viper.go b/viper.go index 11f50ba..5f76cc0 100644 --- a/viper.go +++ b/viper.go @@ -1700,7 +1700,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop", "dotenv", "env": err := v.decoderRegistry.Decode(format, buf.Bytes(), c) if err != nil { - return &ConfigParseError{err} + return ConfigParseError{err} } } diff --git a/viper_test.go b/viper_test.go index 45c71e1..77b8455 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1485,17 +1485,14 @@ func TestWrongDirsSearchNotFoundForMerge(t *testing.T) { assert.Equal(t, `default`, v.GetString(`key`)) } -func TestUnwrap(t *testing.T) { - yamlInvalid := []byte(`hash: map - - foo - - bar - `) +var yamlInvalid = []byte(`hash: map +- foo +- bar +`) +func TestUnwrapParseErrors(t *testing.T) { SetConfigType("yaml") - err := ReadConfig(bytes.NewBuffer(yamlInvalid)) - - var cpe ConfigParseError - if !errors.As(err, &cpe) { + if !errors.As(ReadConfig(bytes.NewBuffer(yamlInvalid)), &ConfigParseError{}) { t.Fatalf("not a ConfigParseError") } }