Skip to content

Commit e439f07

Browse files
authored
file: load should propage file not found error (#84)
So it could be handled by caller. For example here VictoriaMetrics/VictoriaMetrics#8952
1 parent f180005 commit e439f07

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func load(filePath string, maxBytes int) (*Cache, error) {
141141
// Read bucket files from filePath dir.
142142
d, err := os.Open(filePath)
143143
if err != nil {
144-
return nil, fmt.Errorf("cannot open %q: %s", filePath, err)
144+
return nil, fmt.Errorf("cannot open %q: %w", filePath, err)
145145
}
146146
defer func() {
147147
_ = d.Close()
@@ -206,7 +206,7 @@ func loadMetadata(dir string) (uint64, error) {
206206
metadataPath := dir + "/metadata.bin"
207207
metadataFile, err := os.Open(metadataPath)
208208
if err != nil {
209-
return 0, fmt.Errorf("cannot open %q: %s", metadataPath, err)
209+
return 0, fmt.Errorf("cannot open %q: %w", metadataPath, err)
210210
}
211211
defer func() {
212212
_ = metadataFile.Close()

file_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fastcache
22

33
import (
4+
"errors"
45
"fmt"
56
"io/ioutil"
67
"os"
@@ -45,6 +46,19 @@ func TestSaveLoadSmall(t *testing.T) {
4546
}
4647
}
4748

49+
func TestLoadFileNotExist(t *testing.T) {
50+
c, err := LoadFromFile(`non-existing-file`)
51+
if err == nil {
52+
t.Fatalf("LoadFromFile must return error; got nil")
53+
}
54+
if !errors.Is(err, os.ErrNotExist) {
55+
t.Fatalf("LoadFromFile must return os.ErrNotExist; got: %s", err)
56+
}
57+
if c != nil {
58+
t.Fatalf("LoadFromFile must return nil cache")
59+
}
60+
}
61+
4862
func TestSaveLoadFile(t *testing.T) {
4963
for _, concurrency := range []int{0, 1, 2, 4, 10} {
5064
t.Run(fmt.Sprintf("concurrency_%d", concurrency), func(t *testing.T) {

0 commit comments

Comments
 (0)