Skip to content

Commit

Permalink
fix: improved error checking around nils
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <[email protected]>
  • Loading branch information
wolf31o2 committed Apr 27, 2024
1 parent 2d3e4c9 commit abf5fbe
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 17 deletions.
18 changes: 18 additions & 0 deletions client/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
24 changes: 24 additions & 0 deletions client/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
18 changes: 18 additions & 0 deletions client/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
15 changes: 15 additions & 0 deletions client/asset_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions client/block_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")

Expand Down
44 changes: 30 additions & 14 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}

Expand All @@ -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)
}
3 changes: 3 additions & 0 deletions client/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions client/ecosystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit abf5fbe

Please sign in to comment.