Skip to content

Commit 57a3d74

Browse files
Merge pull request #24 from go-viper/error
Remove exposed error type
2 parents cb699d2 + 44474a2 commit 57a3d74

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

error.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
"strings"
88
)
99

10-
// Error implements the error interface and can represents multiple
10+
// joinedError implements the error interface and can represents multiple
1111
// errors that occur in the course of a single decode.
12-
type Error struct {
12+
type joinedError struct {
1313
Errors []string
1414
}
1515

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

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

42+
// TODO: replace with errors.Join when Go 1.20 is minimum version.
4343
func appendErrors(errors []string, err error) []string {
4444
switch e := err.(type) {
45-
case *Error:
45+
case *joinedError:
4646
return append(errors, e.Errors...)
4747
default:
4848
return append(errors, e.Error())

mapstructure.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ func (d *Decoder) decodeMapFromMap(name string, dataVal reflect.Value, val refle
923923

924924
// If we had errors, return those
925925
if len(errors) > 0 {
926-
return &Error{errors}
926+
return &joinedError{errors}
927927
}
928928

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

11851185
// If there were errors, we return those
11861186
if len(errors) > 0 {
1187-
return &Error{errors}
1187+
return &joinedError{errors}
11881188
}
11891189

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

12511251
// If there were errors, we return those
12521252
if len(errors) > 0 {
1253-
return &Error{errors}
1253+
return &joinedError{errors}
12541254
}
12551255

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

14971497
if len(errors) > 0 {
1498-
return &Error{errors}
1498+
return &joinedError{errors}
14991499
}
15001500

15011501
// Add the unused keys to the list of unused keys if we're tracking metadata

mapstructure_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,9 +2323,9 @@ func TestInvalidType(t *testing.T) {
23232323
t.Fatal("error should exist")
23242324
}
23252325

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

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

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

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

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

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

0 commit comments

Comments
 (0)