Skip to content

Commit 21a7fd8

Browse files
authored
Allow ConfigParseError to unwrap (#1433)
* Allow ConfigParseError to unwrap * wip * Avoid pointer type
1 parent 53cdb52 commit 21a7fd8

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func (pe ConfigParseError) Error() string {
3131
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
3232
}
3333

34+
// Unwrap returns the wrapped error.
35+
func (pe ConfigParseError) Unwrap() error {
36+
return pe.err
37+
}
38+
3439
// toCaseInsensitiveValue checks if the value is a map;
3540
// if so, create a copy and lower-case the keys recursively.
3641
func toCaseInsensitiveValue(value interface{}) interface{} {

viper_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package viper
88
import (
99
"bytes"
1010
"encoding/json"
11+
"errors"
1112
"io"
1213
"io/ioutil"
1314
"os"
@@ -1573,6 +1574,18 @@ func TestWrongDirsSearchNotFoundForMerge(t *testing.T) {
15731574
assert.Equal(t, `default`, v.GetString(`key`))
15741575
}
15751576

1577+
var yamlInvalid = []byte(`hash: map
1578+
- foo
1579+
- bar
1580+
`)
1581+
1582+
func TestUnwrapParseErrors(t *testing.T) {
1583+
SetConfigType("yaml")
1584+
if !errors.As(ReadConfig(bytes.NewBuffer(yamlInvalid)), &ConfigParseError{}) {
1585+
t.Fatalf("not a ConfigParseError")
1586+
}
1587+
}
1588+
15761589
func TestSub(t *testing.T) {
15771590
v := New()
15781591
v.SetConfigType("yaml")

0 commit comments

Comments
 (0)