fix panic when getting error stack

This commit is contained in:
Matthew Penner 2020-11-15 13:34:19 -07:00
parent 121a4d1146
commit 30ec6dc78d

View File

@ -1,12 +1,12 @@
package cli package cli
import ( import (
"emperror.dev/errors"
"fmt" "fmt"
"github.com/apex/log" "github.com/apex/log"
"github.com/apex/log/handlers/cli" "github.com/apex/log/handlers/cli"
color2 "github.com/fatih/color" color2 "github.com/fatih/color"
"github.com/mattn/go-colorable" "github.com/mattn/go-colorable"
"emperror.dev/errors"
"io" "io"
"os" "os"
"sync" "sync"
@ -88,7 +88,14 @@ func getErrorStack(err error, i bool) errors.StackTrace {
if i { if i {
// Just abort out of this and return a stacktrace leading up to this point. It isn't perfect // Just abort out of this and return a stacktrace leading up to this point. It isn't perfect
// but it'll at least include what function lead to this being called which we can then handle. // but it'll at least include what function lead to this being called which we can then handle.
return errors.WrapIf(err, "failed to generate stacktrace for caught error").(tracer).StackTrace() if e, ok = errors.WrapIf(err, "failed to generate stacktrace for caught error").(tracer); ok {
return e.StackTrace()
}
// The errors.WrapIf did not return a interface compatible with `tracer`, so
// we don't have an easy way to get the stacktrace, this should probably be changed
// at some point, but without this the application may panic when handling some errors.
return nil
} }
return getErrorStack(errors.WrapIf(err, err.Error()), true) return getErrorStack(errors.WrapIf(err, err.Error()), true)