Skip to content

Commit

Permalink
Change sdkerrors to errorsmod (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs authored Feb 16, 2023
1 parent 3e0f404 commit dfc5fb0
Show file tree
Hide file tree
Showing 77 changed files with 476 additions and 406 deletions.
2 changes: 1 addition & 1 deletion app/upgrades/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func CreateUpgradeHandler(
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
if err := {module}migration.MigrateStore(ctx, {module}StoreKey, cdc); err != nil {
return vm, sdkerrors.Wrapf(err, "unable to migrate {module} store")
return vm, errorsmod.Wrapf(err, "unable to migrate {module} store")
}
vm[{moduleName}] = mm.GetVersionMap()[{moduleName}]
return mm.RunMigrations(ctx, configurator, vm)
Expand Down
11 changes: 6 additions & 5 deletions app/upgrades/v5/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/types/module"
authz "github.com/cosmos/cosmos-sdk/x/authz"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
Expand Down Expand Up @@ -71,22 +72,22 @@ func CreateUpgradeHandler(
// - stakeibc
logModuleMigration(ctx, currentVersions, claimtypes.ModuleName)
if err := claimmigration.MigrateStore(ctx, claimStoreKey, cdc); err != nil {
return vm, sdkerrors.Wrapf(err, "unable to migrate claim store")
return vm, errorsmod.Wrapf(err, "unable to migrate claim store")
}

logModuleMigration(ctx, currentVersions, icacallbacktypes.ModuleName)
if err := icacallbacksmigration.MigrateStore(ctx, icacallbackStorekey, cdc); err != nil {
return vm, sdkerrors.Wrapf(err, "unable to migrate icacallbacks store")
return vm, errorsmod.Wrapf(err, "unable to migrate icacallbacks store")
}

logModuleMigration(ctx, currentVersions, recordtypes.ModuleName)
if err := recordsmigration.MigrateStore(ctx, recordStoreKey, cdc); err != nil {
return vm, sdkerrors.Wrapf(err, "unable to migrate records store")
return vm, errorsmod.Wrapf(err, "unable to migrate records store")
}

logModuleMigration(ctx, currentVersions, stakeibctypes.ModuleName)
if err := stakeibcmigration.MigrateStore(ctx, stakeibcStoreKey, cdc); err != nil {
return vm, sdkerrors.Wrapf(err, "unable to migrate stakeibc store")
return vm, errorsmod.Wrapf(err, "unable to migrate stakeibc store")
}

// `RunMigrations` (below) checks the old consensus version of each module (found in
Expand Down
8 changes: 5 additions & 3 deletions cmd/strided/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"

errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"
)
Expand Down Expand Up @@ -74,16 +76,16 @@ func SetAddressPrefixes(config *sdk.Config) {
// source: https://github.com/cosmos/cosmos-sdk/blob/v0.43.0-beta1/types/address.go#L141
config.SetAddressVerifier(func(bytes []byte) error {
if len(bytes) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownAddress, "addresses cannot be empty")
return errorsmod.Wrap(sdkerrors.ErrUnknownAddress, "addresses cannot be empty")
}

if len(bytes) > address.MaxAddrLen {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "address max length is %d, got %d", address.MaxAddrLen, len(bytes))
return errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "address max length is %d, got %d", address.MaxAddrLen, len(bytes))
}

// TODO: Do we want to allow addresses of lengths other than 20 and 32 bytes?
if len(bytes) != 20 && len(bytes) != 32 {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "address length must be 20 or 32 bytes, got %d", len(bytes))
return errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "address length must be 20 or 32 bytes, got %d", len(bytes))
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/bech32"

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

channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
Expand All @@ -35,7 +36,7 @@ func Int64ToCoinString(amount int64, denom string) string {

func ValidateAdminAddress(address string) error {
if !Admins[address] {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, fmt.Sprintf("invalid creator address (%s)", address))
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, fmt.Sprintf("invalid creator address (%s)", address))
}
return nil
}
Expand Down Expand Up @@ -106,11 +107,11 @@ func VerifyAddressFormat(bz []byte) error {
}

if len(bz) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownAddress, "addresses cannot be empty")
return errorsmod.Wrap(sdkerrors.ErrUnknownAddress, "addresses cannot be empty")
}

if len(bz) > address.MaxAddrLen {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "address max length is %d, got %d", address.MaxAddrLen, len(bz))
return errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "address max length is %d, got %d", address.MaxAddrLen, len(bz))
}

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

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

Expand Down Expand Up @@ -31,7 +32,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)
}
}
}
16 changes: 9 additions & 7 deletions x/claim/keeper/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"

errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authvestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
Expand Down Expand Up @@ -150,7 +152,7 @@ func (k Keeper) GetAirdropByIdentifier(ctx sdk.Context, airdropIdentifier string
func (k Keeper) GetDistributorAccountBalance(ctx sdk.Context, airdropIdentifier string) (sdk.Coin, error) {
airdrop := k.GetAirdropByIdentifier(ctx, airdropIdentifier)
if airdrop == nil {
return sdk.Coin{}, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid airdrop identifier: GetDistributorAccountBalance")
return sdk.Coin{}, errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid airdrop identifier: GetDistributorAccountBalance")
}

addr, err := k.GetAirdropDistributor(ctx, airdropIdentifier)
Expand Down Expand Up @@ -331,7 +333,7 @@ func (k Keeper) SetClaimRecord(ctx sdk.Context, claimRecord types.ClaimRecord) e
func (k Keeper) GetAirdropDistributor(ctx sdk.Context, airdropIdentifier string) (sdk.AccAddress, error) {
airdrop := k.GetAirdropByIdentifier(ctx, airdropIdentifier)
if airdrop == nil {
return sdk.AccAddress{}, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid airdrop identifier: GetAirdropDistributor")
return sdk.AccAddress{}, errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid airdrop identifier: GetAirdropDistributor")
}
return sdk.AccAddressFromBech32(airdrop.DistributorAddress)
}
Expand All @@ -340,7 +342,7 @@ func (k Keeper) GetAirdropDistributor(ctx sdk.Context, airdropIdentifier string)
func (k Keeper) GetAirdropClaimDenom(ctx sdk.Context, airdropIdentifier string) (string, error) {
airdrop := k.GetAirdropByIdentifier(ctx, airdropIdentifier)
if airdrop == nil {
return "", sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid airdrop identifier: GetAirdropClaimDenom")
return "", errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid airdrop identifier: GetAirdropClaimDenom")
}
return airdrop.ClaimDenom, nil
}
Expand Down Expand Up @@ -457,7 +459,7 @@ func (k Keeper) AfterClaim(ctx sdk.Context, airdropIdentifier string, claimAmoun
// fetch the airdrop
airdrop := k.GetAirdropByIdentifier(ctx, airdropIdentifier)
if airdrop == nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid airdrop identifier: AfterClaim")
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid airdrop identifier: AfterClaim")
}
// increment the claimed so far
err := k.IncrementClaimedSoFar(ctx, airdropIdentifier, claimAmount)
Expand Down Expand Up @@ -486,7 +488,7 @@ func (k Keeper) ClaimAllCoinsForAction(ctx sdk.Context, addr sdk.AccAddress, act
func (k Keeper) ClaimCoinsForAction(ctx sdk.Context, addr sdk.AccAddress, action types.Action, airdropIdentifier string) (sdk.Coins, error) {
isPassed := k.IsInitialPeriodPassed(ctx, airdropIdentifier)
if airdropIdentifier == "" {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid airdrop identifier: ClaimCoinsForAction")
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid airdrop identifier: ClaimCoinsForAction")
}

claimableAmount, err := k.GetClaimableAmountForAction(ctx, addr, action, airdropIdentifier, false)
Expand Down Expand Up @@ -519,7 +521,7 @@ func (k Keeper) ClaimCoinsForAction(ctx sdk.Context, addr sdk.AccAddress, action
// Convert user account into stride veting account.
baseAccount := k.accountKeeper.NewAccountWithAddress(ctx, addr)
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)
}

periodLength := GetAirdropDurationForAction(action)
Expand Down Expand Up @@ -562,7 +564,7 @@ func (k Keeper) ClaimCoinsForAction(ctx sdk.Context, addr sdk.AccAddress, action

airdrop := k.GetAirdropByIdentifier(ctx, airdropIdentifier)
if airdrop == nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid airdrop identifier: ClaimCoinsForAction")
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid airdrop identifier: ClaimCoinsForAction")
}
err = k.AfterClaim(ctx, airdropIdentifier, claimableAmount.AmountOf(airdrop.ClaimDenom))
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions x/claim/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

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

"github.com/Stride-Labs/stride/v5/x/claim/types"
Expand Down Expand Up @@ -41,7 +42,7 @@ func (server msgServer) SetAirdropAllocations(goCtx context.Context, msg *types.
}

if !addr.Equals(airdropDistributor) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid distributor address")
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid distributor address")
}

users, weights := server.keeper.GetUnallocatedUsers(ctx, msg.AirdropIdentifier, msg.Users, msg.Weights)
Expand Down Expand Up @@ -117,7 +118,7 @@ func (server msgServer) DeleteAirdrop(goCtx context.Context, msg *types.MsgDelet
}

if !addr.Equals(distributor) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid distributor address")
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid distributor address")
}

err = server.keeper.DeleteAirdropAndEpoch(ctx, msg.Identifier)
Expand Down
4 changes: 3 additions & 1 deletion x/claim/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

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

"github.com/Stride-Labs/stride/v5/x/claim/types"
Expand All @@ -20,7 +22,7 @@ func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {

switch path[0] {
default:
err = sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
err = errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
}

return res, err
Expand Down
7 changes: 4 additions & 3 deletions x/claim/migrations/v2/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

errorsmod "cosmossdk.io/errors"

oldclaimtypes "github.com/Stride-Labs/stride/v5/x/claim/migrations/v2/types"
claimtypes "github.com/Stride-Labs/stride/v5/x/claim/types"
Expand All @@ -16,14 +17,14 @@ func migrateClaimParams(store sdk.KVStore, cdc codec.Codec) error {
var oldParams oldclaimtypes.Params
err := cdc.UnmarshalJSON(oldParamsBz, &oldParams)
if err != nil {
return sdkerrors.Wrapf(err, "unable to unmarshal claims params using old data types")
return errorsmod.Wrapf(err, "unable to unmarshal claims params using old data types")
}

// Convert and serialize using the new type
newParams := convertToNewClaimParams(oldParams)
newParamsBz, err := cdc.MarshalJSON(&newParams)
if err != nil {
return sdkerrors.Wrapf(err, "unable to marshal claims params using new data types")
return errorsmod.Wrapf(err, "unable to marshal claims params using new data types")
}

// Store new type
Expand Down
18 changes: 8 additions & 10 deletions x/claim/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ package types

// DONTCOVER

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

// x/claim module sentinel errors
var (
ErrTotalWeightNotSet = sdkerrors.Register(ModuleName, 1101,
ErrTotalWeightNotSet = errorsmod.Register(ModuleName, 1101,
"total weight not set")
ErrTotalWeightParse = sdkerrors.Register(ModuleName, 1102,
ErrTotalWeightParse = errorsmod.Register(ModuleName, 1102,
"total weight parse error")
ErrFailedToGetTotalWeight = sdkerrors.Register(ModuleName, 1104,
ErrFailedToGetTotalWeight = errorsmod.Register(ModuleName, 1104,
"failed to get total weight")
ErrFailedToParseDec = sdkerrors.Register(ModuleName, 1105,
ErrFailedToParseDec = errorsmod.Register(ModuleName, 1105,
"failed to parse dec from str")
ErrAirdropAlreadyExists = sdkerrors.Register(ModuleName, 1106,
ErrAirdropAlreadyExists = errorsmod.Register(ModuleName, 1106,
"airdrop with same identifier already exists")
ErrDistributorAlreadyExists = sdkerrors.Register(ModuleName, 1107,
ErrDistributorAlreadyExists = errorsmod.Register(ModuleName, 1107,
"airdrop with same distributor already exists")
ErrInvalidAmount = sdkerrors.Register(ModuleName, 1108,
ErrInvalidAmount = errorsmod.Register(ModuleName, 1108,
"cannot claim negative tokens")
)
Loading

0 comments on commit dfc5fb0

Please sign in to comment.