From 7620f85da904fe37329a98ebb334c5907f835cdd Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Sun, 31 Mar 2024 15:12:44 -0600 Subject: [PATCH] store: catch when file does not exist --- store.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/store.go b/store.go index d151596..ead5523 100644 --- a/store.go +++ b/store.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "log" "os" @@ -26,11 +27,17 @@ type Store string func (s Store) Set(key string, value string) { err := os.WriteFile(path.Join(string(s), key), []byte(value), 0600) if err != nil { - log.Println(err) + log.Println(fmt.Errorf("failed to set %q: %s", key, err)) } } func (s Store) Get(key string) (string, error) { + keyPath := path.Join(string(s), key) + _, err := os.Stat(keyPath) + if errors.Is(err, os.ErrNotExist) { + return "", os.ErrNotExist + } + data, err := os.ReadFile(path.Join(string(s), key)) if err != nil { return "", nil