From 3c5a58c5f894218d7c37c91cdec056fc7e8ce246 Mon Sep 17 00:00:00 2001 From: dairlair <51401608+dairlair@users.noreply.github.com> Date: Thu, 31 Aug 2023 17:29:08 +0300 Subject: [PATCH] Refactor checkErr func I moved a simpler condition to the beginning of the function (when the error == nil). This has substantially streamlined the function for readability and comprehension of its logic flow. --- cmd/kubeadm/app/util/error.go | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/cmd/kubeadm/app/util/error.go b/cmd/kubeadm/app/util/error.go index b67b395da41..ea0ae67091b 100644 --- a/cmd/kubeadm/app/util/error.go +++ b/cmd/kubeadm/app/util/error.go @@ -73,28 +73,25 @@ type preflightError interface { // checkErr formats a given error as a string and calls the passed handleErr // func with that string and an exit code. func checkErr(err error, handleErr func(string, int)) { + if err == nil { + return + } - var msg string - if err != nil { - msg = fmt.Sprintf("%s\nTo see the stack trace of this error execute with --v=5 or higher", err.Error()) - // check if the verbosity level in klog is high enough and print a stack trace. - f := flag.CommandLine.Lookup("v") - if f != nil { - // assume that the "v" flag contains a parseable Int32 as per klog's "Level" type alias, - // thus no error from ParseInt is handled here. - if v, e := strconv.ParseInt(f.Value.String(), 10, 32); e == nil { - // https://git.k8s.io/community/contributors/devel/sig-instrumentation/logging.md - // klog.V(5) - Trace level verbosity - if v > 4 { - msg = fmt.Sprintf("%+v", err) - } + msg := fmt.Sprintf("%s\nTo see the stack trace of this error execute with --v=5 or higher", err.Error()) + // check if the verbosity level in klog is high enough and print a stack trace. + f := flag.CommandLine.Lookup("v") + if f != nil { + // assume that the "v" flag contains a parseable Int32 as per klog's "Level" type alias, + // thus no error from ParseInt is handled here. + if v, e := strconv.ParseInt(f.Value.String(), 10, 32); e == nil { + // https://git.k8s.io/community/contributors/devel/sig-instrumentation/logging.md + // klog.V(5) - Trace level verbosity + if v > 4 { + msg = fmt.Sprintf("%+v", err) } } } - if err == nil { - return - } switch { case err == ErrExit: handleErr("", DefaultErrorExitCode)