Skip to content

Commit c706d80

Browse files
authored
Merge pull request #41 from monzo/dont-lose-cause-context-with-wrap
Keep cause error context when calling Wrap
2 parents 238cbfb + 33980ab commit c706d80

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

errors.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
// user defined error parameters.
55
//
66
// Terrors can be used to wrap any object that satisfies the error interface:
7+
//
78
// terr := terrors.Wrap(err, map[string]string{"context": "my_context"})
89
//
910
// Terrors can be instantiated directly:
10-
// err := terrors.New("not_found", "object not found", map[string]string{
11+
//
12+
// err := terrors.New("not_found", "object not found", map[string]string{
1113
// "context": "my_context"
1214
// })
1315
//
1416
// Terrors offers built-in functions for instantiating Errors with common codes:
17+
//
1518
// err := terrors.NotFound("config_file", "config file not found", map[string]string{
1619
// "context": my_context
1720
// })
@@ -208,6 +211,7 @@ func addParams(err *Error, params map[string]string) *Error {
208211
Params: copiedParams,
209212
StackFrames: err.StackFrames,
210213
IsRetryable: err.IsRetryable,
214+
cause: err.cause,
211215
}
212216
}
213217

@@ -237,7 +241,9 @@ func (p *Error) PrefixMatches(prefixParts ...string) bool {
237241
// `PrefixMatches`, so if you were previously matching against a part of the string returned from error.Error() that
238242
// is _not_ the prefix, then this will be a breaking change. In this case you should update the string to match the
239243
// prefix. If this is not possible, you can match against the entire error string explicitly, for example:
240-
// strings.Contains(err.Error(), "context deadline exceeded")
244+
//
245+
// strings.Contains(err.Error(), "context deadline exceeded")
246+
//
241247
// But we consider this bad practice and is part of the motivation for deprecating Matches in the first place.
242248
func Matches(err error, match string) bool {
243249
if terr, ok := Wrap(err, nil).(*Error); ok {

errors_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,15 @@ func TestAugmentTerror(t *testing.T) {
276276
assert.Equal(t, base, terr.cause)
277277
}
278278

279+
func TestAugmentTerrorWithWrap(t *testing.T) {
280+
base := NotFound("foo", "failed to find foo", map[string]string{"base": "meta"})
281+
augmentedErr := Augment(base, "added context", map[string]string{"new": "meta"})
282+
assert.Equal(t, "not_found.foo: added context: failed to find foo", augmentedErr.Error())
283+
284+
wrappedErr := Wrap(augmentedErr, map[string]string{"wrap": "meta"})
285+
assert.Equal(t, "not_found.foo: added context: failed to find foo", wrappedErr.Error())
286+
}
287+
279288
func TestAugmentNil(t *testing.T) {
280289
assert.Nil(t, Augment(nil, "added context", map[string]string{
281290
"new": "meta",

0 commit comments

Comments
 (0)