Skip to content

Commit

Permalink
Merge pull request #24 from go-viper/error
Browse files Browse the repository at this point in the history
Remove exposed error type
  • Loading branch information
sagikazarmark authored Jun 2, 2024
2 parents cb699d2 + 44474a2 commit 57a3d74
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"strings"
)

// Error implements the error interface and can represents multiple
// joinedError implements the error interface and can represents multiple
// errors that occur in the course of a single decode.
type Error struct {
type joinedError struct {
Errors []string
}

func (e *Error) Error() string {
func (e *joinedError) Error() string {
points := make([]string, len(e.Errors))
for i, err := range e.Errors {
points[i] = fmt.Sprintf("* %s", err)
Expand All @@ -25,9 +25,8 @@ func (e *Error) Error() string {
len(e.Errors), strings.Join(points, "\n"))
}

// WrappedErrors implements the errwrap.Wrapper interface to make this
// return value more useful with the errwrap and go-multierror libraries.
func (e *Error) WrappedErrors() []error {
// Unwrap implements the Unwrap function added in Go 1.20.
func (e *joinedError) Unwrap() []error {
if e == nil {
return nil
}
Expand All @@ -40,9 +39,10 @@ func (e *Error) WrappedErrors() []error {
return result
}

// TODO: replace with errors.Join when Go 1.20 is minimum version.
func appendErrors(errors []string, err error) []string {
switch e := err.(type) {
case *Error:
case *joinedError:
return append(errors, e.Errors...)
default:
return append(errors, e.Error())
Expand Down
8 changes: 4 additions & 4 deletions mapstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ func (d *Decoder) decodeMapFromMap(name string, dataVal reflect.Value, val refle

// If we had errors, return those
if len(errors) > 0 {
return &Error{errors}
return &joinedError{errors}
}

return nil
Expand Down Expand Up @@ -1184,7 +1184,7 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)

// If there were errors, we return those
if len(errors) > 0 {
return &Error{errors}
return &joinedError{errors}
}

return nil
Expand Down Expand Up @@ -1250,7 +1250,7 @@ func (d *Decoder) decodeArray(name string, data interface{}, val reflect.Value)

// If there were errors, we return those
if len(errors) > 0 {
return &Error{errors}
return &joinedError{errors}
}

return nil
Expand Down Expand Up @@ -1495,7 +1495,7 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
}

if len(errors) > 0 {
return &Error{errors}
return &joinedError{errors}
}

// Add the unused keys to the list of unused keys if we're tracking metadata
Expand Down
12 changes: 6 additions & 6 deletions mapstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2323,9 +2323,9 @@ func TestInvalidType(t *testing.T) {
t.Fatal("error should exist")
}

derr, ok := err.(*Error)
derr, ok := err.(*joinedError)
if !ok {
t.Fatalf("error should be kind of Error, instead: %#v", err)
t.Fatalf("error should be kind of joinedError, instead: %#v", err)
}

if derr.Errors[0] !=
Expand All @@ -2342,9 +2342,9 @@ func TestInvalidType(t *testing.T) {
t.Fatal("error should exist")
}

derr, ok = err.(*Error)
derr, ok = err.(*joinedError)
if !ok {
t.Fatalf("error should be kind of Error, instead: %#v", err)
t.Fatalf("error should be kind of joinedError, instead: %#v", err)
}

if derr.Errors[0] != "cannot parse 'Vuint', -42 overflows uint" {
Expand All @@ -2360,9 +2360,9 @@ func TestInvalidType(t *testing.T) {
t.Fatal("error should exist")
}

derr, ok = err.(*Error)
derr, ok = err.(*joinedError)
if !ok {
t.Fatalf("error should be kind of Error, instead: %#v", err)
t.Fatalf("error should be kind of joinedError, instead: %#v", err)
}

if derr.Errors[0] != "cannot parse 'Vuint', -42.000000 overflows uint" {
Expand Down

0 comments on commit 57a3d74

Please sign in to comment.