Skip to content

Commit 8c9b794

Browse files
[commonerrors] Add Ignore so that errors can be ignored (#240)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description <!-- Please add any detail or context that would be useful to a reviewer. --> Add IgnoreErrors so that errors can be ignored ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent e4d3384 commit 8c9b794

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

changes/20230428172043.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `[commonerrors]` Add Ignore so that errors can be ignored

utils/commonerrors/errors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,11 @@ func ConvertContextError(err error) error {
9898
}
9999
return err
100100
}
101+
102+
// Ignore will return nil if the target error matches one of the errors to ignore
103+
func Ignore(target error, ignore ...error) error {
104+
if Any(target, ignore...) {
105+
return nil
106+
}
107+
return target
108+
}

utils/commonerrors/errors_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,14 @@ func TestContextErrorConversion(t *testing.T) {
7171
require.NotNil(t, err)
7272
assert.True(t, Any(err, ErrTimeout, ErrCancelled))
7373
}
74+
75+
func TestIgnore(t *testing.T) {
76+
assert.Equal(t, nil, Ignore(ErrNotImplemented, ErrInvalid, ErrNotImplemented, ErrUnknown))
77+
assert.Equal(t, ErrNotImplemented, Ignore(ErrNotImplemented, ErrInvalid, ErrUnknown))
78+
assert.Equal(t, nil, Ignore(ErrNotImplemented, nil, ErrNotImplemented))
79+
assert.Equal(t, nil, Ignore(nil, nil, ErrNotImplemented))
80+
assert.Equal(t, ErrNotImplemented, Ignore(ErrNotImplemented, nil, ErrInvalid, ErrUnknown))
81+
assert.Equal(t, nil, Ignore(nil, ErrInvalid, ErrUnknown))
82+
assert.Equal(t, nil, Ignore(fmt.Errorf("an error %w", ErrNotImplemented), ErrInvalid, ErrNotImplemented, ErrUnknown))
83+
assert.Equal(t, fmt.Errorf("an error %w", ErrNotImplemented), Ignore(fmt.Errorf("an error %w", ErrNotImplemented), ErrInvalid, ErrUnknown))
84+
}

0 commit comments

Comments
 (0)