From ce17fce058d35899a38f5302905e48eebbe025ce Mon Sep 17 00:00:00 2001 From: Brad Davidson Date: Fri, 30 Jan 2026 00:05:16 +0000 Subject: [PATCH] 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::: or 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 --- pkg/util/errors.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) 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, "->")) +}