Skip to content

Commit

Permalink
Handle error related to closing file.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxki committed Sep 30, 2023
1 parent 65f576f commit be01915
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/dyocsp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func main() {
os.Exit(0)
}

if err := cfgF.Close(); err != nil {
stdlog.Printf("failed to close file: %v\n", err)
}

responder := newResponder(cfg)

err = run(cfg, responder)
Expand Down
14 changes: 10 additions & 4 deletions pkg/db/file_db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ const (
)

// Scan reads a file and parses each line into an IntermediateEntry.
func (h FileDBClient) Scan(ctx context.Context) ([]IntermidiateEntry, error) {
db, err := os.Open(h.dbFile)
func (h FileDBClient) Scan(ctx context.Context) (entries []IntermidiateEntry, err error) {
file, err := os.Open(h.dbFile)
if err != nil {
return nil, fmt.Errorf("could not read file DB %s: %w", h.dbFile, err)
}
defer func() {
closeErr := file.Close()
if err == nil {
err = closeErr
}
}()

scanner := bufio.NewScanner(db)
entries := make([]IntermidiateEntry, 0)
scanner := bufio.NewScanner(file)
entries = make([]IntermidiateEntry, 0)

for scanner.Scan() {
var entry IntermidiateEntry
Expand Down

0 comments on commit be01915

Please sign in to comment.