Skip to content

Commit

Permalink
Merge pull request #41 from monzo/dont-lose-cause-context-with-wrap
Browse files Browse the repository at this point in the history
Keep cause error context when calling Wrap
  • Loading branch information
suhailpatel authored Sep 28, 2022
2 parents 238cbfb + 33980ab commit c706d80
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 8 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
// user defined error parameters.
//
// Terrors can be used to wrap any object that satisfies the error interface:
//
// terr := terrors.Wrap(err, map[string]string{"context": "my_context"})
//
// Terrors can be instantiated directly:
// err := terrors.New("not_found", "object not found", map[string]string{
//
// err := terrors.New("not_found", "object not found", map[string]string{
// "context": "my_context"
// })
//
// Terrors offers built-in functions for instantiating Errors with common codes:
//
// err := terrors.NotFound("config_file", "config file not found", map[string]string{
// "context": my_context
// })
Expand Down Expand Up @@ -208,6 +211,7 @@ func addParams(err *Error, params map[string]string) *Error {
Params: copiedParams,
StackFrames: err.StackFrames,
IsRetryable: err.IsRetryable,
cause: err.cause,
}
}

Expand Down Expand Up @@ -237,7 +241,9 @@ func (p *Error) PrefixMatches(prefixParts ...string) bool {
// `PrefixMatches`, so if you were previously matching against a part of the string returned from error.Error() that
// is _not_ the prefix, then this will be a breaking change. In this case you should update the string to match the
// prefix. If this is not possible, you can match against the entire error string explicitly, for example:
// strings.Contains(err.Error(), "context deadline exceeded")
//
// strings.Contains(err.Error(), "context deadline exceeded")
//
// But we consider this bad practice and is part of the motivation for deprecating Matches in the first place.
func Matches(err error, match string) bool {
if terr, ok := Wrap(err, nil).(*Error); ok {
Expand Down
9 changes: 9 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ func TestAugmentTerror(t *testing.T) {
assert.Equal(t, base, terr.cause)
}

func TestAugmentTerrorWithWrap(t *testing.T) {
base := NotFound("foo", "failed to find foo", map[string]string{"base": "meta"})
augmentedErr := Augment(base, "added context", map[string]string{"new": "meta"})
assert.Equal(t, "not_found.foo: added context: failed to find foo", augmentedErr.Error())

wrappedErr := Wrap(augmentedErr, map[string]string{"wrap": "meta"})
assert.Equal(t, "not_found.foo: added context: failed to find foo", wrappedErr.Error())
}

func TestAugmentNil(t *testing.T) {
assert.Nil(t, Augment(nil, "added context", map[string]string{
"new": "meta",
Expand Down

0 comments on commit c706d80

Please sign in to comment.