Skip to content

ensure close response body if response is not nil #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions node-registrar/client/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 17 additions & 2 deletions node-registrar/client/farm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down
20 changes: 16 additions & 4 deletions node-registrar/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions node-registrar/client/zos_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading