github.com/mgoltzsche/khelm@v1.0.1/cmd/khelm/errlog.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/pkg/errors" 9 ) 10 11 func logStackTrace(err error, debug bool) { 12 if err != nil && debug { 13 if e := traceableRootCause(err); e != nil { 14 if s := fmt.Sprintf("%+v", e); s != err.Error() { 15 log.Println("DEBUG:", strings.ReplaceAll(s, "\n", "\n ")) 16 } 17 } 18 } 19 } 20 21 func traceableRootCause(err error) error { 22 cause := errors.Unwrap(err) 23 if cause != nil && cause != err { 24 rootCause := traceableRootCause(cause) 25 if hasMoreInfo(rootCause, cause) { 26 return rootCause 27 } 28 if hasMoreInfo(cause, err) { 29 return cause 30 } 31 } 32 return err 33 } 34 35 func hasMoreInfo(err, than error) bool { 36 s := than.Error() 37 for _, l := range strings.Split(fmt.Sprintf("%+v", err), "\n") { 38 if !strings.Contains(s, l) { 39 return true 40 } 41 } 42 return false 43 }