Skip to content

Commit

Permalink
refactor: error handling improvements (#9)
Browse files Browse the repository at this point in the history
* issue on load.go

* fixed tests

* added client offline

* client_online.go

* added error handling on utils.go and unified both files

* added plugins.go

* added config, converter and libraries

* linted and fixed issues

* fixed some issues
  • Loading branch information
bizk authored Sep 1, 2023
1 parent 6f5fcc6 commit 7a36558
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 176 deletions.
21 changes: 11 additions & 10 deletions client_offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rosetta
import (
"context"
"encoding/hex"
"fmt"

"github.com/coinbase/rosetta-sdk-go/types"

Expand Down Expand Up @@ -48,17 +49,17 @@ func (c *Client) ConstructionPayload(_ context.Context, request *types.Construct

tx, err := c.converter.ToSDK().UnsignedTx(request.Operations)
if err != nil {
return nil, crgerrs.WrapError(crgerrs.ErrInvalidOperation, err.Error())
return nil, crgerrs.WrapError(crgerrs.ErrOffline, fmt.Sprintf("getting unsigned tx %s", err.Error()))
}

metadata := new(ConstructionMetadata)
if err = metadata.FromMetadata(request.Metadata); err != nil {
return nil, err
return nil, crgerrs.WrapError(crgerrs.ErrOffline, fmt.Sprintf("getting metadata from request %s", err.Error()))
}

txBytes, payloads, err := c.converter.ToRosetta().SigningComponents(tx, metadata, request.PublicKeys)
if err != nil {
return nil, err
return nil, crgerrs.WrapError(crgerrs.ErrOffline, fmt.Sprintf("getting signed components %s", err.Error()))
}

return &types.ConstructionPayloadsResponse{
Expand All @@ -75,13 +76,13 @@ func (c *Client) PreprocessOperationsToOptions(_ context.Context, req *types.Con
// now we need to parse the operations to cosmos sdk messages
tx, err := c.converter.ToSDK().UnsignedTx(req.Operations)
if err != nil {
return nil, err
return nil, crgerrs.WrapError(crgerrs.ErrOffline, fmt.Sprintf("converting unsigned tx %s", err.Error()))
}

// get the signers
signers, err := tx.GetSigners()
if err != nil {
return nil, err
return nil, crgerrs.WrapError(crgerrs.ErrOffline, fmt.Sprintf("getting signers from unsigned tx %s", err.Error()))
}

signersStr := make([]string, len(signers))
Expand All @@ -98,15 +99,15 @@ func (c *Client) PreprocessOperationsToOptions(_ context.Context, req *types.Con
meta := new(ConstructionPreprocessMetadata)
err = meta.FromMetadata(req.Metadata)
if err != nil {
return nil, err
return nil, crgerrs.WrapError(crgerrs.ErrOffline, fmt.Sprintf("parsing metadata %s", err.Error()))
}

if meta.GasPrice == "" {
return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "no gas prices")
return nil, crgerrs.WrapError(crgerrs.ErrOffline, "no gas price")
}

if meta.GasLimit == 0 {
return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "no gas limit")
return nil, crgerrs.WrapError(crgerrs.ErrOffline, "no gas limit")
}

// prepare the options to return
Expand All @@ -119,7 +120,7 @@ func (c *Client) PreprocessOperationsToOptions(_ context.Context, req *types.Con

metaOptions, err := options.ToMetadata()
if err != nil {
return nil, err
return nil, crgerrs.WrapError(crgerrs.ErrOffline, fmt.Sprintf("parsing metadata %s", err.Error()))
}
return &types.ConstructionPreprocessResponse{
Options: metaOptions,
Expand All @@ -130,7 +131,7 @@ func (c *Client) PreprocessOperationsToOptions(_ context.Context, req *types.Con
func (c *Client) AccountIdentifierFromPublicKey(pubKey *types.PublicKey) (*types.AccountIdentifier, error) {
pk, err := c.converter.ToSDK().PubKey(pubKey)
if err != nil {
return nil, err
return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("converting pub key to sdk %s", err.Error()))
}

return &types.AccountIdentifier{
Expand Down
Loading

0 comments on commit 7a36558

Please sign in to comment.