[go: up one dir, main page]

Skip to content

Workhorse error handling in deferred functions

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Problem

golangci linter complains about lack of verification for errors in deferred functions.

Error return value of `content.Close` is not checked (errcheck)

During the refactoring this problem was addressed in several ways:

  1. Log an error in defer function
defer func() {
    if err = content.Close(); err != nil {
        fmt.Printf("Error closing file: %v", err)
    }
}()

source

  1. Ignore the error
defer func() { _ = body.Close() }()

source

Currently, the codebase includes both options.

Proposal

  1. Discuss and define how should we handle this case
  2. Document the decision
  3. Unify codebase to follow the same approach
  4. Try to enforce it via golangci (if possible)
Edited by 🤖 GitLab Bot 🤖