Improved error handling

This commit is contained in:
Eduard Urbach 2023-07-06 15:40:25 +02:00
parent 8c5a1da093
commit befe975594
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

View File

@ -76,7 +76,11 @@ func (c *collection[T]) Get(key string) (*T, bool) {
// Set sets the value for the given key. // Set sets the value for the given key.
func (c *collection[T]) Set(key string, value *T) { func (c *collection[T]) Set(key string, value *T) {
c.data.Store(key, value) 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. // 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. // 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) fileName := c.keyFile(key)
file, err := os.Create(fileName) file, err := os.Create(fileName)
if err != nil { if err != nil {
panic(err) return err
} }
encoder := json.NewEncoder(file) encoder := json.NewEncoder(file)
err = encoder.Encode(value) err = encoder.Encode(value)
if err != nil { if err != nil {
panic(err) return err
} }
err = file.Close() return file.Close()
if err != nil {
panic(err)
}
} }