Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return original command error in error tree #184

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/bitrise-io/go-utils/v2/env"
"github.com/bitrise-io/go-utils/v2/errorutil"
)

// ErrorFinder ...
Expand Down Expand Up @@ -168,10 +169,11 @@ func printableCommandArgs(isQuoteFirst bool, fullCommandArgs []string) string {
func (c command) wrapError(err error) error {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
origErr := errorutil.NewHiddenOriginalError(err)
if c.errorCollector != nil && len(c.errorCollector.errorLines) > 0 {
return fmt.Errorf("command failed with exit status %d (%s): %w", exitErr.ExitCode(), c.PrintableCommandArgs(), errors.New(strings.Join(c.errorCollector.errorLines, "\n")))
return fmt.Errorf("command failed with exit status %d (%s): %w%w", exitErr.ExitCode(), c.PrintableCommandArgs(), errors.New(strings.Join(c.errorCollector.errorLines, "\n")), origErr)
}
return fmt.Errorf("command failed with exit status %d (%s): %w", exitErr.ExitCode(), c.PrintableCommandArgs(), errors.New("check the command's output for details"))
return fmt.Errorf("command failed with exit status %d (%s): %w %w", exitErr.ExitCode(), c.PrintableCommandArgs(), errors.New("check the command's output for details"), origErr)
}
return fmt.Errorf("executing command failed (%s): %w", c.PrintableCommandArgs(), err)
}
Expand Down
25 changes: 25 additions & 0 deletions errorutil/hiddenerror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package errorutil

// HiddenOriginalError allows to include an error in the error chain but do not print it.
// this allows replacing it with a more readable error message,
// while allowing code to check for the type of the error
type HiddenOriginalError struct {
originalErr error
}

// NewHiddenOriginalError ...
func NewHiddenOriginalError(originalErr error) *HiddenOriginalError {
return &HiddenOriginalError{
originalErr: originalErr,
}
}

// Error ...
func (h HiddenOriginalError) Error() string {
return ""
}

// Unwrap ...
func (h HiddenOriginalError) Unwrap() error {
return h.originalErr
}
Loading