diff --git a/pkg/util/errors.go b/pkg/util/errors.go index 92a648be8af..237987c9ff0 100644 --- a/pkg/util/errors.go +++ b/pkg/util/errors.go @@ -1,6 +1,31 @@ package util -import "errors" +import ( + "errors" + "fmt" + "path" + "runtime" + "slices" + "strings" +) var ErrCommandNoArgs = errors.New("this command does not take any arguments") var ErrUnsupportedPlatform = errors.New("unsupported platform") + +func ErrWithStack(message string) error { + stack := []string{} + pcs := make([]uintptr, 32) + runtime.Callers(2, pcs) + frames := runtime.CallersFrames(pcs) + for { + frame, more := frames.Next() + if !strings.HasPrefix(frame.Function, "runtime.") { + stack = append(stack, fmt.Sprintf("%s(%s:%d)", frame.Function, path.Base(frame.File), frame.Line)) + } + if !more { + break + } + } + slices.Reverse(stack) + return errors.New(message + " at " + strings.Join(stack, "->")) +}