Skip to content

Commit

Permalink
Don't check for HTTP response body before checking for an error
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Feb 11, 2020
1 parent 2a50608 commit ef3679f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 78 deletions.
13 changes: 6 additions & 7 deletions internal/httpauth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,16 @@ func (c *Client) Nonce(ctx context.Context, key cipher.PubKey) (Nonce, error) {
req = req.WithContext(ctx)

resp, err := c.client.Do(req)
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()
}
if err != nil {
return 0, err
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()

if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("error getting current nonce: status: %d <- %v", resp.StatusCode, extractError(resp.Body))
}
Expand Down
13 changes: 6 additions & 7 deletions internal/utclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,16 @@ func (c *httpClient) Get(ctx context.Context, path string) (*http.Response, erro
// UpdateVisorUptime updates visor uptime.
func (c *httpClient) UpdateVisorUptime(ctx context.Context) error {
resp, err := c.Get(ctx, "/update")
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close response body")
}
}()
}
if err != nil {
return err
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close response body")
}
}()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status: %d, error: %v", resp.StatusCode, extractError(resp.Body))
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/hypervisor/hypervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ func testCase(t *testing.T, addr string, client *http.Client, tc TestCase, testT
}

resp, err := client.Do(req)
if resp != nil {
defer func() {
assert.NoError(t, resp.Body.Close())
}()
}

require.NoError(t, err, testTag)

defer func() {
assert.NoError(t, resp.Body.Close())
}()

assert.Equal(t, tc.RespStatus, resp.StatusCode, testTag)

if tc.RespBody != nil {
Expand Down
66 changes: 31 additions & 35 deletions pkg/transport-discovery/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,16 @@ func (c *apiClient) RegisterTransports(ctx context.Context, entries ...*transpor
}

resp, err := c.Post(ctx, "/transports/", entries)
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()
}
if err != nil {
return err
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()

if resp.StatusCode == http.StatusCreated {
return nil
}
Expand All @@ -111,17 +110,16 @@ func (c *apiClient) RegisterTransports(ctx context.Context, entries ...*transpor
// GetTransportByID returns Transport for corresponding ID.
func (c *apiClient) GetTransportByID(ctx context.Context, id uuid.UUID) (*transport.EntryWithStatus, error) {
resp, err := c.Get(ctx, fmt.Sprintf("/transports/id:%s", id.String()))
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()
}
if err != nil {
return nil, err
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d, error: %v", resp.StatusCode, extractError(resp.Body))
}
Expand All @@ -137,17 +135,16 @@ func (c *apiClient) GetTransportByID(ctx context.Context, id uuid.UUID) (*transp
// GetTransportsByEdge returns all Transports registered for the edge.
func (c *apiClient) GetTransportsByEdge(ctx context.Context, pk cipher.PubKey) ([]*transport.EntryWithStatus, error) {
resp, err := c.Get(ctx, fmt.Sprintf("/transports/edge:%s", pk))
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()
}
if err != nil {
return nil, err
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d, error: %v", resp.StatusCode, extractError(resp.Body))
}
Expand All @@ -163,16 +160,16 @@ func (c *apiClient) GetTransportsByEdge(ctx context.Context, pk cipher.PubKey) (
// DeleteTransport deletes given transport by it's ID. A visor can only delete transports if he is one of it's edges.
func (c *apiClient) DeleteTransport(ctx context.Context, id uuid.UUID) error {
resp, err := c.Delete(ctx, fmt.Sprintf("/transports/id:%s", id.String()))
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()
}
if err != nil {
return err
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status: %d, error: %v", resp.StatusCode, extractError(resp.Body))
}
Expand All @@ -187,17 +184,16 @@ func (c *apiClient) UpdateStatuses(ctx context.Context, statuses ...*transport.S
}

resp, err := c.Post(ctx, "/statuses", statuses)
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()
}
if err != nil {
return nil, err
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d, error: %v", resp.StatusCode, extractError(resp.Body))
}
Expand Down
42 changes: 18 additions & 24 deletions pkg/util/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,16 @@ func getChecksum(checksums, filename string) (string, error) {

func downloadChecksums(url string) (checksums string, err error) {
resp, err := http.Get(url) // nolint:gosec
if resp != nil {
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
}

if err != nil {
return "", err
}

defer func() {
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("received bad status code: %d", resp.StatusCode)
}
Expand All @@ -266,18 +264,16 @@ func downloadChecksums(url string) (checksums string, err error) {

func downloadFile(url, filename string) (path string, err error) {
resp, err := http.Get(url) // nolint:gosec
if resp != nil {
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
}

if err != nil {
return "", err
}

defer func() {
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()

tmpDir := os.TempDir()
path = filepath.Join(tmpDir, filename)

Expand Down Expand Up @@ -341,18 +337,16 @@ func currentVersion() (*Version, error) {

func lastVersionHTML() (data []byte, err error) {
resp, err := http.Get(releaseURL)
if resp != nil {
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
}

if err != nil {
return nil, err
}

defer func() {
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()

return ioutil.ReadAll(resp.Body)
}

Expand Down

0 comments on commit ef3679f

Please sign in to comment.