Skip to content
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

address further lint issues #5186

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
refactor defer to handle errors
  • Loading branch information
pdabelf5 committed Feb 29, 2024
commit 5745475dfe2c9cc166cfb279cc3585b2f4e57560
7 changes: 5 additions & 2 deletions internal/nginx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,16 @@ func verifyConfigVersion(httpClient *http.Client, configVersion int, timeout tim
if err != nil {
return fmt.Errorf("error doing request: %w", err)
}
defer resp.Body.Close()
err = nil
defer func() {
err = resp.Body.Close()
}()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API returned non-success status: %v", resp.StatusCode)
}

return nil
return err
}

// SetOpenTracing sets the value of OpenTracing for the Manager
Expand Down
8 changes: 6 additions & 2 deletions internal/nginx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ func createFileAndWrite(name string, b []byte) error {
return fmt.Errorf("failed to open %v: %w", name, err)
}

defer w.Close()
defer func() {
if tempErr := w.Close(); tempErr != nil {
err = tempErr
}
}()

_, err = w.Write(b)
if err != nil {
return fmt.Errorf("failed to write to %v: %w", name, err)
}

return nil
return err
}

func createFileAndWriteAtomically(filename string, tempPath string, mode os.FileMode, content []byte) {
Expand Down
9 changes: 7 additions & 2 deletions internal/nginx/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ func (c *verifyClient) GetConfigVersion() (int, error) {
if err != nil {
return 0, fmt.Errorf("error getting client: %w", err)
}
defer resp.Body.Close()
err = nil
defer func() {
if tempErr := resp.Body.Close(); tempErr != nil {
err = tempErr
}
}()

if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("non-200 response: %v", resp.StatusCode)
Expand All @@ -63,7 +68,7 @@ func (c *verifyClient) GetConfigVersion() (int, error) {
if err != nil {
return 0, fmt.Errorf("error converting string to int: %w", err)
}
return v, nil
return v, err
}

// WaitForCorrectVersion calls the config version endpoint until it gets the expectedVersion,
Expand Down