Skip to content

Commit bad9aac

Browse files
committed
add functions Join and Unwrap
1 parent 1334694 commit bad9aac

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

error_1_13.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build go1.13
12
// +build go1.13
23

34
package errors
@@ -6,14 +7,17 @@ import (
67
baseErrors "errors"
78
)
89

9-
// find error in any wrapped error
10+
// As finds the first error in err's tree that matches target, and if one is found, sets
11+
// target to that error value and returns true. Otherwise, it returns false.
12+
//
13+
// For more information see stdlib errors.As.
1014
func As(err error, target interface{}) bool {
1115
return baseErrors.As(err, target)
1216
}
1317

1418
// Is detects whether the error is equal to a given error. Errors
1519
// are considered equal by this function if they are matched by errors.Is
16-
// or if their contained errors are matched through errors.Is
20+
// or if their contained errors are matched through errors.Is.
1721
func Is(e error, original error) bool {
1822
if baseErrors.Is(e, original) {
1923
return true
@@ -29,3 +33,29 @@ func Is(e error, original error) bool {
2933

3034
return false
3135
}
36+
37+
// Join returns an error that wraps the given errors.
38+
// Any nil error values are discarded.
39+
// Join returns nil if every value in errs is nil.
40+
// The error formats as the concatenation of the strings obtained
41+
// by calling the Error method of each element of errs, with a newline
42+
// between each string.
43+
//
44+
// A non-nil error returned by Join implements the Unwrap() []error method.
45+
//
46+
// For more information see stdlib errors.Join.
47+
func Join(errs ...error) error {
48+
return baseErrors.Join(errs...)
49+
}
50+
51+
// Unwrap returns the result of calling the Unwrap method on err, if err's
52+
// type contains an Unwrap method returning error.
53+
// Otherwise, Unwrap returns nil.
54+
//
55+
// Unwrap only calls a method of the form "Unwrap() error".
56+
// In particular Unwrap does not unwrap errors returned by [Join].
57+
//
58+
// For more information see stdlib errors.Unwrap.
59+
func Unwrap(err error) error {
60+
return baseErrors.Unwrap(err)
61+
}

error_backward.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !go1.13
12
// +build !go1.13
23

34
package errors
@@ -55,3 +56,70 @@ func Is(e error, original error) bool {
5556

5657
return false
5758
}
59+
60+
// Disclaimer: functions Join and Unwrap are copied from the stdlib errors
61+
// package v1.21.0.
62+
63+
// Join returns an error that wraps the given errors.
64+
// Any nil error values are discarded.
65+
// Join returns nil if every value in errs is nil.
66+
// The error formats as the concatenation of the strings obtained
67+
// by calling the Error method of each element of errs, with a newline
68+
// between each string.
69+
//
70+
// A non-nil error returned by Join implements the Unwrap() []error method.
71+
func Join(errs ...error) error {
72+
n := 0
73+
for _, err := range errs {
74+
if err != nil {
75+
n++
76+
}
77+
}
78+
if n == 0 {
79+
return nil
80+
}
81+
e := &joinError{
82+
errs: make([]error, 0, n),
83+
}
84+
for _, err := range errs {
85+
if err != nil {
86+
e.errs = append(e.errs, err)
87+
}
88+
}
89+
return e
90+
}
91+
92+
type joinError struct {
93+
errs []error
94+
}
95+
96+
func (e *joinError) Error() string {
97+
var b []byte
98+
for i, err := range e.errs {
99+
if i > 0 {
100+
b = append(b, '\n')
101+
}
102+
b = append(b, err.Error()...)
103+
}
104+
return string(b)
105+
}
106+
107+
func (e *joinError) Unwrap() []error {
108+
return e.errs
109+
}
110+
111+
// Unwrap returns the result of calling the Unwrap method on err, if err's
112+
// type contains an Unwrap method returning error.
113+
// Otherwise, Unwrap returns nil.
114+
//
115+
// Unwrap only calls a method of the form "Unwrap() error".
116+
// In particular Unwrap does not unwrap errors returned by [Join].
117+
func Unwrap(err error) error {
118+
u, ok := err.(interface {
119+
Unwrap() error
120+
})
121+
if !ok {
122+
return nil
123+
}
124+
return u.Unwrap()
125+
}

0 commit comments

Comments
 (0)