Skip to content

Commit

Permalink
Update x/token errors
Browse files Browse the repository at this point in the history
  • Loading branch information
0Tech committed Dec 8, 2022
1 parent 2e833db commit 7a2ce69
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
50 changes: 45 additions & 5 deletions x/token/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package token

import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

sdkerrors "github.com/line/lbm-sdk/types/errors"
)

Expand All @@ -9,9 +12,9 @@ const tokenCodespace = ModuleName
var (
ErrInvalidContractID = sdkerrors.Register(tokenCodespace, 2, "invalid contract id")
ErrContractNotFound = sdkerrors.Register(tokenCodespace, 3, "contract not found")
ErrInvalidPermission = sdkerrors.Register(tokenCodespace, 4, "invalid permission")
ErrGrantNotFound = sdkerrors.Register(tokenCodespace, 5, "grant not found")
ErrGrantAlreadyExists = sdkerrors.Register(tokenCodespace, 6, "grant already exists")
ErrWrongContract = sdkerrors.Register(tokenCodespace, 4, "contract not supports this feature")
ErrInvalidPermission = sdkerrors.Register(tokenCodespace, 5, "invalid permission")
ErrGrantNotFound = sdkerrors.Register(tokenCodespace, 6, "grant not found")
ErrOperatorIsHolder = sdkerrors.Register(tokenCodespace, 7, "operator and holder should be different")
ErrAuthorizationNotFound = sdkerrors.Register(tokenCodespace, 8, "authorization not found")
ErrAuthorizationAlreadyExists = sdkerrors.Register(tokenCodespace, 9, "authorization already exists")
Expand All @@ -22,6 +25,43 @@ var (
ErrInvalidImageURI = sdkerrors.Register(tokenCodespace, 14, "invalid image_uri")
ErrInvalidMeta = sdkerrors.Register(tokenCodespace, 15, "invalid meta")
ErrInvalidDecimals = sdkerrors.Register(tokenCodespace, 16, "invalid decimals")
ErrNotMintable = sdkerrors.Register(tokenCodespace, 17, "not mintable")
ErrInvalidChanges = sdkerrors.Register(tokenCodespace, 18, "invalid changes")
ErrInvalidChanges = sdkerrors.Register(tokenCodespace, 17, "invalid changes")

sdkToGRPC = map[*sdkerrors.Error]codes.Code{
// this codespace
ErrContractNotFound: codes.NotFound,
ErrGrantNotFound: codes.NotFound,
ErrAuthorizationNotFound: codes.NotFound,
ErrInvalidContractID: codes.InvalidArgument,
ErrInvalidPermission: codes.InvalidArgument,
ErrOperatorIsHolder: codes.InvalidArgument,
ErrInvalidAmount: codes.InvalidArgument,
ErrInvalidName: codes.InvalidArgument,
ErrInvalidDecimals: codes.InvalidArgument,
ErrInvalidImageURI: codes.InvalidArgument,
ErrWrongContract: codes.InvalidArgument,
ErrInvalidChanges: codes.InvalidArgument,
ErrInvalidMeta: codes.InvalidArgument,
ErrInsufficientTokens: codes.FailedPrecondition,
ErrAuthorizationAlreadyExists: codes.AlreadyExists,

// sdk codespace
sdkerrors.ErrInvalidAddress: codes.InvalidArgument,
sdkerrors.ErrInvalidType: codes.InvalidArgument,
sdkerrors.ErrUnauthorized: codes.PermissionDenied,
}
)

func SDKErrorToGRPCError(err error) error {
if err == nil {
return nil
}

for sdkerror, grpcCode := range sdkToGRPC {
if sdkerror.Is(err) {
return status.Error(grpcCode, sdkerror.Error())
}
}

panic("unknown error")
}
3 changes: 0 additions & 3 deletions x/token/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ func (s msgServer) GrantPermission(c context.Context, req *token.MsgGrantPermiss
if _, err := s.keeper.GetGrant(ctx, req.ContractId, granter, permission); err != nil {
return nil, err
}
if _, err := s.keeper.GetGrant(ctx, req.ContractId, grantee, permission); err == nil {
return nil, token.ErrGrantAlreadyExists.Wrapf("%s already granted for %s", grantee, permission)
}

s.keeper.Grant(ctx, req.ContractId, granter, grantee, permission)

Expand Down
5 changes: 0 additions & 5 deletions x/token/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,6 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() {
permission: token.LegacyPermissionModify.String(),
valid: true,
},
"already granted": {
granter: s.vendor,
grantee: s.operator,
permission: token.LegacyPermissionMint.String(),
},
"granter has no permission": {
granter: s.customer,
grantee: s.operator,
Expand Down

0 comments on commit 7a2ce69

Please sign in to comment.