Skip to content

Commit

Permalink
feat(backtrace): Show error's stack if QRI_BACKTRACE is defined
Browse files Browse the repository at this point in the history
Taking an idea from rust's RUST_BACKTRACE, show a stack dump on error only if
the environment variable QRI_BACKTRACE is defined. This prevents the need to
comment and uncomment code blocks (which may get accidentally commited to git)
in order to get valuable debugging information.
  • Loading branch information
dustmop committed May 30, 2018
1 parent fb871d6 commit 88160e2
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import (
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
// Catch errors & pretty-print.
// comment this out to get stack traces back.
defer func() {
if r := recover(); r != nil {
if err, ok := r.(error); ok {
fmt.Println(err.Error())
} else {
fmt.Println(r)
if os.Getenv("QRI_BACKTRACE") == "" {
// Catch errors & pretty-print.
defer func() {
if r := recover(); r != nil {
if err, ok := r.(error); ok {
fmt.Println(err.Error())
} else {
fmt.Println(r)
}
}
}
}()
}()
}

if err := RootCmd.Execute(); err != nil {
printErr(err)
Expand Down

0 comments on commit 88160e2

Please sign in to comment.