Skip to content

Commit

Permalink
fix: Handle program i/o errors
Browse files Browse the repository at this point in the history
Stop execution of program with non-zero exit code on errors:
- while writing to stdin
- while closing input files.

The error was detected by <https://github.com/kisielk/errcheck>.
  • Loading branch information
macie committed Sep 8, 2024
1 parent 68d5df7 commit c429c3b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cmd/sortof/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func main() {
}

if config.ExitMessage != "" {
fmt.Fprintln(os.Stdin, config.ExitMessage)
if _, err := fmt.Fprintln(os.Stdout, config.ExitMessage); err != nil {
panic(err)
}
os.Exit(0)
}

Expand All @@ -44,7 +46,12 @@ func main() {
log.Println(err)
os.Exit(1)
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
log.Println(err)
os.Exit(1)
}
}()

files = append(files, f)
}
Expand All @@ -68,7 +75,9 @@ func main() {
}

for _, v := range sorted {
fmt.Fprintln(os.Stdout, v)
if _, err := fmt.Fprintln(os.Stdout, v); err != nil {
panic(err)
}
}
}

Expand Down

0 comments on commit c429c3b

Please sign in to comment.