diff --git a/util.go b/util.go index 64e657505..95009a147 100644 --- a/util.go +++ b/util.go @@ -31,6 +31,11 @@ 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 { + return pe.err +} + // toCaseInsensitiveValue checks if the value is a map; // if so, create a copy and lower-case the keys recursively. func toCaseInsensitiveValue(value interface{}) interface{} { diff --git a/viper_test.go b/viper_test.go index 926ffc293..77b8455c5 100644 --- a/viper_test.go +++ b/viper_test.go @@ -8,6 +8,7 @@ package viper import ( "bytes" "encoding/json" + "errors" "io" "io/ioutil" //nolint:staticcheck "os" @@ -1484,6 +1485,18 @@ func TestWrongDirsSearchNotFoundForMerge(t *testing.T) { assert.Equal(t, `default`, v.GetString(`key`)) } +var yamlInvalid = []byte(`hash: map +- foo +- bar +`) + +func TestUnwrapParseErrors(t *testing.T) { + SetConfigType("yaml") + if !errors.As(ReadConfig(bytes.NewBuffer(yamlInvalid)), &ConfigParseError{}) { + t.Fatalf("not a ConfigParseError") + } +} + func TestSub(t *testing.T) { v := New() v.SetConfigType("yaml")