Skip to content

Commit 14bf4cd

Browse files
committed
refactor: improve error handling and resource cleanup in file operations
1 parent a58fa05 commit 14bf4cd

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

config/formats/formats.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ func parseConfig(filename string, out any, parser Parser) error {
2323
func loadConfigFile(filename string) ([]byte, error) {
2424
configFile, err := os.ReadFile(filepath.Clean(filename))
2525
if err != nil {
26-
//lint:ignore ST1005 this is a formatted error
27-
return nil, fmt.Errorf("error reading config file: %s\n", err)
26+
return nil, fmt.Errorf("reading config file: %w", err)
2827
}
2928

3029
return configFile, nil

filesystem/filesystem_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package filesystem
22

33
import (
4+
"log"
45
"os"
56
"testing"
67

@@ -10,7 +11,12 @@ import (
1011

1112
func TestFileExists(t *testing.T) {
1213
file := createTempFile(t)
13-
defer os.Remove(file)
14+
defer func(name string) {
15+
err := os.Remove(name)
16+
if err != nil {
17+
log.Printf("could not remove temp file: %s, error: %v", name, err)
18+
}
19+
}(file)
1420

1521
assert.True(t, FileExists(file), "expected file to exist: %s", file)
1622
}
@@ -25,7 +31,12 @@ func createTempFile(t *testing.T) string {
2531
t.Helper()
2632
f, err := os.CreateTemp("./", "temp-*.txt")
2733
require.NoError(t, err, "could not create temp file")
28-
defer f.Close()
34+
defer func(f *os.File) {
35+
err = f.Close()
36+
if err != nil {
37+
log.Printf("could not close temp file: %s, error: %v", f.Name(), err)
38+
}
39+
}(f)
2940

3041
return f.Name()
3142
}

shutdown/shutdown.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (o *observerSingle) AddCommand(fn ObserverFunc) {
3838
// Will be automatically executed on first call to GetObserver.
3939
func (o *observerSingle) hookOnSigTerm() {
4040
channel := make(chan os.Signal, 1)
41-
//lint:ignore SA1017 Kill code 15 should always lead to the final execution of provided functions
41+
//nolint:govet // SA1017: intentional – SIGTERM must always trigger graceful shutdown hooks
4242
signal.Notify(channel, os.Interrupt, syscall.SIGTERM)
4343
go func() {
4444
<-channel

0 commit comments

Comments
 (0)