From 21a7fd828ed231bbe62068d6aafa5aa9f85dc79e Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 30 May 2023 12:07:27 +0200 Subject: [PATCH] Allow ConfigParseError to unwrap (#1433) * Allow ConfigParseError to unwrap * wip * Avoid pointer type --- util.go | 5 +++++ viper_test.go | 13 +++++++++++++ 2 files changed, 18 insertions(+) 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 b48c95081..e0bfc57bd 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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")