From befe97559494b7336374d97aec9afd34723c520b Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 6 Jul 2023 15:40:25 +0200 Subject: [PATCH] Improved error handling --- Collection.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Collection.go b/Collection.go index 94c87e9..c7bdb89 100644 --- a/Collection.go +++ b/Collection.go @@ -76,7 +76,11 @@ func (c *collection[T]) Get(key string) (*T, bool) { // Set sets the value for the given key. func (c *collection[T]) Set(key string, value *T) { c.data.Store(key, value) - c.writeFileToDisk(key, value) + err := c.writeFileToDisk(key, value) + + if err != nil { + panic(err) + } } // Delete deletes a key from the collection. @@ -151,24 +155,20 @@ func (c *collection[T]) loadFileFromDisk(name string) error { } // writeFileToDisk writes the value for the key to disk as a JSON file. -func (c *collection[T]) writeFileToDisk(key string, value *T) { +func (c *collection[T]) writeFileToDisk(key string, value *T) error { fileName := c.keyFile(key) file, err := os.Create(fileName) if err != nil { - panic(err) + return err } encoder := json.NewEncoder(file) err = encoder.Encode(value) if err != nil { - panic(err) + return err } - err = file.Close() - - if err != nil { - panic(err) - } + return file.Close() }