From abf5fbe7e43ce1715f68dc289d54e09dd1df7b48 Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Sat, 27 Apr 2024 12:08:17 -0400 Subject: [PATCH] fix: improved error checking around nils Signed-off-by: Chris Gianelloni --- client/accounts.go | 18 ++++++++++++++++ client/addresses.go | 24 ++++++++++++++++++++++ client/asset.go | 18 ++++++++++++++++ client/asset_policy.go | 15 ++++++++++++++ client/block_info.go | 6 ++++++ client/client.go | 44 +++++++++++++++++++++++++++------------- client/datum.go | 3 +++ client/ecosystem.go | 3 +++ client/epochs.go | 6 ++++++ client/general.go | 12 +++++++++++ client/linear_vesting.go | 9 ++++++++ client/pools.go | 24 ++++++++++++++++++++++ client/scripts.go | 3 +++ client/transactions.go | 28 +++++++++++++++++++++++-- client/txmanager.go | 12 +++++++++++ main.go | 8 +++++++- 16 files changed, 216 insertions(+), 17 deletions(-) diff --git a/client/accounts.go b/client/accounts.go index da2e686..11f8262 100644 --- a/client/accounts.go +++ b/client/accounts.go @@ -22,6 +22,9 @@ func (c *Client) AccountAddresses( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -47,6 +50,9 @@ func (c *Client) AccountAssets( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -72,6 +78,9 @@ func (c *Client) StakeAccountHistory( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -92,6 +101,9 @@ func (c *Client) StakeAccountInformation( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -118,6 +130,9 @@ func (c *Client) StakeAccountRewards( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -143,6 +158,9 @@ func (c *Client) StakeAccountUpdates( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/addresses.go b/client/addresses.go index 2086e98..314c3b2 100644 --- a/client/addresses.go +++ b/client/addresses.go @@ -15,6 +15,9 @@ func (c *Client) DecodeAddress(address string) (*models.DecodedAddress, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -33,6 +36,9 @@ func (c *Client) AddressTransactionCount(address string) (*models.AddressTransac if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -58,6 +64,9 @@ func (c *Client) AddressTransactions( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -83,6 +92,9 @@ func (c *Client) PaymentCredentialTransactions( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -108,6 +120,9 @@ func (c *Client) UtxoReferencesAtAddress( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -133,6 +148,9 @@ func (c *Client) UtxosAtAddress( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -159,6 +177,9 @@ func (c *Client) UtxosAtAddresses( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -184,6 +205,9 @@ func (c *Client) UtxosByPaymentCredential( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/asset.go b/client/asset.go index 42ebe51..9413977 100644 --- a/client/asset.go +++ b/client/asset.go @@ -22,6 +22,9 @@ func (c *Client) AccountsHoldingAsset( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -47,6 +50,9 @@ func (c *Client) AddressHoldingAsset( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -65,6 +71,9 @@ func (c *Client) Asset(assetId string) (*models.AssetInformations, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -90,6 +99,9 @@ func (c *Client) AssetTransactions( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -115,6 +127,9 @@ func (c *Client) AssetUpdates( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -138,6 +153,9 @@ func (c *Client) AssetUtxos(assetId string, params *utils.Parameters) (*models.A if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/asset_policy.go b/client/asset_policy.go index 2fc7cbf..3f81005 100644 --- a/client/asset_policy.go +++ b/client/asset_policy.go @@ -22,6 +22,9 @@ func (c *Client) AccountsHoldingPolicy( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -47,6 +50,9 @@ func (c *Client) AddressesHoldingPolicy( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -72,6 +78,9 @@ func (c *Client) SpecificPolicyInformations( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -97,6 +106,9 @@ func (c *Client) TransactionsMovingPolicy( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -122,6 +134,9 @@ func (c *Client) UtxosContainingPolicy( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/block_info.go b/client/block_info.go index 3cc614c..a3f1170 100644 --- a/client/block_info.go +++ b/client/block_info.go @@ -51,6 +51,9 @@ func (c *Client) BlockInfo(blockHeight int64) (*BlockInfo, error) { if err != nil { return nil, err } + if req == nil { + return nil, fmt.Errorf("empty request") + } req.Header.Set("Content-Type", "application/json") @@ -73,6 +76,9 @@ func (c *Client) LatestBlock() (*BlockInfo, error) { if err != nil { return nil, err } + if req == nil { + return nil, fmt.Errorf("empty request") + } req.Header.Set("Content-Type", "application/json") diff --git a/client/client.go b/client/client.go index 9c51fb3..4b5a535 100644 --- a/client/client.go +++ b/client/client.go @@ -44,33 +44,40 @@ type errorResponse struct { // } func (c *Client) sendRequest(req *http.Request, responseBody *string) error { + if req == nil { + return fmt.Errorf("empty request") + } req.Header.Set("Accept", "application/json") req.Header.Add("api-key", c.apiKey) - res, err := c.HTTPClient.Do(req) + if c.HTTPClient == nil { + return fmt.Errorf("missing http client") + } + resp, err := c.HTTPClient.Do(req) if err != nil { return err } - - defer res.Body.Close() + if resp == nil { + return fmt.Errorf("empty response") + } // Try to unmarshall into errorResponse - if res.StatusCode != http.StatusOK { + if resp.StatusCode != http.StatusOK { var errRes errorResponse - if err = json.NewDecoder(res.Body).Decode(&errRes); err == nil { + if err = json.NewDecoder(resp.Body).Decode(&errRes); err == nil { return errors.New(errRes.Message) } - return fmt.Errorf("unknown error, status code: %d", res.StatusCode) + return fmt.Errorf("unknown error, status code: %d", resp.StatusCode) } - respBodyBytes, err := io.ReadAll(res.Body) + respBodyBytes, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read body: %s", err) } - defer res.Body.Close() + defer resp.Body.Close() *responseBody = string(respBodyBytes) @@ -79,11 +86,14 @@ func (c *Client) sendRequest(req *http.Request, responseBody *string) error { func (c *Client) get(url string) (*http.Response, error) { req, err := http.NewRequest("GET", c.BaseUrl+url, nil) - req.Header.Set("Accept", "application/json") - req.Header.Add("api-key", c.apiKey) if err != nil { return nil, err } + if req == nil { + return nil, fmt.Errorf("empty request") + } + req.Header.Set("Accept", "application/json") + req.Header.Add("api-key", c.apiKey) return c.HTTPClient.Do(req) } @@ -93,22 +103,28 @@ func (c *Client) post(url string, body interface{}) (*http.Response, error) { return nil, err } req, err := http.NewRequest("POST", c.BaseUrl+url, bytes.NewReader(jsonBody)) - req.Header.Set("Accept", "application/json") - req.Header.Add("api-key", c.apiKey) if err != nil { return nil, err } + if req == nil { + return nil, fmt.Errorf("empty request") + } + req.Header.Set("Accept", "application/json") + req.Header.Add("api-key", c.apiKey) req.Header.Set("Content-Type", "application/json") return c.HTTPClient.Do(req) } func (c *Client) postBuffer(url string, buffer []byte) (*http.Response, error) { req, err := http.NewRequest("POST", c.BaseUrl+url, bytes.NewBuffer(buffer)) - req.Header.Set("Accept", "application/cbor") - req.Header.Add("api-key", c.apiKey) if err != nil { return nil, err } + if req == nil { + return nil, fmt.Errorf("empty request") + } + req.Header.Set("Accept", "application/cbor") + req.Header.Add("api-key", c.apiKey) req.Header.Set("Content-Type", "application/cbor") return c.HTTPClient.Do(req) } diff --git a/client/datum.go b/client/datum.go index 6333ad6..633da66 100644 --- a/client/datum.go +++ b/client/datum.go @@ -14,6 +14,9 @@ func (c *Client) DatumFromHash(hash string) (*models.DatumFromHash, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/ecosystem.go b/client/ecosystem.go index b22f190..c78723f 100644 --- a/client/ecosystem.go +++ b/client/ecosystem.go @@ -14,6 +14,9 @@ func (c *Client) ResolveAdaHandle(handle string) (*models.BasicResponse, error) if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/epochs.go b/client/epochs.go index ec27e12..eea4ff0 100644 --- a/client/epochs.go +++ b/client/epochs.go @@ -14,6 +14,9 @@ func (c *Client) CurrentEpoch() (*models.EpochResp, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -32,6 +35,9 @@ func (c *Client) SpecificEpoch(epochNo int) (*models.EpochResp, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/general.go b/client/general.go index d89a924..b63d9bc 100644 --- a/client/general.go +++ b/client/general.go @@ -14,6 +14,9 @@ func (c *Client) ChainTip() (*models.ChainTip, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -32,6 +35,9 @@ func (c *Client) EraHistory() (*models.EraHistory, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -50,6 +56,9 @@ func (c *Client) ProtocolParameters() (*models.ProtocolParameters, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -68,6 +77,9 @@ func (c *Client) BlockChainStartTime() (models.BasicResponse, error) { if err != nil { return models.BasicResponse{}, err } + if resp == nil { + return models.BasicResponse{}, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return models.BasicResponse{}, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/linear_vesting.go b/client/linear_vesting.go index 2f668df..5b54c31 100644 --- a/client/linear_vesting.go +++ b/client/linear_vesting.go @@ -28,6 +28,9 @@ func (c *Client) LockAssets(lockBody LockBody) (*models.LockTransaction, error) if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -46,6 +49,9 @@ func (c *Client) StateOfVestingAssets(beneficiary string) (*[]models.VestingStat if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -65,6 +71,9 @@ func (c *Client) CollectAssets(beneficiary string) (*models.CollectTransaction, if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/pools.go b/client/pools.go index 9c94f02..c93a7ad 100644 --- a/client/pools.go +++ b/client/pools.go @@ -19,6 +19,9 @@ func (c *Client) ListOfRegisteredPools(params *utils.Parameters) (*models.Regist if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -44,6 +47,9 @@ func (c *Client) StakePoolMintedBlocks( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -69,6 +75,9 @@ func (c *Client) StakePoolDelegators( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -94,6 +103,9 @@ func (c *Client) StakePoolHistory( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -112,6 +124,9 @@ func (c *Client) StakePoolInformation(poolId string) (*models.StakePoolInformati if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -130,6 +145,9 @@ func (c *Client) StakePoolMetadata(poolId string) (*models.StakePoolMetadata, er if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -148,6 +166,9 @@ func (c *Client) StakePoolRelays(poolId string) (*models.StakePoolRelays, error) if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -167,6 +188,9 @@ func (c *Client) StakePoolUpdates(poolId string) (*models.StakePoolUpdates, erro if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/scripts.go b/client/scripts.go index 614fe7f..df3bc46 100644 --- a/client/scripts.go +++ b/client/scripts.go @@ -14,6 +14,9 @@ func (c *Client) ScriptByHash(hash string) (*models.ScriptByHash, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/transactions.go b/client/transactions.go index 72d81a9..9a72c61 100644 --- a/client/transactions.go +++ b/client/transactions.go @@ -15,6 +15,9 @@ func (c *Client) AddressByOutputReference(txHash string, index int) (*models.Bas if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -29,11 +32,14 @@ func (c *Client) AddressByOutputReference(txHash string, index int) (*models.Bas } func (c *Client) SubmitTx(cbor string) (models.BasicResponse, error) { - url := "/submit/tx" + url := "/submitmodels.BasicResponse{}/tx" resp, err := c.post(url, cbor) if err != nil { return models.BasicResponse{}, err } + if resp == nil { + return models.BasicResponse{}, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return models.BasicResponse{}, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -53,6 +59,9 @@ func (c *Client) TransactionCbor(txHash string) (*models.BasicResponse, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -72,6 +81,9 @@ func (c *Client) TransactionDetails(txHash string) (*models.TransactionDetails, if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -99,6 +111,9 @@ func (c *Client) TransactionOutputFromReference( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -125,6 +140,9 @@ func (c *Client) TransactionOutputsFromReferences( if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -138,7 +156,10 @@ func (c *Client) TransactionOutputsFromReferences( } -func (c *Client) EvaluateTx(txCbor string, AdditionalUtxos ...string) ([]models.RedeemerEvaluation, error) { +func (c *Client) EvaluateTx( + txCbor string, + AdditionalUtxos ...string, +) ([]models.RedeemerEvaluation, error) { url := "/transactions/evaluate" body := models.EvaluateTx{ Cbor: txCbor, @@ -148,6 +169,9 @@ func (c *Client) EvaluateTx(txCbor string, AdditionalUtxos ...string) ([]models. if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/client/txmanager.go b/client/txmanager.go index 5e6ede2..eb40d36 100644 --- a/client/txmanager.go +++ b/client/txmanager.go @@ -16,6 +16,9 @@ func (c *Client) TxManagerHistory() (*[]models.TxManagerState, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } @@ -38,6 +41,9 @@ func (c *Client) TxManagerSubmit(txHex string) (string, error) { if err != nil { return "", err } + if resp == nil { + return "", fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted { return "", fmt.Errorf("unexpected error: %d", resp.Body) } @@ -60,6 +66,9 @@ func (c *Client) TxManagerSubmitTurbo(txHex string) (string, error) { if err != nil { return "", err } + if resp == nil { + return "", fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted { return "", fmt.Errorf("unexpected error: %d", resp.Body) } @@ -78,6 +87,9 @@ func (c *Client) TxManagerState(txHash string) (*models.TxManagerState, error) { if err != nil { return nil, err } + if resp == nil { + return nil, fmt.Errorf("empty response") + } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected error: %d", resp.Body) } diff --git a/main.go b/main.go index dda4e3f..0a85a2b 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "github.com/maestro-org/go-sdk/client" ) @@ -10,7 +11,12 @@ func main() { maestroClient := client.NewClient("AMXzQsaXXuUPwfV4IWz8q3tMTmO6x40d", "mainnet") blockInfo, err := maestroClient.BlockInfo(9005859) if err != nil { - fmt.Printf("Failed to retrieve block info: %s\n", err) + fmt.Printf("Failed to retrieve block info: %s", err) + os.Exit(1) + } + if blockInfo == nil { + fmt.Printf("Block info empty!") + os.Exit(1) } fmt.Println(blockInfo.Data.AbsoluteSlot) }