Skip to content

Commit

Permalink
Unwrap kpt errors to prevent printing stacktrace (#1880)
Browse files Browse the repository at this point in the history
* Unwrap kpt errors to prevent printing stacktrace

* Fix failing test
  • Loading branch information
mortent committed May 4, 2021
1 parent 631a753 commit 9d6733b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

exitCode: 1
stdErr: 'fn gcr.io/kpt-fn/set-label:unstable: missing function config "labelconfig1.yaml"'
stdErr: 'Error: missing function config "labelconfig1.yaml"'
stdOut: |
[RUNNING] "gcr.io/kpt-fn/set-label:unstable"
[PASS] "gcr.io/kpt-fn/set-label:unstable"
17 changes: 17 additions & 0 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,20 @@ func UnwrapKioError(err error) error {
}
return kioErr.Err
}

// UnwrapErrors unwraps any *Error errors in the chain and returns
// the first error it finds that is of a different type. If no such error
// can be found, the last return value will be false.
//nolint
func UnwrapErrors(err error) (error, bool) {
for {
if err == nil {
return nil, false
}
e, ok := err.(*Error)
if !ok {
return err, true
}
err = e.Err
}
}
8 changes: 3 additions & 5 deletions internal/util/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"

"github.com/GoogleContainerTools/kpt/internal/errors"
goerrors "github.com/go-errors/errors"
"github.com/spf13/cobra"
)

Expand All @@ -40,13 +39,12 @@ func FixDocs(old, new string, c *cobra.Command) {
c.Example = strings.ReplaceAll(c.Example, old, new)
}

func PrintErrorStacktrace(err error) {
func PrintErrorStacktrace() bool {
e := os.Getenv(StackTraceOnErrors)
if StackOnError || e == trueString || e == "1" {
if err, ok := err.(*goerrors.Error); ok {
fmt.Fprintf(os.Stderr, "%s", err.Stack())
}
return true
}
return false
}

// StackOnError if true, will print a stack trace on failure.
Expand Down
4 changes: 2 additions & 2 deletions internal/util/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func getDest(v, repo, subdir string) (string, error) {
parent := filepath.Dir(v)
if _, err := os.Stat(parent); os.IsNotExist(err) {
// error -- fetch to directory where parent does not exist
return "", errors.Errorf("parent directory %s does not exist", parent)
return "", errors.Errorf("parent directory %q does not exist", parent)
}
// fetch to a specific directory -- don't default the name
return v, nil
Expand All @@ -180,7 +180,7 @@ func getDest(v, repo, subdir string) (string, error) {

// make sure the destination directory does not yet exist yet
if _, err := os.Stat(v); !os.IsNotExist(err) {
return "", errors.Errorf("destination directory %s already exists", v)
return "", errors.Errorf("destination directory %q already exists", v)
}
return v, nil
}
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/GoogleContainerTools/kpt/internal/errors"
"github.com/GoogleContainerTools/kpt/internal/errors/resolver"
"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
"github.com/GoogleContainerTools/kpt/run"
"github.com/spf13/cobra"
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand Down Expand Up @@ -75,15 +76,19 @@ func runMain() int {
func handleErr(cmd *cobra.Command, err error) int {
// First attempt to see if we can resolve the error into a specific
// error message.
re, resolved := resolver.ResolveError(err)
if resolved {
if re, resolved := resolver.ResolveError(err); resolved {
fmt.Fprintf(cmd.ErrOrStderr(), "%s \n", re.Message)
return re.ExitCode
}

// Then try to see if it is of type *errors.Error
var kptErr *errors.Error
if errors.As(err, &kptErr) {
unwrapped, ok := errors.UnwrapErrors(kptErr)
if ok && !cmdutil.PrintErrorStacktrace() {
fmt.Fprintf(cmd.ErrOrStderr(), "Error: %s \n", unwrapped.Error())
return 1
}
fmt.Fprintf(cmd.ErrOrStderr(), "%s \n", kptErr.Error())
return 1
}
Expand Down

0 comments on commit 9d6733b

Please sign in to comment.