-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
694 additions
and
994 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package utils | ||
|
||
import ( | ||
"io" | ||
|
||
logging "github.com/ipfs/go-log/v2" | ||
) | ||
|
||
func CloseAndLog(log logging.StandardLogger, name string, closer io.Closer) { | ||
if err := closer.Close(); err != nil { | ||
log.Warnf("closing %s: %s", name, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package utils | ||
|
||
import "errors" | ||
|
||
// ErrorContains reports whether any error in err's tree matches any error in targets tree. | ||
func ErrorContains(err, target error) bool { | ||
if errors.Is(err, target) || target == nil { | ||
return true | ||
} | ||
|
||
target = errors.Unwrap(target) | ||
if target == nil { | ||
return false | ||
} | ||
return ErrorContains(err, target) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ErrorContains(t *testing.T) { | ||
err1 := errors.New("1") | ||
err2 := errors.New("2") | ||
|
||
w1 := func(err error) error { | ||
return fmt.Errorf("wrap1: %w", err) | ||
} | ||
w2 := func(err error) error { | ||
return fmt.Errorf("wrap1: %w", err) | ||
} | ||
|
||
type args struct { | ||
err error | ||
target error | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
"nil err", | ||
args{ | ||
err: nil, | ||
target: err1, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"nil target", | ||
args{ | ||
err: err1, | ||
target: nil, | ||
}, | ||
true, | ||
}, | ||
{ | ||
"errors.Is true", | ||
args{ | ||
err: w1(err1), | ||
target: err1, | ||
}, | ||
true, | ||
}, | ||
{ | ||
"errors.Is false", | ||
args{ | ||
err: w1(err1), | ||
target: err2, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"same wrap but different base error", | ||
args{ | ||
err: w1(err1), | ||
target: w1(err2), | ||
}, | ||
false, | ||
}, | ||
{ | ||
"both wrapped true", | ||
args{ | ||
err: w1(err1), | ||
target: w2(err1), | ||
}, | ||
true, | ||
}, | ||
{ | ||
"both wrapped false", | ||
args{ | ||
err: w1(err1), | ||
target: w2(err2), | ||
}, | ||
false, | ||
}, | ||
{ | ||
"multierr first in slice", | ||
args{ | ||
err: errors.Join(w1(err1), w2(err2)), | ||
target: w2(err1), | ||
}, | ||
true, | ||
}, | ||
{ | ||
"multierr second in slice", | ||
args{ | ||
err: errors.Join(w1(err1), w2(err2)), | ||
target: w1(err2), | ||
}, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, | ||
tt.want, | ||
ErrorContains(tt.args.err, tt.args.target), | ||
"ErrorContains(%v, %v)", tt.args.err, tt.args.target) | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.