From 2dc306b017d2dff9756d6063945d7d8b9035e04b Mon Sep 17 00:00:00 2001 From: David Florness Date: Sun, 30 May 2021 12:20:29 -0400 Subject: [PATCH] Use logger and close TUI (if present) when handling panics --- cmd/tallyard/main.go | 20 ++++++++-------- ui/tui.go | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/cmd/tallyard/main.go b/cmd/tallyard/main.go index 5e5c7cd..682cce0 100644 --- a/cmd/tallyard/main.go +++ b/cmd/tallyard/main.go @@ -41,7 +41,7 @@ func main() { client, err := mautrix.NewClient(authInfo.Homeserver, authInfo.UserID, authInfo.AccessToken) if err != nil { - panic(err) + log.Panic(err) } setDeviceName(client, authInfo.DeviceID) @@ -49,7 +49,7 @@ func main() { // setup the elections store electionsMap, err := getElections(authInfo.UserID) if err != nil { - panic(err) + log.Panic(err) } client.Store = matrix.NewTallyardStore(electionsMap) defer electionsMap.Save() @@ -62,19 +62,19 @@ func main() { go func() { res, err := client.CreateFilter(electionFilter) if err != nil { - panic(err) + log.Panic(err) } client.Store.SaveFilterID(client.UserID, res.FilterID) err = client.Sync() if err != nil { - panic(err) + log.Panic(err) } }() // select a room room, err := ui.RoomListTUI(client.Store.(*matrix.TallyardStore), client.UserID, syncer) if err != nil { - panic(err) + log.Panic(err) } if room == nil { // no room selected; user likely hit C-c @@ -84,7 +84,7 @@ func main() { // select an election el, err := ui.RoomTUI(client, room, electionsMap, syncer) if err != nil { - panic(err) + log.Panic(err) } if el == nil || el.LocalVoter == nil { // no election selected; user likely hit C-c @@ -94,7 +94,7 @@ func main() { // wait for election to start if needed err = ui.ElectionWaitTUI(client, el, electionsMap.EventStore) if err != nil { - panic(err) + log.Panic(err) } if el.StartID == nil || el.FinalJoinIDs == nil { // election never started; user likely hit C-c @@ -117,7 +117,7 @@ func main() { if el.LocalVoter != nil && el.LocalVoter.Poly == nil { ballot, err := ui.VoteTUI(el.Candidates) if err != nil { - panic(err) + log.Panic(err) } if ballot == nil { // user likely hit C-c @@ -135,7 +135,7 @@ func main() { if el.LocalVoter != nil && el.LocalVoter.EvalsID == nil { err = el.SendEvals(client, electionsMap.EventStore) if err != nil { - panic(err) + log.Panic(err) } } @@ -149,7 +149,7 @@ func main() { } err = el.SendSum(client, electionsMap.EventStore) if err != nil { - panic(err) + log.Panic(err) } } diff --git a/ui/tui.go b/ui/tui.go index c012b58..57b29ca 100644 --- a/ui/tui.go +++ b/ui/tui.go @@ -2,6 +2,7 @@ package ui import ( "fmt" + "runtime/debug" "sort" "strconv" "strings" @@ -23,6 +24,15 @@ import ( // store is already properly saving rooms func RoomListTUI(store *matrix.TallyardStore, localUserID id.UserID, syncer mautrix.ExtensibleSyncer) (*election.Room, error) { app := newTallyardApplication() + defer func() { + if r := recover(); r != nil { + if app.alive { + app.Stop() + } + log.Panicf("RoomListTUI panicked! panic=%s\n%s", r, debug.Stack()) + } + }() + list := tview.NewList().AddItem("syncing...", "", 0, nil) list.SetTitle("Select a room").SetBorder(true) var rooms []*roomWithTitle @@ -170,6 +180,15 @@ func (roomI *roomWithTitle) cmp(roomJ *roomWithTitle) bool { func RoomTUI(client *mautrix.Client, room *election.Room, electionsMap *election.ElectionsMap, syncer mautrix.ExtensibleSyncer) (el *election.Election, err error) { app := newTallyardApplication() + defer func() { + if r := recover(); r != nil { + if app.alive { + app.Stop() + } + log.Panicf("RoomTUI panicked! panic=%s\n%s", r, debug.Stack()) + } + }() + list := tview.NewList(). AddItem("Create Election", "start a new election in this room", 0, nil) list.SetTitle("Select election").SetBorder(true) @@ -261,6 +280,15 @@ func RoomTUI(client *mautrix.Client, room *election.Room, electionsMap *election func electionConfirmation(el *election.Election) (shouldJoin bool) { app := newTallyardApplication() + defer func() { + if r := recover(); r != nil { + if app.alive { + app.Stop() + } + log.Panicf("electionConfirmation panicked! panic=%s\n%s", r, debug.Stack()) + } + }() + var text string el.RLock() @@ -301,6 +329,15 @@ func CreateElectionTUI() (title string, candidates []election.Candidate) { var form *tview.Form n := 2 app := newTallyardApplication() + defer func() { + if r := recover(); r != nil { + if app.alive { + app.Stop() + } + log.Panicf("CreateElectionTUI panicked! panic=%s\n%s", r, debug.Stack()) + } + }() + plus := func() { if n < 5 { form.AddInputField(fmt.Sprintf("%d.", n+1), "", 50, nil, nil) @@ -351,6 +388,15 @@ func ElectionWaitTUI(client *mautrix.Client, el *election.Election, eventStore * frame := tview.NewFrame(votersTextView) frame.SetTitle(el.Title).SetBorder(true) app := newTallyardApplication() + defer func() { + if r := recover(); r != nil { + if app.alive { + app.Stop() + } + log.Panicf("ElectionWaitTUI panicked! panic=%s\n%s", r, debug.Stack()) + } + }() + el.RLock() if el.LocalVoter != nil && el.CreateEvt.Sender == el.LocalVoter.JoinEvt.Sender { frame.AddText("Press enter to start the election", false, tview.AlignCenter, tcell.ColorWhite) @@ -443,6 +489,15 @@ func ElectionWaitTUI(client *mautrix.Client, el *election.Election, eventStore * // displays a voting UI to the user and returns the encoded ballot func VoteTUI(candidates []election.Candidate) (ballot *[][]byte, err error) { app := newTallyardApplication() + defer func() { + if r := recover(); r != nil { + if app.alive { + app.Stop() + } + log.Panicf("VoteTUI panicked! panic=%s\n%s", r, debug.Stack()) + } + }() + form := tview.NewForm() form.SetTitle("Vote").SetBorder(true) -- 2.38.4