From 0e942527da7e23596190e80839e95999ebe69958 Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Tue, 3 Sep 2019 08:48:30 -0400 Subject: [PATCH] Fixed error handling in rpc endpoints (#89) --- rpc/eth_api.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/rpc/eth_api.go b/rpc/eth_api.go index 6068b15d..4f9e9d9b 100644 --- a/rpc/eth_api.go +++ b/rpc/eth_api.go @@ -76,31 +76,29 @@ func (e *PublicEthAPI) BlockNumber() *big.Int { } // GetBalance returns the provided account's balance up to the provided block number. -func (e *PublicEthAPI) GetBalance(address common.Address, blockNum rpc.BlockNumber) *hexutil.Big { +func (e *PublicEthAPI) GetBalance(address common.Address, blockNum rpc.BlockNumber) (*hexutil.Big, error) { ctx := e.cliCtx.WithHeight(blockNum.Int64()) res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/balance/%s", types.ModuleName, address), nil) if err != nil { - fmt.Printf("could not resolve: %s\n", err) - return nil + return nil, err } var out types.QueryResBalance e.cliCtx.Codec.MustUnmarshalJSON(res, &out) - return (*hexutil.Big)(out.Balance) + return (*hexutil.Big)(out.Balance), nil } // GetStorageAt returns the contract storage at the given address, block number, and key. -func (e *PublicEthAPI) GetStorageAt(address common.Address, key string, blockNum rpc.BlockNumber) hexutil.Bytes { +func (e *PublicEthAPI) GetStorageAt(address common.Address, key string, blockNum rpc.BlockNumber) (hexutil.Bytes, error) { ctx := e.cliCtx.WithHeight(blockNum.Int64()) res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/storage/%s/%s", types.ModuleName, address, key), nil) if err != nil { - fmt.Printf("could not resolve: %s\n", err) - return nil + return nil, err } var out types.QueryResStorage e.cliCtx.Codec.MustUnmarshalJSON(res, &out) - return out.Value[:] + return out.Value[:], nil } // GetTransactionCount returns the number of transactions at the given address up to the given block number. @@ -140,17 +138,16 @@ func (e *PublicEthAPI) GetUncleCountByBlockNumber(blockNum rpc.BlockNumber) hexu } // GetCode returns the contract code at the given address and block number. -func (e *PublicEthAPI) GetCode(address common.Address, blockNumber rpc.BlockNumber) hexutil.Bytes { +func (e *PublicEthAPI) GetCode(address common.Address, blockNumber rpc.BlockNumber) (hexutil.Bytes, error) { ctx := e.cliCtx.WithHeight(blockNumber.Int64()) res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/code/%s", types.ModuleName, address), nil) if err != nil { - fmt.Printf("could not resolve: %s\n", err) - return nil + return nil, err } var out types.QueryResCode e.cliCtx.Codec.MustUnmarshalJSON(res, &out) - return out.Code + return out.Code, nil } // Sign signs the provided data using the private key of address via Geth's signature standard.