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 committed 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
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
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.