k3s/pkg/util/errors.go
Brad Davidson ce17fce058 Add helper function for including stack trace with error message
Not currently used, but was useful in tracking down the specific call path for the empty token handling

Prints error as:
> `msg="Error: starting kubernetes: failed to start cluster: failed to normalize server token; must be in format K10<CA-HASH>::<USERNAME>:<PASSWORD> or <PASSWORD> at github.com/urfave/cli/v2.(*App).RunContext(app.go:333)->github.com/urfave/cli/v2.(*Command).Run(command.go:269)->github.com/urfave/cli/v2.(*Command).Run(command.go:276)->github.com/k3s-io/k3s/pkg/cli/server.Run(server.go:48)->github.com/k3s-io/k3s/pkg/cli/server.run(server.go:629)->github.com/k3s-io/k3s/pkg/server.StartServer(server.go:74)->github.com/k3s-io/k3s/pkg/daemons/control.Server(server.go:72)->github.com/k3s-io/k3s/pkg/cluster.(*Cluster).Start(cluster.go:75)->github.com/k3s-io/k3s/pkg/cluster.Save(storage.go:79)->github.com/k3s-io/k3s/pkg/util.NormalizeToken(token.go:51)"`

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
2026-02-03 14:47:50 -08:00

31 lines
697 B
Go

package util
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, "->"))
}