Skip to content

Commit b9964c1

Browse files
authored
🐛 [commonerrors] fix string representation (#618)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description fix string formatting of errors ### 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 89b797c commit b9964c1

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

changes/20250521081848.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:bug: `[commonerrors]` fix string representation

utils/commonerrors/errors.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,12 @@ func Errorf(targetErr error, format string, args ...any) error {
277277
if len(args) > 0 {
278278
msg = fmt.Sprintf(format, args...)
279279
}
280-
return fmt.Errorf("%w%v %v", tErr, string(TypeReasonErrorSeparator), msg)
280+
cleansedMsg := strings.TrimSpace(msg)
281+
if cleansedMsg == "" {
282+
return tErr
283+
} else {
284+
return fmt.Errorf("%w%v %v", tErr, string(TypeReasonErrorSeparator), cleansedMsg)
285+
}
281286
}
282287

283288
// WrapError wraps an error into a particular targetError. However, if the original error has to do with a contextual error (i.e. ErrCancelled or ErrTimeout), it will be passed through without having is type changed.
@@ -295,7 +300,12 @@ func WrapError(targetError, originalError error, msg string) error {
295300
if originalError == nil {
296301
return New(tErr, msg)
297302
} else {
298-
return Errorf(tErr, "%v%v %v", msg, string(TypeReasonErrorSeparator), originalError.Error())
303+
cleansedMsg := strings.TrimSpace(msg)
304+
if cleansedMsg == "" {
305+
return New(tErr, originalError.Error())
306+
} else {
307+
return Errorf(tErr, "%v%v %v", cleansedMsg, string(TypeReasonErrorSeparator), originalError.Error())
308+
}
299309
}
300310
}
301311

utils/commonerrors/errors_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,21 @@ func TestWrapError(t *testing.T) {
221221
assert.True(t, Any(WrapIfNotCommonError(ErrUndefined, errors.New(faker.Sentence()), faker.Sentence()), ErrUndefined))
222222
assert.True(t, Any(WrapIfNotCommonErrorf(ErrUndefined, errors.New(faker.Sentence()), faker.Sentence()), ErrUndefined))
223223
}
224+
225+
func TestString(t *testing.T) {
226+
assert.Equal(t, "unknown", New(nil, "").Error())
227+
assert.Equal(t, "unknown", Newf(nil, "").Error())
228+
assert.Equal(t, "unknown", WrapError(nil, nil, "").Error())
229+
assert.Equal(t, "unknown", WrapErrorf(nil, nil, "").Error())
230+
assert.Equal(t, "unsupported", New(ErrUnsupported, "").Error())
231+
assert.Equal(t, "unsupported", Newf(ErrUnsupported, "").Error())
232+
assert.Equal(t, "unsupported", WrapError(ErrUnsupported, nil, "").Error())
233+
assert.Equal(t, "unsupported", WrapErrorf(ErrUnsupported, nil, "").Error())
234+
assert.Equal(t, "unsupported: test", New(ErrUnsupported, "test").Error())
235+
assert.Equal(t, "unknown: test", New(nil, "test").Error())
236+
assert.Equal(t, "unsupported: test 56", Newf(ErrUnsupported, "test %v", 56).Error())
237+
assert.Equal(t, "unsupported: test", WrapError(ErrUnsupported, nil, "test").Error())
238+
assert.Equal(t, "unsupported: not found", WrapError(ErrUnsupported, ErrNotFound, "").Error())
239+
assert.Equal(t, "unknown: test: unsupported", WrapError(nil, ErrUnsupported, "test").Error())
240+
assert.Equal(t, "unsupported: test 56: not found", WrapErrorf(ErrUnsupported, ErrNotFound, "test %v", 56).Error())
241+
}

0 commit comments

Comments
 (0)