Skip to content

Commit

Permalink
Merge pull request #235 from muvaf/ignore-more
Browse files Browse the repository at this point in the history
Introduce IgnoreAny to be able to ignore more than one type of error
  • Loading branch information
muvaf authored Jan 12, 2021
2 parents 479dbc8 + 10aa02f commit 8c0b53e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,26 @@ func MustGetKind(obj runtime.Object, ot runtime.ObjectTyper) schema.GroupVersion
type ErrorIs func(err error) bool

// Ignore any errors that satisfy the supplied ErrorIs function by returning
// nil. Errors that do not satisfy the suppled function are returned unmodified.
// nil. Errors that do not satisfy the supplied function are returned unmodified.
func Ignore(is ErrorIs, err error) error {
if is(err) {
return nil
}
return err
}

// IgnoreAny ignores errors that satisfy any of the supplied ErrorIs functions
// by returning nil. Errors that do not satisfy any of the supplied functions
// are returned unmodified.
func IgnoreAny(err error, is ...ErrorIs) error {
for _, f := range is {
if f(err) {
return nil
}
}
return err
}

// IgnoreNotFound returns the supplied error, or nil if the error indicates a
// Kubernetes resource was not found.
func IgnoreNotFound(err error) error {
Expand Down
47 changes: 47 additions & 0 deletions pkg/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,53 @@ func TestIgnore(t *testing.T) {
}
}

func TestIgnoreAny(t *testing.T) {
errBoom := errors.New("boom")

type args struct {
is []ErrorIs
err error
}
cases := map[string]struct {
args args
want error
}{
"IgnoreError": {
args: args{
is: []ErrorIs{func(err error) bool { return true }},
err: errBoom,
},
want: nil,
},
"IgnoreErrorArr": {
args: args{
is: []ErrorIs{
func(err error) bool { return true },
func(err error) bool { return false },
},
err: errBoom,
},
want: nil,
},
"PropagateError": {
args: args{
is: []ErrorIs{func(err error) bool { return false }},
err: errBoom,
},
want: errBoom,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := IgnoreAny(tc.args.err, tc.args.is...)
if diff := cmp.Diff(tc.want, got, test.EquateErrors()); diff != "" {
t.Errorf("Ignore(...): -want error, +got error:\n%s", diff)
}
})
}
}

func TestIsConditionTrue(t *testing.T) {
cases := map[string]struct {
c xpv1.Condition
Expand Down

0 comments on commit 8c0b53e

Please sign in to comment.