Skip to content

Commit

Permalink
fix: wrappedError.Is (#8355)
Browse files Browse the repository at this point in the history
* fix: wrappedError.Is

Is method should return true when used on 2 same errors.

* Changelog update

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 19, 2021
1 parent a334a59 commit 93c2b5e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ sure you are aware of any relevant breaking changes.
* (x/slashing) [\#6212](https://github.com/cosmos/cosmos-sdk/pull/6212) Remove `Get*` prefixes from key construction functions
* (server) [\#6079](https://github.com/cosmos/cosmos-sdk/pull/6079) Remove `UpgradeOldPrivValFile` (deprecated in Tendermint Core v0.28).
* [\#5719](https://github.com/cosmos/cosmos-sdk/pull/5719) Bump Go requirement to 1.14+


### State Machine Breaking

Expand Down
5 changes: 2 additions & 3 deletions types/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,11 @@ func (e *wrappedError) Cause() error {

// Is reports whether any error in e's chain matches a target.
func (e *wrappedError) Is(target error) bool {
if target == nil {
return e == target
if e == target {
return true
}

w := e.Cause()

for {
if w == target {
return true
Expand Down
15 changes: 11 additions & 4 deletions types/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,24 @@ func (s *errorsTestSuite) TestWrapEmpty() {
}

func (s *errorsTestSuite) TestWrappedIs() {
require := s.Require()
err := Wrap(ErrTxTooLarge, "context")
s.Require().True(stdlib.Is(err, ErrTxTooLarge))
require.True(stdlib.Is(err, ErrTxTooLarge))

err = Wrap(err, "more context")
s.Require().True(stdlib.Is(err, ErrTxTooLarge))
require.True(stdlib.Is(err, ErrTxTooLarge))

err = Wrap(err, "even more context")
s.Require().True(stdlib.Is(err, ErrTxTooLarge))
require.True(stdlib.Is(err, ErrTxTooLarge))

err = Wrap(ErrInsufficientFee, "...")
s.Require().False(stdlib.Is(err, ErrTxTooLarge))
require.False(stdlib.Is(err, ErrTxTooLarge))

errs := stdlib.New("other")
require.True(stdlib.Is(errs, errs))

errw := &wrappedError{"msg", errs}
require.True(errw.Is(errw), "should match itself")
}

func (s *errorsTestSuite) TestWrappedIsMultiple() {
Expand Down

0 comments on commit 93c2b5e

Please sign in to comment.