From cce1403822094828e5a96b9b125b339890b7f100 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 20 Mar 2018 12:13:47 +0200 Subject: Don't panic if session loading fails --- config/config.go | 6 +++--- config/session.go | 26 ++++++++++++++------------ 2 files changed, 17 insertions(+), 15 deletions(-) (limited to 'config') diff --git a/config/config.go b/config/config.go index d4ede80..858f1d2 100644 --- a/config/config.go +++ b/config/config.go @@ -30,13 +30,13 @@ type Config struct { MXID string `yaml:"mxid"` HS string `yaml:"homeserver"` - dir string `yaml:"-"` - Session *Session `yaml:"-"` + dir string `yaml:"-"` + Session *Session `yaml:"-"` } func NewConfig(dir string) *Config { return &Config{ - dir: dir, + dir: dir, } } diff --git a/config/session.go b/config/session.go index a90fc20..6de9436 100644 --- a/config/session.go +++ b/config/session.go @@ -35,9 +35,9 @@ type Session struct { Rooms map[string]*rooms.Room } -func (config *Config) LoadSession(mxid string) { +func (config *Config) LoadSession(mxid string) error { config.Session = config.NewSession(mxid) - config.Session.Load() + return config.Session.Load() } func (config *Config) NewSession(mxid string) *Session { @@ -55,32 +55,34 @@ func (s *Session) Clear() { s.Save() } -func (s *Session) Load() { +func (s *Session) Load() error { data, err := ioutil.ReadFile(s.path) if err != nil { - debug.Print("Failed to read session from", s.path) - panic(err) + debug.Print("Failed to read session from", s.path, err) + return err } err = json.Unmarshal(data, s) if err != nil { - debug.Print("Failed to parse session at", s.path) - panic(err) + debug.Print("Failed to parse session at", s.path, err) + return err } + return nil } -func (s *Session) Save() { +func (s *Session) Save() error { data, err := json.Marshal(s) if err != nil { - debug.Print("Failed to marshal session of", s.MXID) - panic(err) + debug.Print("Failed to marshal session of", s.MXID, err) + return err } err = ioutil.WriteFile(s.path, data, 0600) if err != nil { - debug.Print("Failed to write session to", s.path) - panic(err) + debug.Print("Failed to write session to", s.path, err) + return err } + return nil } func (s *Session) LoadFilterID(_ string) string { -- cgit v1.2.3