Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove exposed error type #24

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading