From 7a4b108b37ccf415f75d74738dddbdba23af1805 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 19 Mar 2018 01:18:36 +0200 Subject: Avoid showing panics directly if debug mode is not enabled --- ui/debug/debug.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- ui/view-main.go | 4 +++- 2 files changed, 49 insertions(+), 4 deletions(-) (limited to 'ui') diff --git a/ui/debug/debug.go b/ui/debug/debug.go index c855897..a87d64c 100644 --- a/ui/debug/debug.go +++ b/ui/debug/debug.go @@ -18,6 +18,11 @@ package debug import ( "fmt" + "io/ioutil" + "os" + "time" + + "runtime/debug" "maunium.net/go/tview" ) @@ -30,7 +35,7 @@ type Printer interface { type Pane struct { *tview.TextView Height int - num int + num int } var Default Printer @@ -46,8 +51,8 @@ func NewPane() *Pane { return &Pane{ TextView: pane, - Height: 35, - num: 0, + Height: 35, + num: 0, } } @@ -81,3 +86,41 @@ func Print(text ...interface{}) { Default.Print(text...) } } + +const Oops = ` __________ +< Oh noes! > + ‾‾‾\‾‾‾‾‾‾ + \ ^__^ + \ (XX)\_______ + (__)\ )\/\ + U ||----W | + || ||` + +func PrettyPanic() { + fmt.Println(Oops) + fmt.Println("") + fmt.Println("A fatal error has occurred.") + fmt.Println("") + traceFile := fmt.Sprintf("/tmp/gomuks-panic-%s.txt", time.Now().Format("2006-01-02--15-04-05")) + data := debug.Stack() + err := ioutil.WriteFile(traceFile, data, 0644) + if err != nil { + fmt.Println("Saving the stack trace to", traceFile, "failed:") + fmt.Println("--------------------------------------------------------------------------------") + fmt.Println(err) + fmt.Println("--------------------------------------------------------------------------------") + fmt.Println("") + fmt.Println("You can file an issue at https://github.com/tulir/gomuks/issues.") + fmt.Println("Please provide the file save error (above) and the stack trace of the original error (below) when filing an issue.") + fmt.Println("") + fmt.Println("--------------------------------------------------------------------------------") + debug.PrintStack() + fmt.Println("--------------------------------------------------------------------------------") + } else { + fmt.Println("The stack trace has been saved to", traceFile) + fmt.Println("") + fmt.Println("You can file an issue at https://github.com/tulir/gomuks/issues.") + fmt.Println("Please provide the contents of that file when filing an issue.") + } + os.Exit(1) +} diff --git a/ui/view-main.go b/ui/view-main.go index 8ec482e..c2cd30f 100644 --- a/ui/view-main.go +++ b/ui/view-main.go @@ -148,7 +148,7 @@ func (view *MainView) InputDone(key tcell.Key) { } func (view *MainView) HandleCommand(room, command string, args []string) { - view.gmx.Recover() + defer view.gmx.Recover() debug.Print("Handling command", command, args) switch command { case "/quit": @@ -156,6 +156,8 @@ func (view *MainView) HandleCommand(room, command string, args []string) { case "/clearcache": view.config.Session.Clear() view.gmx.Stop() + case "/panic": + panic("This is a test panic.") case "/part": fallthrough case "/leave": -- cgit v1.2.3