Skip to content

Commit a4dd29b

Browse files
commonerrors Add support for determining commonerror from a string (#468)
<!-- 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 support for determining commonerror from a string ### 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 ba11f87 commit a4dd29b

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

changes/20240604150742.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `commonerrors` Add support for determining commonerror from a string

utils/commonerrors/errors.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ func CorrespondTo(target error, description ...string) bool {
101101
return false
102102
}
103103

104+
// RelatesTo determines whether an error description string could relate to a particular set of common errors
105+
// This assumes that the error description follows the convention of placing the type of errors at the start of the string.
106+
func RelatesTo(target string, errors ...error) bool {
107+
for i := range errors {
108+
if strings.HasPrefix(target, errors[i].Error()) {
109+
return true
110+
}
111+
}
112+
return false
113+
}
114+
104115
// deserialiseCommonError returns the common error corresponding to its string value
105116
func deserialiseCommonError(errStr string) (bool, error) {
106117
errStr = strings.TrimSpace(errStr)

utils/commonerrors/errors_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,18 @@ func TestIgnore(t *testing.T) {
186186
assert.Equal(t, nil, Ignore(fmt.Errorf("an error %w", ErrNotImplemented), ErrInvalid, ErrNotImplemented, ErrUnknown))
187187
assert.Equal(t, fmt.Errorf("an error %w", ErrNotImplemented), Ignore(fmt.Errorf("an error %w", ErrNotImplemented), ErrInvalid, ErrUnknown))
188188
}
189+
190+
func TestErrorRelatesTo(t *testing.T) {
191+
assert.True(t, RelatesTo(fmt.Errorf("%w: %v", ErrInvalid, faker.Sentence()).Error(), ErrInvalid))
192+
assert.True(t, RelatesTo(fmt.Errorf("%w: %v", ErrInvalid, faker.Sentence()).Error(), ErrInvalid, ErrCondition))
193+
assert.False(t, RelatesTo(fmt.Errorf("%w: %v", ErrUnauthorised, faker.Sentence()).Error(), ErrInvalid))
194+
assert.False(t, RelatesTo(fmt.Errorf("%w: %v", ErrUnauthorised, faker.Sentence()).Error(), ErrInvalid, ErrCondition))
195+
assert.True(t, RelatesTo(fmt.Sprint(fmt.Errorf("%w: %v", ErrInvalid, faker.Sentence())), ErrInvalid))
196+
assert.True(t, RelatesTo(fmt.Sprint(fmt.Errorf("%w: %v", ErrInvalid, faker.Sentence())), ErrInvalid, ErrCondition))
197+
assert.False(t, RelatesTo(fmt.Sprint(fmt.Errorf("%w: %v", ErrUnauthorised, faker.Sentence())), ErrInvalid))
198+
assert.False(t, RelatesTo(fmt.Sprint(fmt.Errorf("%w: %v", ErrUnauthorised, faker.Sentence())), ErrInvalid, ErrCondition))
199+
assert.True(t, RelatesTo(fmt.Sprintf("%v: %v", ErrInvalid.Error(), faker.Sentence()), ErrInvalid))
200+
assert.True(t, RelatesTo(fmt.Sprintf("%v: %v", ErrInvalid.Error(), faker.Sentence()), ErrInvalid, ErrCondition))
201+
assert.False(t, RelatesTo(fmt.Sprintf("%v: %v", ErrUnauthorised.Error(), faker.Sentence()), ErrInvalid))
202+
assert.False(t, RelatesTo(fmt.Sprintf("%v: %v", ErrUnauthorised.Error(), faker.Sentence()), ErrInvalid, ErrCondition))
203+
}

0 commit comments

Comments
 (0)