Skip to content

Commit 9d6733b

Browse files
authored
Unwrap kpt errors to prevent printing stacktrace (#1880)
* Unwrap kpt errors to prevent printing stacktrace * Fix failing test
1 parent 631a753 commit 9d6733b

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

e2e/testdata/fn-render/missing-fnconfig/.expected/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
exitCode: 1
16-
stdErr: 'fn gcr.io/kpt-fn/set-label:unstable: missing function config "labelconfig1.yaml"'
16+
stdErr: 'Error: missing function config "labelconfig1.yaml"'
1717
stdOut: |
1818
[RUNNING] "gcr.io/kpt-fn/set-label:unstable"
1919
[PASS] "gcr.io/kpt-fn/set-label:unstable"

internal/errors/errors.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,20 @@ func UnwrapKioError(err error) error {
248248
}
249249
return kioErr.Err
250250
}
251+
252+
// UnwrapErrors unwraps any *Error errors in the chain and returns
253+
// the first error it finds that is of a different type. If no such error
254+
// can be found, the last return value will be false.
255+
//nolint
256+
func UnwrapErrors(err error) (error, bool) {
257+
for {
258+
if err == nil {
259+
return nil, false
260+
}
261+
e, ok := err.(*Error)
262+
if !ok {
263+
return err, true
264+
}
265+
err = e.Err
266+
}
267+
}

internal/util/cmdutil/cmdutil.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"strings"
2424

2525
"github.com/GoogleContainerTools/kpt/internal/errors"
26-
goerrors "github.com/go-errors/errors"
2726
"github.com/spf13/cobra"
2827
)
2928

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

43-
func PrintErrorStacktrace(err error) {
42+
func PrintErrorStacktrace() bool {
4443
e := os.Getenv(StackTraceOnErrors)
4544
if StackOnError || e == trueString || e == "1" {
46-
if err, ok := err.(*goerrors.Error); ok {
47-
fmt.Fprintf(os.Stderr, "%s", err.Stack())
48-
}
45+
return true
4946
}
47+
return false
5048
}
5149

5250
// StackOnError if true, will print a stack trace on failure.

internal/util/parse/parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func getDest(v, repo, subdir string) (string, error) {
162162
parent := filepath.Dir(v)
163163
if _, err := os.Stat(parent); os.IsNotExist(err) {
164164
// error -- fetch to directory where parent does not exist
165-
return "", errors.Errorf("parent directory %s does not exist", parent)
165+
return "", errors.Errorf("parent directory %q does not exist", parent)
166166
}
167167
// fetch to a specific directory -- don't default the name
168168
return v, nil
@@ -180,7 +180,7 @@ func getDest(v, repo, subdir string) (string, error) {
180180

181181
// make sure the destination directory does not yet exist yet
182182
if _, err := os.Stat(v); !os.IsNotExist(err) {
183-
return "", errors.Errorf("destination directory %s already exists", v)
183+
return "", errors.Errorf("destination directory %q already exists", v)
184184
}
185185
return v, nil
186186
}

main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

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

8484
// Then try to see if it is of type *errors.Error
8585
var kptErr *errors.Error
8686
if errors.As(err, &kptErr) {
87+
unwrapped, ok := errors.UnwrapErrors(kptErr)
88+
if ok && !cmdutil.PrintErrorStacktrace() {
89+
fmt.Fprintf(cmd.ErrOrStderr(), "Error: %s \n", unwrapped.Error())
90+
return 1
91+
}
8792
fmt.Fprintf(cmd.ErrOrStderr(), "%s \n", kptErr.Error())
8893
return 1
8994
}

0 commit comments

Comments
 (0)