Skip to content

Commit

Permalink
Updated error codes and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Aug 10, 2023
1 parent ac5c80f commit f5acc9f
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 133 deletions.
6 changes: 3 additions & 3 deletions x/deposit/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (k *Keeper) SendCoinsFromAccountToDeposit(ctx sdk.Context, fromAddr, toAddr

deposit.Coins = deposit.Coins.Add(coins...)
if deposit.Coins.IsAnyNegative() {
return types.NewErrorInsufficientFunds(fromAddr, deposit.Coins)
return types.NewErrorInsufficientFunds(fromAddr)
}

k.SetDeposit(ctx, deposit)
Expand All @@ -113,7 +113,7 @@ func (k *Keeper) SendCoinsFromDepositToAccount(ctx sdk.Context, fromAddr, toAddr

deposit.Coins, _ = deposit.Coins.SafeSub(coins)
if deposit.Coins.IsAnyNegative() {
return types.NewErrorInsufficientFunds(fromAddr, coins)
return types.NewErrorInsufficientDeposit(fromAddr)
}

if err := k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, toAddr, coins); err != nil {
Expand Down Expand Up @@ -141,7 +141,7 @@ func (k *Keeper) SendCoinsFromDepositToModule(ctx sdk.Context, fromAddr sdk.AccA

deposit.Coins, _ = deposit.Coins.SafeSub(coins)
if deposit.Coins.IsAnyNegative() {
return types.NewErrorInsufficientFunds(fromAddr, coins)
return types.NewErrorInsufficientDeposit(fromAddr)
}

if err := k.bank.SendCoinsFromModuleToModule(ctx, types.ModuleName, toModule, coins); err != nil {
Expand Down
18 changes: 11 additions & 7 deletions x/deposit/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
)

var (
ErrorInsufficientFunds = errors.Register(ModuleName, 201, "insufficient funds")
ErrorNotFound = errors.Register(ModuleName, 202, "not found")
ErrorDepositNotFound = errors.Register(ModuleName, 201, "deposit not found")
ErrorInsufficientDeposit = errors.Register(ModuleName, 202, "insufficient deposit")
ErrorInsufficientFunds = errors.Register(ModuleName, 203, "insufficient funds")
)

func NewErrorInsufficientFunds(addr sdk.AccAddress, coins sdk.Coins) error {
return errors.Wrapf(ErrorInsufficientFunds, "insufficient deposit funds %s for address %s", coins, addr)
func NewErrorDepositNotFound(addr interface{}) error {
return errors.Wrapf(ErrorDepositNotFound, "deposit for address %s does not exist", addr)
}

func NewErrorDepositNotFound(addr sdk.AccAddress) error {
return errors.Wrapf(ErrorNotFound, "deposit for address %s does not exist", addr)
func NewErrorInsufficientDeposit(addr interface{}) error {
return errors.Wrapf(ErrorInsufficientDeposit, "insufficient deposit for address %s", addr)
}

func NewErrorInsufficientFunds(addr interface{}) error {
return errors.Wrapf(ErrorInsufficientFunds, "insufficient funds for address %s", addr)
}
28 changes: 12 additions & 16 deletions x/node/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,34 @@ package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"

hubtypes "github.com/sentinel-official/hub/types"
)

var (
ErrorInvalidMessage = errors.Register(ModuleName, 101, "invalid message")
)

var (
ErrorDuplicate = errors.Register(ModuleName, 201, "duplicate")
ErrorDuplicateNode = errors.Register(ModuleName, 201, "duplicate node")
ErrorInvalidGigabytes = errors.Register(ModuleName, 202, "invalid gigabytes")
ErrorInvalidHours = errors.Register(ModuleName, 203, "invalid hours")
ErrorInvalidPrices = errors.Register(ModuleName, 204, "invalid prices")
ErrorNotFound = errors.Register(ModuleName, 205, "not found")
ErrorNodeNotFound = errors.Register(ModuleName, 205, "node not found")
)

func NewErrorInvalidPrices(prices sdk.Coins) error {
return errors.Wrapf(ErrorInvalidPrices, "invalid prices %s", prices)
func NewErrorDuplicateNode(addr interface{}) error {
return errors.Wrapf(ErrorDuplicateNode, "node %s already exist", addr)
}

func NewErrorNodeNotFound(addr hubtypes.NodeAddress) error {
return errors.Wrapf(ErrorNotFound, "node %s does not exist", addr)
func NewErrorInvalidGigabytes(gigabytes int64) error {
return errors.Wrapf(ErrorInvalidGigabytes, "invalid gigabytes %d", gigabytes)
}

func NewErrorDuplicateNode(addr hubtypes.NodeAddress) error {
return errors.Wrapf(ErrorDuplicate, "node %s already exists", addr)
func NewErrorInvalidHours(hours int64) error {
return errors.Wrapf(ErrorInvalidHours, "invalid hours %d", hours)
}

func NewErrorInvalidGigabytes(gigabytes int64) error {
return errors.Wrapf(ErrorInvalidGigabytes, "invalid subscription gigabytes %d", gigabytes)
func NewErrorInvalidPrices(prices sdk.Coins) error {
return errors.Wrapf(ErrorInvalidPrices, "invalid prices %s", prices)
}

func NewErrorInvalidHours(hours int64) error {
return errors.Wrapf(ErrorInvalidHours, "invalid subscription hours %d", hours)
func NewErrorNodeNotFound(addr interface{}) error {
return errors.Wrapf(ErrorNodeNotFound, "node %s does not exist", addr)
}
24 changes: 11 additions & 13 deletions x/plan/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@ package types

import (
"github.com/cosmos/cosmos-sdk/types/errors"

hubtypes "github.com/sentinel-official/hub/types"
)

var (
ErrorInvalidMessage = errors.Register(ModuleName, 101, "invalid message")
)

var (
ErrorNotFound = errors.Register(ModuleName, 201, "not found")
ErrorUnauthorized = errors.Register(ModuleName, 202, "unauthorized")
ErrorNodeNotFound = errors.Register(ModuleName, 201, "node not found")
ErrorPlanNotFound = errors.Register(ModuleName, 202, "plan not found")
ErrorProviderNotFound = errors.Register(ModuleName, 203, "provider not found")
ErrorUnauthorized = errors.Register(ModuleName, 204, "unauthorised")
)

func NewErrorProviderNotFound(addr hubtypes.ProvAddress) error {
return errors.Wrapf(ErrorNotFound, "provider %s does not exist", addr)
func NewErrorNodeNotFound(addr interface{}) error {
return errors.Wrapf(ErrorNodeNotFound, "node %s does not exist", addr)
}

func NewErrorPlanNotFound(id uint64) error {
return errors.Wrapf(ErrorNotFound, "plan %d does not exist", id)
return errors.Wrapf(ErrorPlanNotFound, "plan %d does not exist", id)
}

func NewErrorUnauthorized(addr string) error {
return errors.Wrapf(ErrorUnauthorized, "address %s is not authorised", addr)
func NewErrorProviderNotFound(addr interface{}) error {
return errors.Wrapf(ErrorProviderNotFound, "provider %s does not exist", addr)
}

func NewErrorNodeNotFound(addr hubtypes.NodeAddress) error {
return errors.Wrapf(ErrorNotFound, "node %s does not exist", addr)
func NewErrorUnauthorized(addr interface{}) error {
return errors.Wrapf(ErrorUnauthorized, "address %s is not authorized", addr)
}
16 changes: 6 additions & 10 deletions x/provider/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@ package types

import (
"github.com/cosmos/cosmos-sdk/types/errors"

hubtypes "github.com/sentinel-official/hub/types"
)

var (
ErrorInvalidMessage = errors.Register(ModuleName, 101, "invalid message")
)

var (
ErrorDuplicate = errors.Register(ModuleName, 201, "duplicate")
ErrorNotFound = errors.Register(ModuleName, 202, "not found")
ErrorDuplicateProvider = errors.Register(ModuleName, 201, "duplicate provider")
ErrorProviderNotFound = errors.Register(ModuleName, 202, "provider not found")
)

func NewErrorDuplicateProvider(addr hubtypes.ProvAddress) error {
return errors.Wrapf(ErrorDuplicate, "provider %s already exists", addr)
func NewErrorDuplicateProvider(addr interface{}) error {
return errors.Wrapf(ErrorDuplicateProvider, "provider %s already exist", addr)
}

func NewErrorProviderNotFound(addr hubtypes.ProvAddress) error {
return errors.Wrapf(ErrorNotFound, "provider %s does not exist", addr)
func NewErrorProviderNotFound(addr interface{}) error {
return errors.Wrapf(ErrorProviderNotFound, "provider %s does not exist", addr)
}
8 changes: 4 additions & 4 deletions x/session/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ func (k *msgServer) MsgStart(c context.Context, msg *types.MsgStartRequest) (*ty

provAddr := plan.GetProviderAddress()
if _, found = k.GetLatestPayoutForAccountByNode(ctx, provAddr.Bytes(), nodeAddr); !found {
return nil, types.NewErrorPayoutNotFound(provAddr.Bytes(), nodeAddr)
return nil, types.NewErrorPayoutForAddressByNodeNotFound(provAddr, nodeAddr)
}

// Ensure that the node is associated with the plan.
if !k.HasNodeForPlan(ctx, s.PlanID, nodeAddr) {
return nil, types.NewErrorInvalidNode(node.Address)
}
default:
// If the subscription type is not recognized, return an error indicating an invalid subscription type.
return nil, types.NewErrorInvalidSubscriptionType(subscription.GetID(), subscription.Type().String())
// If the subscription type is not recognized, return an error indicating an invalid subscription.
return nil, types.NewErrorInvalidSubscription(subscription.GetID())
}

// Parse the account address from the Bech32 encoded address provided in the message.
Expand Down Expand Up @@ -131,7 +131,7 @@ func (k *msgServer) MsgStart(c context.Context, msg *types.MsgStartRequest) (*ty
session, found := k.GetLatestSessionForAllocation(ctx, subscription.GetID(), accAddr)
if found && session.Status.Equal(hubtypes.StatusActive) {
// If an active session already exists, return an error indicating a duplicate active session.
return nil, types.NewErrorDuplicateActiveSession(subscription.GetID(), accAddr, session.ID)
return nil, types.NewErrorDuplicateActiveSession(session.ID)
}

// Increment the session count to assign a new session ID.
Expand Down
84 changes: 44 additions & 40 deletions x/session/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,87 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"

hubtypes "github.com/sentinel-official/hub/types"
)

var (
ErrorInvalidMessage = errors.Register(ModuleName, 101, "invalid message")
)

var (
ErrorDuplicate = errors.Register(ModuleName, 201, "duplicate")
ErrorInvalidAllocation = errors.Register(ModuleName, 202, "invalid allocation")
ErrorInvalidSignature = errors.Register(ModuleName, 203, "invalid signature")
ErrorInvalidStatus = errors.Register(ModuleName, 204, "invalid status")
ErrorNotFound = errors.Register(ModuleName, 205, "not found")
ErrorUnauthorized = errors.Register(ModuleName, 209, "unauthorized")
ErrorInvalidNode = errors.Register(ModuleName, 210, "invalid node")
ErrorInvalidType = errors.Register(ModuleName, 211, "invalid type")
ErrorAllocationNotFound = errors.Register(ModuleName, 201, "allocation not found")
ErrorDuplicateActiveSession = errors.Register(ModuleName, 202, "duplicate active session")
ErrorInvalidAllocation = errors.Register(ModuleName, 203, "invalid allocation")
ErrorInvalidNode = errors.Register(ModuleName, 204, "invalid node")
ErrorInvalidNodeStatus = errors.Register(ModuleName, 205, "invalid node status")
ErrorInvalidSessionStatus = errors.Register(ModuleName, 206, "invalid session status")
ErrorInvalidSignature = errors.Register(ModuleName, 207, "invalid signature")
ErrorInvalidSubscription = errors.Register(ModuleName, 208, "invalid subscription")
ErrorInvalidSubscriptionStatus = errors.Register(ModuleName, 209, "invalid subscription status")
ErrorNodeNotFound = errors.Register(ModuleName, 210, "node not found")
ErrorPayoutForAddressByNodeNotFound = errors.Register(ModuleName, 211, "payout for address by node not found")
ErrorPlanNotFound = errors.Register(ModuleName, 212, "plan not found")
ErrorSessionNotFound = errors.Register(ModuleName, 213, "session not found")
ErrorSubscriptionNotFound = errors.Register(ModuleName, 214, "subscription not found")
ErrorUnauthorized = errors.Register(ModuleName, 215, "unauthorised")
)

func NewErrorSubscriptionNotFound(id uint64) error {
return errors.Wrapf(ErrorNotFound, "subscription %d does not exit", id)
func NewErrorAllocationNotFound(id uint64, addr interface{}) error {
return errors.Wrapf(ErrorAllocationNotFound, "allocation %d/%s does not exist", id, addr)
}

func NewErrorInvalidSubscriptionStatus(id uint64, status hubtypes.Status) error {
return errors.Wrapf(ErrorInvalidStatus, "invalid status %s for subscription %d", status, id)
func NewErrorDuplicateActiveSession(id uint64) error {
return errors.Wrapf(ErrorDuplicateActiveSession, "active session %d already exist", id)
}

func NewErrorNodeNotFound(addr hubtypes.NodeAddress) error {
return errors.Wrapf(ErrorNotFound, "node %s does not exist", addr)
func NewErrorInvalidAllocation(id uint64, addr interface{}) error {
return errors.Wrapf(ErrorInvalidAllocation, "invalid allocation %d/%s", id, addr)
}

func NewErrorInvalidNodeStatus(addr hubtypes.NodeAddress, status hubtypes.Status) error {
return errors.Wrapf(ErrorInvalidStatus, "invalid status %s for node %s", status, addr)
func NewErrorInvalidNode(addr interface{}) error {
return errors.Wrapf(ErrorInvalidNode, "invalid node %s ", addr)
}

func NewErrorAllocationNotFound(id uint64, addr sdk.AccAddress) error {
return errors.Wrapf(ErrorNotFound, "subscription allocation %d/%s does not exist", id, addr)
func NewErrorInvalidNodeStatus(addr interface{}, status hubtypes.Status) error {
return errors.Wrapf(ErrorInvalidNodeStatus, "invalid status %s for node %s ", status, addr)
}

func NewErrorDuplicateActiveSession(subscriptionID uint64, addr sdk.AccAddress, sessionID uint64) error {
return errors.Wrapf(ErrorDuplicate, "active session %d already exist for subscription allocation %d/%s", sessionID, subscriptionID, addr)
func NewErrorInvalidSessionStatus(id uint64, status hubtypes.Status) error {
return errors.Wrapf(ErrorInvalidSessionStatus, "invalid status %s for session %d", status, id)
}

func NewErrorInvalidAllocation(id uint64, addr sdk.AccAddress) error {
return errors.Wrapf(ErrorInvalidAllocation, "invalid subscription allocation %d/%s", id, addr)
func NewErrorInvalidSignature(signature []byte) error {
return errors.Wrapf(ErrorInvalidSignature, "invalid signature %X", signature)
}

func NewErrorSessionNotFound(id uint64) error {
return errors.Wrapf(ErrorNotFound, "session %d does not exist", id)
func NewErrorInvalidSubscription(id uint64) error {
return errors.Wrapf(ErrorInvalidSubscription, "invalid subscription %d", id)
}

func NewErrorUnauthorized(addr string) error {
return errors.Wrapf(ErrorUnauthorized, "address %s is not authorized", addr)
func NewErrorInvalidSubscriptionStatus(id uint64, status hubtypes.Status) error {
return errors.Wrapf(ErrorInvalidSubscriptionStatus, "invalid status %s for subscription %d", status, id)
}

func NewErrorInvalidSignature(signature []byte) error {
return errors.Wrapf(ErrorInvalidSignature, "invalid bandwidth proof signature %X", signature)
func NewErrorNodeNotFound(addr interface{}) error {
return errors.Wrapf(ErrorNodeNotFound, "node %s does not exist", addr)
}

func NewErrorInvalidSessionStatus(id uint64, status hubtypes.Status) error {
return errors.Wrapf(ErrorInvalidStatus, "invalid status %s for session %d", status, id)
func NewErrorPayoutForAddressByNodeNotFound(accAddr, nodeAddr interface{}) error {
return errors.Wrapf(ErrorPayoutForAddressByNodeNotFound, "payout for address %s by node %s not found", accAddr, nodeAddr)
}

func NewErrorPlanNotFound(id uint64) error {
return errors.Wrapf(ErrorNotFound, "plan %d does not exist", id)
return errors.Wrapf(ErrorPlanNotFound, "plan %d does not exist", id)
}

func NewErrorPayoutNotFound(accAddr sdk.AccAddress, nodeAddr hubtypes.NodeAddress) error {
return errors.Wrapf(ErrorNotFound, "payout for account %s and node %s does not exist", accAddr, nodeAddr)
func NewErrorSessionNotFound(id uint64) error {
return errors.Wrapf(ErrorSessionNotFound, "session %d does not exist", id)
}

func NewErrorInvalidNode(addr string) error {
return errors.Wrapf(ErrorInvalidNode, "invalid node %s", addr)
func NewErrorSubscriptionNotFound(id uint64) error {
return errors.Wrapf(ErrorSubscriptionNotFound, "subscription %s does not exist", id)
}

func NewErrorInvalidSubscriptionType(id uint64, t string) error {
return errors.Wrapf(ErrorInvalidType, "invalid type %s for subscription %d", t, id)
func NewErrorUnauthorized(addr interface{}) error {
return errors.Wrapf(ErrorUnauthorized, "address %s is not authorized", addr)
}
Loading

0 comments on commit f5acc9f

Please sign in to comment.