Skip to content

Commit

Permalink
Allow ConfigParseError to unwrap (#1433)
Browse files Browse the repository at this point in the history
* Allow ConfigParseError to unwrap

* wip

* Avoid pointer type
  • Loading branch information
andig authored May 30, 2023
1 parent 53cdb52 commit 21a7fd8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
13 changes: 13 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package viper
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 21a7fd8

Please sign in to comment.