refactor: simply errors (#15067)

This commit is contained in:
Julien Robert
2023-02-20 12:03:40 +00:00
committed by GitHub
parent f69b9ff58f
commit 067ee92d4d
51 changed files with 391 additions and 160 deletions
+8 -8
View File
@@ -31,21 +31,21 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [Unreleased]
### Improvements
* [\#11762](https://github.com/cosmos/cosmos-sdk/pull/11762) Improve error messages
## v1.0.0
### Features
* [\#10779](https://github.com/cosmos/cosmos-sdk/pull/10779) Import code from the `github.com/cosmos/cosmos-sdk/types/errors` package.
* [\#11274](https://github.com/cosmos/cosmos-sdk/pull/11274) Add `RegisterWithGRPCCode` function to associate a gRPC error code with errors.
* [#10779](https://github.com/cosmos/cosmos-sdk/pull/10779) Import code from the `github.com/cosmos/cosmos-sdk/types/errors` package.
* [#11274](https://github.com/cosmos/cosmos-sdk/pull/11274) Add `RegisterWithGRPCCode` function to associate a gRPC error code with errors.
### Improvements
* [#11762](https://github.com/cosmos/cosmos-sdk/pull/11762) Improve error messages.
### API Breaking
* [\#11274](https://github.com/cosmos/cosmos-sdk/pull/11274) `New` now is an alias for `Register` and should only be used in initialization code.
* [#11274](https://github.com/cosmos/cosmos-sdk/pull/11274) `New` now is an alias for `Register` and should only be used in initialization code.
### Bug Fixes
* [\#11714](https://github.com/cosmos/cosmos-sdk/pull/11714) Add wrapped error messages in `GRPCStatus()`
* [#11714](https://github.com/cosmos/cosmos-sdk/pull/11714) Add wrapped error messages in `GRPCStatus()`
+1 -1
View File
@@ -18,7 +18,7 @@ information by using Wrap function, for example:
func safeDiv(val, div int) (int, err) {
if div == 0 {
return 0, errorsmod.Wrapf(ErrZeroDivision, "cannot divide %d", val)
return 0, errors.Wrapf(ErrZeroDivision, "cannot divide %d", val)
}
return val / div, nil
}
+3 -23
View File
@@ -67,12 +67,12 @@ func (s *errorsTestSuite) TestErrorIs() {
},
"successful comparison to a wrapped error": {
a: ErrUnauthorized,
b: errorsmod.Wrap(ErrUnauthorized, "gone"),
b: Wrap(ErrUnauthorized, "gone"),
wantIs: true,
},
"unsuccessful comparison to a wrapped error": {
a: ErrUnauthorized,
b: errorsmod.Wrap(ErrInsufficientFee, "too big"),
b: Wrap(ErrInsufficientFee, "too big"),
wantIs: false,
},
"not equal to stdlib error": {
@@ -82,7 +82,7 @@ func (s *errorsTestSuite) TestErrorIs() {
},
"not equal to a wrapped stdlib error": {
a: ErrUnauthorized,
b: errorsmod.Wrap(fmt.Errorf("stdlib error"), "wrapped"),
b: Wrap(fmt.Errorf("stdlib error"), "wrapped"),
wantIs: false,
},
"nil is nil": {
@@ -224,26 +224,6 @@ func (s *errorsTestSuite) TestGRPCStatus() {
s.Require().Equal("codespace testtesttest code 38: not found: test", status.Message())
}
func ExampleWrap() {
err1 := Wrap(ErrInsufficientFunds, "90 is smaller than 100")
err2 := errorsmod.Wrap(ErrInsufficientFunds, "90 is smaller than 100")
fmt.Println(err1.Error())
fmt.Println(err2.Error())
// Output:
// 90 is smaller than 100: insufficient funds
// 90 is smaller than 100: insufficient funds
}
func ExampleWrapf() {
err1 := Wrap(ErrInsufficientFunds, "90 is smaller than 100")
err2 := errorsmod.Wrap(ErrInsufficientFunds, "90 is smaller than 100")
fmt.Println(err1.Error())
fmt.Println(err2.Error())
// Output:
// 90 is smaller than 100: insufficient funds
// 90 is smaller than 100: insufficient funds
}
const testCodespace = "testtesttest"
var (