diff options
author | Tulir Asokan <tulir@maunium.net> | 2018-03-19 01:18:36 +0200 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2018-03-19 01:21:53 +0200 |
commit | 7a4b108b37ccf415f75d74738dddbdba23af1805 (patch) | |
tree | c24add86e6e6f489ef9e05bf9a5463463613fa12 /ui/debug | |
parent | f288a18a0c0ea2609667a02ebe42ca0a8b034004 (diff) |
Avoid showing panics directly if debug mode is not enabled
Diffstat (limited to 'ui/debug')
-rw-r--r-- | ui/debug/debug.go | 49 |
1 files changed, 46 insertions, 3 deletions
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) +} |