mirror of
https://github.com/k3s-io/k3s.git
synced 2026-02-03 20:39:49 -05:00
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>
This commit is contained in:
parent
5e63bbe260
commit
ce17fce058
1 changed files with 26 additions and 1 deletions
|
|
@ -1,6 +1,31 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
var ErrCommandNoArgs = errors.New("this command does not take any arguments")
|
var ErrCommandNoArgs = errors.New("this command does not take any arguments")
|
||||||
var ErrUnsupportedPlatform = errors.New("unsupported platform")
|
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, "->"))
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue