Improved error handling
This commit is contained in:
parent
8c5a1da093
commit
befe975594
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user