diff --git a/docs/docs/02-guide/04-blog/04-update.md b/docs/docs/02-guide/04-blog/04-update.md index 1b279231cc..462837b81d 100644 --- a/docs/docs/02-guide/04-blog/04-update.md +++ b/docs/docs/02-guide/04-blog/04-update.md @@ -80,6 +80,7 @@ import ( "blog/x/blog/types" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -94,10 +95,10 @@ func (k msgServer) UpdatePost(goCtx context.Context, msg *types.MsgUpdatePost) ( } val, found := k.GetPost(ctx, msg.Id) if !found { - return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id)) + return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id)) } if msg.Creator != val.Creator { - return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner") + return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner") } k.SetPost(ctx, post) return &types.MsgUpdatePostResponse{}, nil diff --git a/docs/docs/02-guide/04-blog/05-delete.md b/docs/docs/02-guide/04-blog/05-delete.md index 7d3000f3be..1fe784ec2a 100644 --- a/docs/docs/02-guide/04-blog/05-delete.md +++ b/docs/docs/02-guide/04-blog/05-delete.md @@ -33,6 +33,7 @@ import ( "blog/x/blog/types" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -41,10 +42,10 @@ func (k msgServer) DeletePost(goCtx context.Context, msg *types.MsgDeletePost) ( ctx := sdk.UnwrapSDKContext(goCtx) val, found := k.GetPost(ctx, msg.Id) if !found { - return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id)) + return nil, errorsmod.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id)) } if msg.Creator != val.Creator { - return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner") + return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner") } k.RemovePost(ctx, msg.Id) return &types.MsgDeletePostResponse{}, nil @@ -58,7 +59,7 @@ a pointer to a message of type `*types.MsgDeletePostResponse` and an `error`. Inside the function, the context is unwrapped using the `sdk.UnwrapSDKContext` function and the value of the post with the ID specified in the message is retrieved using the `GetPost` function. If the post is not found, an error is -returned using the `sdkerrors.Wrap` function. If the creator of the message does +returned using the `errorsmod.Wrap` function. If the creator of the message does not match the creator of the post, another error is returned. If both of these checks pass, the `RemovePost` function is called with the context and the ID of the post to delete the post. Finally, the function returns a response message diff --git a/docs/docs/02-guide/05-loan/03-request.md b/docs/docs/02-guide/05-loan/03-request.md index a68aed6845..d540ab557c 100644 --- a/docs/docs/02-guide/05-loan/03-request.md +++ b/docs/docs/02-guide/05-loan/03-request.md @@ -76,6 +76,7 @@ import ( // highlight-next-line "strconv" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -83,33 +84,33 @@ import ( func (msg *MsgRequestLoan) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(msg.Creator) if err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err) + return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err) } // highlight-start amount, _ := sdk.ParseCoinsNormalized(msg.Amount) if !amount.IsValid() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "amount is not a valid Coins object") + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "amount is not a valid Coins object") } if amount.Empty() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "amount is empty") + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "amount is empty") } fee, _ := sdk.ParseCoinsNormalized(msg.Fee) if !fee.IsValid() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "fee is not a valid Coins object") + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "fee is not a valid Coins object") } deadline, err := strconv.ParseInt(msg.Deadline, 10, 64) if err != nil { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "deadline is not an integer") + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "deadline is not an integer") } if deadline <= 0 { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "deadline should be a positive integer") + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "deadline should be a positive integer") } collateral, _ := sdk.ParseCoinsNormalized(msg.Collateral) if !collateral.IsValid() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "collateral is not a valid Coins object") + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "collateral is not a valid Coins object") } if collateral.Empty() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "collateral is empty") + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "collateral is empty") } // highlight-end return nil diff --git a/docs/docs/02-guide/05-loan/04-approve.md b/docs/docs/02-guide/05-loan/04-approve.md index 4fb75acf66..d1ff0d5d5d 100644 --- a/docs/docs/02-guide/05-loan/04-approve.md +++ b/docs/docs/02-guide/05-loan/04-approve.md @@ -21,6 +21,7 @@ package keeper import ( "context" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -31,16 +32,16 @@ func (k msgServer) ApproveLoan(goCtx context.Context, msg *types.MsgApproveLoan) ctx := sdk.UnwrapSDKContext(goCtx) loan, found := k.GetLoan(ctx, msg.Id) if !found { - return nil, sdkerrors.Wrapf(sdkerrors.ErrKeyNotFound, "key %d doesn't exist", msg.Id) + return nil, errorsmod.Wrapf(sdkerrors.ErrKeyNotFound, "key %d doesn't exist", msg.Id) } if loan.State != "requested" { - return nil, sdkerrors.Wrapf(types.ErrWrongLoanState, "%v", loan.State) + return nil, errorsmod.Wrapf(types.ErrWrongLoanState, "%v", loan.State) } lender, _ := sdk.AccAddressFromBech32(msg.Creator) borrower, _ := sdk.AccAddressFromBech32(loan.Borrower) amount, err := sdk.ParseCoinsNormalized(loan.Amount) if err != nil { - return nil, sdkerrors.Wrap(types.ErrWrongLoanState, "Cannot parse coins in loan amount") + return nil, errorsmod.Wrap(types.ErrWrongLoanState, "Cannot parse coins in loan amount") } err = k.bankKeeper.SendCoins(ctx, lender, borrower, amount) if err != nil { @@ -94,4 +95,4 @@ import ( var ( ErrWrongLoanState = sdkerrors.Register(ModuleName, 2, "wrong loan state") ) -``` \ No newline at end of file +``` diff --git a/docs/docs/02-guide/05-loan/05-repay.md b/docs/docs/02-guide/05-loan/05-repay.md index 72150f695f..232327d349 100644 --- a/docs/docs/02-guide/05-loan/05-repay.md +++ b/docs/docs/02-guide/05-loan/05-repay.md @@ -25,6 +25,7 @@ package keeper import ( "context" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -35,15 +36,15 @@ func (k msgServer) RepayLoan(goCtx context.Context, msg *types.MsgRepayLoan) (*t ctx := sdk.UnwrapSDKContext(goCtx) loan, found := k.GetLoan(ctx, msg.Id) if !found { - return nil, sdkerrors.Wrapf(sdkerrors.ErrKeyNotFound, "key %d doesn't exist", msg.Id) + return nil, errorsmod.Wrapf(sdkerrors.ErrKeyNotFound, "key %d doesn't exist", msg.Id) } if loan.State != "approved" { - return nil, sdkerrors.Wrapf(types.ErrWrongLoanState, "%v", loan.State) + return nil, errorsmod.Wrapf(types.ErrWrongLoanState, "%v", loan.State) } lender, _ := sdk.AccAddressFromBech32(loan.Lender) borrower, _ := sdk.AccAddressFromBech32(loan.Borrower) if msg.Creator != loan.Borrower { - return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Cannot repay: not the borrower") + return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Cannot repay: not the borrower") } amount, _ := sdk.ParseCoinsNormalized(loan.Amount) fee, _ := sdk.ParseCoinsNormalized(loan.Fee) diff --git a/docs/docs/02-guide/05-loan/06-liquidate.md b/docs/docs/02-guide/05-loan/06-liquidate.md index 8d49058b76..419cc740b1 100644 --- a/docs/docs/02-guide/05-loan/06-liquidate.md +++ b/docs/docs/02-guide/05-loan/06-liquidate.md @@ -23,6 +23,7 @@ import ( "context" "strconv" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -33,13 +34,13 @@ func (k msgServer) LiquidateLoan(goCtx context.Context, msg *types.MsgLiquidateL ctx := sdk.UnwrapSDKContext(goCtx) loan, found := k.GetLoan(ctx, msg.Id) if !found { - return nil, sdkerrors.Wrapf(sdkerrors.ErrKeyNotFound, "key %d doesn't exist", msg.Id) + return nil, errorsmod.Wrapf(sdkerrors.ErrKeyNotFound, "key %d doesn't exist", msg.Id) } if loan.Lender != msg.Creator { - return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Cannot liquidate: not the lender") + return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Cannot liquidate: not the lender") } if loan.State != "approved" { - return nil, sdkerrors.Wrapf(types.ErrWrongLoanState, "%v", loan.State) + return nil, errorsmod.Wrapf(types.ErrWrongLoanState, "%v", loan.State) } lender, _ := sdk.AccAddressFromBech32(loan.Lender) collateral, _ := sdk.ParseCoinsNormalized(loan.Collateral) @@ -48,7 +49,7 @@ func (k msgServer) LiquidateLoan(goCtx context.Context, msg *types.MsgLiquidateL panic(err) } if ctx.BlockHeight() < deadline { - return nil, sdkerrors.Wrap(types.ErrDeadline, "Cannot liquidate before deadline") + return nil, errorsmod.Wrap(types.ErrDeadline, "Cannot liquidate before deadline") } err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, lender, collateral) if err != nil { @@ -62,15 +63,15 @@ func (k msgServer) LiquidateLoan(goCtx context.Context, msg *types.MsgLiquidateL `LiquidateLoan` takes in a context and a `types.MsgLiquidateLoan` message as input and returns a types.MsgLiquidateLoanResponse message and an error as output. -The function first retrieves a loan using the `GetLoan` method and the `Id` field of the input message. If the loan is not found, it returns an error using the `sdkerrors.Wrap` function and the `sdkerrors.ErrKeyNotFound` error code. +The function first retrieves a loan using the `GetLoan` method and the `Id` field of the input message. If the loan is not found, it returns an error using the `errorsmod.Wrap` function and the `sdkerrors.ErrKeyNotFound` error code. -Next, the function checks that the `Creator` field of the input message is the same as the `Lender` field of the loan. If they are not the same, it returns an error using the `sdkerrors.Wrap` function and the `sdkerrors.ErrUnauthorized` error code. +Next, the function checks that the `Creator` field of the input message is the same as the `Lender` field of the loan. If they are not the same, it returns an error using the `errorsmod.Wrap` function and the `sdkerrors.ErrUnauthorized` error code. -The function then checks that the State field of the loan is equal to "approved". If it is not, it returns an error using the `sdkerrors.Wrapf` function and the `types.ErrWrongLoanState` error code. +The function then checks that the State field of the loan is equal to "approved". If it is not, it returns an error using the `errorsmod.Wrapf` function and the `types.ErrWrongLoanState` error code. The function then converts the Lender field of the loan to an address using the `sdk.AccAddressFromBech32` function and the `Collateral` field to coins using the `sdk.ParseCoinsNormalized` function. It also converts the `Deadline` field to an integer using the `strconv.ParseInt` function. If this function returns an error, it panics. -Finally, the function checks that the current block height is greater than or equal to the deadline. If it is not, it returns an error using the `sdkerrors.Wrap` function and the `types.ErrDeadline` error code. If all checks pass, the function uses the `bankKeeper.SendCoinsFromModuleToAccount` method to transfer the collateral from the module account to the lender's account and updates the `State` field of the loan to `"liquidated"`. It then stores the updated loan using the `SetLoan` method and returns a `types.MsgLiquidateLoanResponse` message with no error. +Finally, the function checks that the current block height is greater than or equal to the deadline. If it is not, it returns an error using the `errorsmod.Wrap` function and the `types.ErrDeadline` error code. If all checks pass, the function uses the `bankKeeper.SendCoinsFromModuleToAccount` method to transfer the collateral from the module account to the lender's account and updates the `State` field of the loan to `"liquidated"`. It then stores the updated loan using the `SetLoan` method and returns a `types.MsgLiquidateLoanResponse` message with no error. ## Register a custom error diff --git a/docs/docs/02-guide/05-loan/07-cancel.md b/docs/docs/02-guide/05-loan/07-cancel.md index 3997072a92..c1d788454a 100644 --- a/docs/docs/02-guide/05-loan/07-cancel.md +++ b/docs/docs/02-guide/05-loan/07-cancel.md @@ -25,13 +25,13 @@ func (k msgServer) CancelLoan(goCtx context.Context, msg *types.MsgCancelLoan) ( ctx := sdk.UnwrapSDKContext(goCtx) loan, found := k.GetLoan(ctx, msg.Id) if !found { - return nil, sdkerrors.Wrapf(sdkerrors.ErrKeyNotFound, "key %d doesn't exist", msg.Id) + return nil, errorsmod.Wrapf(sdkerrors.ErrKeyNotFound, "key %d doesn't exist", msg.Id) } if loan.Borrower != msg.Creator { - return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Cannot cancel: not the borrower") + return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Cannot cancel: not the borrower") } if loan.State != "requested" { - return nil, sdkerrors.Wrapf(types.ErrWrongLoanState, "%v", loan.State) + return nil, errorsmod.Wrapf(types.ErrWrongLoanState, "%v", loan.State) } borrower, _ := sdk.AccAddressFromBech32(loan.Borrower) collateral, _ := sdk.ParseCoinsNormalized(loan.Collateral) @@ -53,16 +53,16 @@ The function begins by using the `sdk.UnwrapSDKContext` method to get the `sdk.Context` from the `context.Context` object. It then uses the `GetLoan` method of the `msgServer` type to retrieve a loan identified by the `Id` field of the `msg` argument. If the loan is not found, the function returns an error -using the `sdk.ErrKeyNotFound` error wrapped with the `sdk.Wrap` method. +using the `sdk.ErrKeyNotFound` error wrapped with the `errorsmod.Wrap` method. Next, the function checks if the `Creator` field of the msg argument is the same as the `Borrower` field of the loan. If they are not the same, the function returns an error using the `sdk.ErrUnauthorized` error wrapped with the -`sdk.Wrap` method. +`errorsmod.Wrap` method. The function then checks if the `State` field of the loan is equal to the string `"requested"`. If it is not, the function returns an error using the -types.`ErrWrongLoanState` error wrapped with the `sdk.Wrapf` method. +types.`ErrWrongLoanState` error wrapped with the `errorsmod.Wrapf` method. If the loan has the correct state and the creator of the message is the borrower of the loan, the function proceeds to send the collateral coins held in the @@ -70,4 +70,4 @@ of the loan, the function proceeds to send the collateral coins held in the `SendCoinsFromModuleToAccount` method of the `bankKeeper`. The function then updates the State field of the loan to the string "cancelled" and sets the updated loan using the `SetLoan` method. Finally, the function returns a -`types.MsgCancelLoanResponse` object and a nil error. \ No newline at end of file +`types.MsgCancelLoanResponse` object and a nil error. diff --git a/docs/docs/06-migration/v0.24.0.md b/docs/docs/06-migration/v0.24.0.md index bcf1b85185..7b803cce85 100644 --- a/docs/docs/06-migration/v0.24.0.md +++ b/docs/docs/06-migration/v0.24.0.md @@ -241,7 +241,7 @@ package module_name func (im IBCModule) OnChanOpenInit( /*...*/ ) (string, error) { //... - return "", sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort) + return "", errorsmod.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort) //... } ``` @@ -259,8 +259,8 @@ package module_name func (im IBCModule) OnRecvPacket( /*...*/ ) { //... if err := modulePacketData.Unmarshal(modulePacket.GetData()); err != nil { - // return channeltypes.NewErrorAcknowledgement(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error()).Error()) - return channeltypes.NewErrorAcknowledgement(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error())) + // return channeltypes.NewErrorAcknowledgement(errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error()).Error()) + return channeltypes.NewErrorAcknowledgement(errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error())) } // ... diff --git a/docs/docs/06-migration/v0.26.0.md b/docs/docs/06-migration/v0.26.0.md index 8dae447d15..de3576dbe8 100644 --- a/docs/docs/06-migration/v0.26.0.md +++ b/docs/docs/06-migration/v0.26.0.md @@ -165,7 +165,7 @@ func (k Keeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error { capName := host.ChannelCapabilityPath(portID, channelID) chanCap, ok := k.scopedKeeper.GetCapability(ctx, capName) if !ok { - return sdkerrors.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName) + return errorsmod.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName) } return k.channelKeeper.ChanCloseInit(ctx, portID, channelID, chanCap) }