aboutsummaryrefslogtreecommitdiff
path: root/gomuks.go
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-03-19 01:18:36 +0200
committerTulir Asokan <tulir@maunium.net>2018-03-19 01:21:53 +0200
commit7a4b108b37ccf415f75d74738dddbdba23af1805 (patch)
treec24add86e6e6f489ef9e05bf9a5463463613fa12 /gomuks.go
parentf288a18a0c0ea2609667a02ebe42ca0a8b034004 (diff)
Avoid showing panics directly if debug mode is not enabled
Diffstat (limited to 'gomuks.go')
-rw-r--r--gomuks.go52
1 files changed, 33 insertions, 19 deletions
diff --git a/gomuks.go b/gomuks.go
index fecb67a..418fc9e 100644
--- a/gomuks.go
+++ b/gomuks.go
@@ -17,8 +17,10 @@
package main
import (
+ "fmt"
"os"
"path/filepath"
+ "time"
"maunium.net/go/gomatrix"
"maunium.net/go/gomuks/config"
@@ -29,17 +31,18 @@ import (
"maunium.net/go/tview"
)
-type gomuks struct {
- app *tview.Application
- ui *ui.GomuksUI
- matrix *matrix.Container
- debug *debug.Pane
- config *config.Config
+type Gomuks struct {
+ app *tview.Application
+ ui *ui.GomuksUI
+ matrix *matrix.Container
+ debug *debug.Pane
+ debugMode bool
+ config *config.Config
}
-func NewGomuks(enableDebug bool) *gomuks {
+func NewGomuks(enableDebug bool) *Gomuks {
configDir := filepath.Join(os.Getenv("HOME"), ".config/gomuks")
- gmx := &gomuks{
+ gmx := &Gomuks{
app: tview.NewApplication(),
}
@@ -63,13 +66,14 @@ func NewGomuks(enableDebug bool) *gomuks {
main := gmx.ui.InitViews()
if enableDebug {
main = gmx.debug.Wrap(main)
+ gmx.debugMode = true
}
gmx.app.SetRoot(main, true)
return gmx
}
-func (gmx *gomuks) Stop() {
+func (gmx *Gomuks) Stop() {
gmx.debug.Print("Disconnecting from Matrix...")
gmx.matrix.Stop()
gmx.debug.Print("Cleaning up UI...")
@@ -78,44 +82,54 @@ func (gmx *gomuks) Stop() {
gmx.debug.Print("Saving session...")
gmx.config.Session.Save()
}
+ os.Exit(0)
}
-func (gmx *gomuks) Recover() {
+func (gmx *Gomuks) Recover() {
if p := recover(); p != nil {
if gmx.App().GetScreen() != nil {
gmx.App().GetScreen().Fini()
}
- panic(p)
+ if gmx.debugMode {
+ panic(p)
+ } else {
+ debug.PrettyPanic()
+ }
}
}
-func (gmx *gomuks) Start() {
+func (gmx *Gomuks) Start() {
if err := gmx.app.Run(); err != nil {
panic(err)
}
}
-func (gmx *gomuks) Matrix() *gomatrix.Client {
+func (gmx *Gomuks) Matrix() *gomatrix.Client {
return gmx.matrix.Client()
}
-func (gmx *gomuks) MatrixContainer() ifc.MatrixContainer {
+func (gmx *Gomuks) MatrixContainer() ifc.MatrixContainer {
return gmx.matrix
}
-func (gmx *gomuks) App() *tview.Application {
+func (gmx *Gomuks) App() *tview.Application {
return gmx.app
}
-func (gmx *gomuks) Config() *config.Config {
+func (gmx *Gomuks) Config() *config.Config {
return gmx.config
}
-func (gmx *gomuks) UI() ifc.GomuksUI {
+func (gmx *Gomuks) UI() ifc.GomuksUI {
return gmx.ui
}
func main() {
- debug := os.Getenv("DEBUG")
- NewGomuks(len(debug) > 0).Start()
+ enableDebug := len(os.Getenv("DEBUG")) > 0
+ NewGomuks(enableDebug).Start()
+
+ // We use os.Exit() everywhere, so exiting by returning from Start() shouldn't happen.
+ time.Sleep(5 * time.Second)
+ fmt.Println("Unexpected exit by return from Gomuks#Start().")
+ os.Exit(2)
}