Allow ConfigParseError to unwrap (#1433)

* Allow ConfigParseError to unwrap

* wip

* Avoid pointer type
This commit is contained in:
andig 2023-05-30 12:07:27 +02:00 committed by GitHub
parent 53cdb5253a
commit 21a7fd828e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View file

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

View file

@ -8,6 +8,7 @@ package viper
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
@ -1573,6 +1574,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")