Skip to content

Commit a11986c

Browse files
commonerrors Add IgnoreCorrespondTo function
1 parent 2c87cb7 commit a11986c

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

changes/20250801105019.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `commonerrors` Add IgnoreCorrespondTo function

utils/commonerrors/errors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ func Ignore(target error, ignore ...error) error {
255255
return target
256256
}
257257

258+
// IgnoreCorrespondTo will return nil if the target error matches one of the errors to ignore
259+
func IgnoreCorrespondTo(target error, ignore ...string) error {
260+
if CorrespondTo(target, ignore...) {
261+
return nil
262+
}
263+
return target
264+
}
265+
258266
// IsEmpty states whether an error is empty or not.
259267
// An error is considered empty if it is `nil` or has no description.
260268
func IsEmpty(err error) bool {

utils/commonerrors/errors_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func TestCorrespondTo(t *testing.T) {
4949
assert.True(t, CorrespondTo(fmt.Errorf("%v %v", faker.Sentence(), strings.ToUpper(ErrUndefined.Error())), strings.ToLower(ErrUndefined.Error())))
5050
}
5151

52+
func TestIgnoreCorrespondTo(t *testing.T) {
53+
assert.NoError(t, IgnoreCorrespondTo(errors.New("test"), "test"))
54+
assert.NoError(t, IgnoreCorrespondTo(errors.New("test 123"), "test"))
55+
assert.Error(t, IgnoreCorrespondTo(errors.New("test 123"), "abc", "def", faker.Word()))
56+
assert.NoError(t, IgnoreCorrespondTo(ErrCondition, "condition"))
57+
}
58+
5259
func TestContextErrorConversion(t *testing.T) {
5360
defer goleak.VerifyNone(t)
5461
task := func(ctx context.Context) {

utils/filesystem/extendedosfs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func (c *ExtendedOsFs) Remove(name string) (err error) {
2525
// The following is to ensure sockets are correctly removed
2626
// https://stackoverflow.com/questions/16681944/how-to-reliably-unlink-a-unix-domain-socket-in-go-programming-language
2727
err = commonerrors.Ignore(ConvertFileSystemError(syscall.Unlink(name)), commonerrors.ErrNotFound)
28-
if err != nil && !commonerrors.CorrespondTo(err, "is a directory") {
28+
err = commonerrors.IgnoreCorrespondTo(err, "is a directory")
29+
if err != nil {
2930
return
3031
}
3132

0 commit comments

Comments
 (0)