Return configuration save failures
This commit is contained in:
1 parent
501943c8f7
commit
c36305cb38
2 files changed
+27
-9
No files matched your search
+11
-9
@@ -46,20 +46,22 @@ type eUser struct {
|
|||||||
LocallyMuted bool // Changed from Muted to LocallyMuted to match User struct
|
LocallyMuted bool // Changed from Muted to LocallyMuted to match User struct
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) SaveConfig() {
|
// SaveConfig atomically replaces the persisted configuration. Errors are
|
||||||
var data []byte
|
// returned so an unavailable directory cannot crash the client.
|
||||||
|
func (c *Config) SaveConfig() error {
|
||||||
data, err := toml.Marshal(c.config)
|
data, err := toml.Marshal(c.config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(c.fn+".tmp", data, 0600)
|
tmp := c.fn + ".tmp"
|
||||||
if err != nil {
|
if err := ioutil.WriteFile(tmp, data, 0600); err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
err = os.Rename(c.fn+".tmp", c.fn)
|
if err := os.Rename(tmp, c.fn); err != nil {
|
||||||
if err != nil {
|
_ = os.Remove(tmp)
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func key(k uiterm.Key) *uiterm.Key {
|
func key(k uiterm.Key) *uiterm.Key {
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Regression: a configuration write failure panicked the client instead of
|
||||||
|
// returning an error to the caller.
|
||||||
|
func TestSaveConfigReturnsWriteError(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "missing", "barnard.toml")
|
||||||
|
cfg := NewConfig(&path)
|
||||||
|
if err := cfg.SaveConfig(); err == nil {
|
||||||
|
t.Fatal("expected configuration write error")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user