diff --git a/node-registrar/client/account.go b/node-registrar/client/account.go index 808fc52..f5e3c5b 100644 --- a/node-registrar/client/account.go +++ b/node-registrar/client/account.go @@ -111,11 +111,15 @@ func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (acco return account, mnemonic, errors.Wrap(err, "failed to send request to the registrar") } + if resp == nil { + return account, mnemonic, errors.New("failed to create account, no response received") + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusCreated { err = parseResponseError(resp.Body) return account, mnemonic, errors.Wrapf(err, "failed to create account with status %s", resp.Status) } - defer resp.Body.Close() err = json.NewDecoder(resp.Body).Decode(&account) @@ -146,6 +150,7 @@ func (c *RegistrarClient) getAccount(id uint64) (account Account, err error) { if resp == nil { return account, errors.New("failed to get account, no response received") } + defer resp.Body.Close() if resp.StatusCode == http.StatusNotFound { return account, ErrorAccountNotFound @@ -155,7 +160,6 @@ func (c *RegistrarClient) getAccount(id uint64) (account Account, err error) { err = parseResponseError(resp.Body) return account, errors.Wrapf(err, "failed to get account by twin id with status code %s", resp.Status) } - defer resp.Body.Close() err = json.NewDecoder(resp.Body).Decode(&account) return diff --git a/node-registrar/client/farm.go b/node-registrar/client/farm.go index e416fd4..f4112e0 100644 --- a/node-registrar/client/farm.go +++ b/node-registrar/client/farm.go @@ -156,6 +156,9 @@ func (c *RegistrarClient) createFarm(farmName, stellarAddr string, dedicated boo return farmID, errors.Wrap(err, "failed to send request to create farm") } + if resp == nil { + return farmID, errors.New("failed to create farm, no response received") + } defer resp.Body.Close() if resp.StatusCode != http.StatusCreated { @@ -214,6 +217,9 @@ func (c *RegistrarClient) updateFarm(farmID uint64, opts []UpdateFarmOpts) (err return errors.Wrap(err, "failed to send request to update farm") } + if resp == nil { + return errors.New("failed to update farm, no response received") + } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { @@ -234,6 +240,11 @@ func (c *RegistrarClient) getFarm(id uint64) (farm Farm, err error) { return farm, err } + if resp == nil { + return farm, errors.New("failed to get farm, no response received") + } + defer resp.Body.Close() + if resp.StatusCode == http.StatusNotFound { return farm, ErrorFarmNotFound } @@ -242,7 +253,6 @@ func (c *RegistrarClient) getFarm(id uint64) (farm Farm, err error) { err = parseResponseError(resp.Body) return farm, errors.Wrapf(err, "failed to get farm with status code %s", resp.Status) } - defer resp.Body.Close() if err = json.NewDecoder(resp.Body).Decode(&farm); err != nil { return farm, err @@ -276,11 +286,16 @@ func (c *RegistrarClient) listFarms(opts ...ListFarmOpts) (farms []Farm, err err return farms, errors.Wrap(err, "failed to send request to list farm") } + if resp == nil { + return farms, errors.New("failed to list farms, no response received") + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { err = parseResponseError(resp.Body) return farms, errors.Wrapf(err, "failed to get list farms with status code %s", resp.Status) } - defer resp.Body.Close() + if err = json.NewDecoder(resp.Body).Decode(&farms); err != nil { return farms, errors.Wrap(err, "failed to decode response body") } diff --git a/node-registrar/client/node.go b/node-registrar/client/node.go index b2ba5a8..5a4c328 100644 --- a/node-registrar/client/node.go +++ b/node-registrar/client/node.go @@ -286,11 +286,15 @@ func (c *RegistrarClient) updateNode(opts []UpdateNodeOpts) (err error) { return errors.Wrap(err, "failed to send request to update node") } + if resp == nil { + return errors.New("no response received") + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { err = parseResponseError(resp.Body) return errors.Wrapf(err, "failed to update node with twin id %d with status code %s", c.twinID, resp.Status) } - defer resp.Body.Close() return } @@ -330,11 +334,15 @@ func (c *RegistrarClient) reportUptime(report UptimeReport) (err error) { return errors.Wrap(err, "failed to send request to update uptime of the node") } + if resp == nil { + return errors.New("no response received") + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusCreated { err = parseResponseError(resp.Body) return errors.Wrapf(err, "failed to update node uptime for node with id %d with status code %s", c.nodeID, resp.Status) } - defer resp.Body.Close() return } @@ -350,6 +358,11 @@ func (c *RegistrarClient) getNode(id uint64) (node Node, err error) { return } + if resp == nil { + return node, errors.New("no response received") + } + defer resp.Body.Close() + if resp.StatusCode == http.StatusNotFound { return node, ErrorNodeNotFound } @@ -358,7 +371,6 @@ func (c *RegistrarClient) getNode(id uint64) (node Node, err error) { err = parseResponseError(resp.Body) return node, errors.Wrapf(err, "failed to get node with status code %s", resp.Status) } - defer resp.Body.Close() err = json.NewDecoder(resp.Body).Decode(&node) if err != nil { @@ -409,6 +421,7 @@ func (c *RegistrarClient) listNodes(opts []ListNodeOpts) (nodes []Node, err erro if resp == nil { return nodes, errors.New("no response received") } + defer resp.Body.Close() if resp.StatusCode == http.StatusNotFound { return nodes, ErrorNodeNotFound @@ -418,7 +431,6 @@ func (c *RegistrarClient) listNodes(opts []ListNodeOpts) (nodes []Node, err erro err = parseResponseError(resp.Body) return nodes, errors.Wrapf(err, "failed to list nodes with with status code %s", resp.Status) } - defer resp.Body.Close() err = json.NewDecoder(resp.Body).Decode(&nodes) if err != nil { diff --git a/node-registrar/client/zos_version.go b/node-registrar/client/zos_version.go index 9ce848d..74f4bce 100644 --- a/node-registrar/client/zos_version.go +++ b/node-registrar/client/zos_version.go @@ -33,13 +33,16 @@ func (c *RegistrarClient) getZosVersion() (version ZosVersion, err error) { return version, err } + if resp == nil { + return version, errors.New("no response received") + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { err = parseResponseError(resp.Body) return version, errors.Wrapf(err, "failed to get zos version with status code %s", resp.Status) } - defer resp.Body.Close() - var versionString string err = json.NewDecoder(resp.Body).Decode(&versionString) if err != nil { @@ -110,6 +113,9 @@ func (c *RegistrarClient) setZosVersion(v string, safeToUpgrade bool) (err error return errors.Wrap(err, "failed to send request to get zos version from the registrar") } + if resp == nil { + return errors.New("no response received") + } defer resp.Body.Close() if resp.StatusCode != http.StatusOK {