aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-03-20 12:13:47 +0200
committerTulir Asokan <tulir@maunium.net>2018-03-20 12:13:47 +0200
commitcce1403822094828e5a96b9b125b339890b7f100 (patch)
tree6ee85126cddd2ccf0d8385a2aa236e09238a83d0
parent160e8ff83642272c744f17e68560eb9b23dc9842 (diff)
Don't panic if session loading fails
-rw-r--r--config/config.go6
-rw-r--r--config/session.go26
2 files changed, 17 insertions, 15 deletions
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 {