Skip to content

Commit

Permalink
chore(lint): start using new errors module
Browse files Browse the repository at this point in the history
  • Loading branch information
ivivanov authored and Lockwarr committed Dec 22, 2023
1 parent df962af commit 991d1fb
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
11 changes: 11 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ run:
output:
# Sort results by: filepath, line and column.
sort-results: true

issues:
# Maximum issues count per one linter.
# Set to 0 to disable.
# Default: 50
max-issues-per-linter: 0
# Maximum count of issues with the same text.
# Set to 0 to disable.
# Default: 3
max-same-issues: 0

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ require (

replace (
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0

// fixes build problem: "cometbft/cometbft/crypto/sr25519/pubkey.go:58:9: too many return values"
github.com/ChainSafe/go-schnorrkel => github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d
github.com/CosmWasm/wasmd => github.com/neutron-org/wasmd v0.40.0-rc.0.0.20230705143547-22c391d461d5

// TODO: Simapp dependency, review removing when updating to SDK with backported update https://github.com/cosmos/cosmos-sdk/issues/13423
Expand Down
3 changes: 2 additions & 1 deletion x/tax/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tax
import (
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/Nolus-Protocol/nolus-core/x/tax/keeper"
"github.com/Nolus-Protocol/nolus-core/x/tax/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -17,7 +18,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
switch msg := msg.(type) {
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
9 changes: 5 additions & 4 deletions x/tax/keeper/taxdecorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/Nolus-Protocol/nolus-core/x/tax/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -32,7 +33,7 @@ func NewDeductTaxDecorator(ak types.AccountKeeper, bk types.BankKeeper, tk Keepe
func (dtd DeductTaxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

// If fees are not specified we call the next AnteHandler
Expand All @@ -44,7 +45,7 @@ func (dtd DeductTaxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
// Ensures the module treasury address has been set
treasuryAddr, err := sdk.AccAddressFromBech32(dtd.tk.ContractAddress(ctx))
if err != nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnknownAddress, fmt.Sprintf("invalid treasury smart contract address: %s", err.Error()))
return ctx, errorsmod.Wrap(sdkerrors.ErrUnknownAddress, fmt.Sprintf("invalid treasury smart contract address: %s", err.Error()))
}

// Ensure not more then one denom for paying tx costs
Expand All @@ -63,7 +64,7 @@ func (dtd DeductTaxDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo

baseDenom := dtd.tk.BaseDenom(ctx)
if baseDenom != feeCoin.Denom {
return ctx, sdkerrors.Wrap(types.ErrInvalidFeeDenom, txFees[0].Denom)
return ctx, errorsmod.Wrap(types.ErrInvalidFeeDenom, txFees[0].Denom)
}

if err = deductTax(ctx, dtd.tk, dtd.bk, feeCoin, treasuryAddr); err != nil {
Expand Down Expand Up @@ -103,7 +104,7 @@ func deductTax(ctx sdk.Context, taxKeeper Keeper, bankKeeper types.BankKeeper, f
// Send tax from fee collector to the treasury smart contract address
err := bankKeeper.SendCoinsFromModuleToAccount(ctx, authtypes.FeeCollectorName, treasuryAddr, sdk.Coins{tax})
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions x/tax/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"strings"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -102,7 +102,7 @@ func validateContractAddress(v interface{}) error {

_, err := sdk.AccAddressFromBech32(contractAddress)
if err != nil {
return sdkerrors.Wrap(ErrInvalidAddress, err.Error())
return errorsmod.Wrap(ErrInvalidAddress, err.Error())
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion x/vestings/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vestings
import (
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

Expand All @@ -21,7 +22,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
7 changes: 4 additions & 3 deletions x/vestings/keeper/msg_server_create_vesting_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -32,16 +33,16 @@ func (k msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCre
}

if bk.BlockedAddr(to) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress)
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress)
}

if acc := ak.GetAccount(ctx, to); acc != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "account %s already exists", msg.ToAddress)
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "account %s already exists", msg.ToAddress)
}

baseAccount := ak.NewAccountWithAddress(ctx, to)
if _, ok := baseAccount.(*authtypes.BaseAccount); !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid account type; expected: BaseAccount, got: %T", baseAccount)
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid account type; expected: BaseAccount, got: %T", baseAccount)
}

baseVestingAccount := vestingtypes.NewBaseVestingAccount(baseAccount.(*authtypes.BaseAccount), msg.Amount.Sort(), msg.EndTime)
Expand Down
15 changes: 8 additions & 7 deletions x/vestings/types/message_create_vesting_account.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -31,30 +32,30 @@ func (msg MsgCreateVestingAccount) Type() string { return TypeMsgCreateVestingAc
// ValidateBasic Implements Msg.
func (msg MsgCreateVestingAccount) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid 'from' address: %s", err)
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid 'from' address: %s", err)
}
if _, err := sdk.AccAddressFromBech32(msg.ToAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid 'to' address: %s", err)
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid 'to' address: %s", err)
}

if !msg.Amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}

if !msg.Amount.IsAllPositive() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}

if msg.StartTime <= 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid start time")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid start time")
}

if msg.EndTime <= 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid end time")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid end time")
}

if msg.StartTime >= msg.EndTime {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid start time")
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid start time")
}

return nil
Expand Down

0 comments on commit 991d1fb

Please sign in to comment.