From b9c0ad4808c2b4c6c91e702d62c39b3c31ff18f5 Mon Sep 17 00:00:00 2001 From: LaszloP <7979773+lpusok@users.noreply.github.com> Date: Wed, 20 Mar 2024 17:05:18 +0100 Subject: [PATCH] Add a note that original error is present --- command/command.go | 4 ++-- command/command_test.go | 14 +++++++------- errorutil/hiddenerror.go | 4 +++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/command/command.go b/command/command.go index 683bc7e..5bada11 100644 --- a/command/command.go +++ b/command/command.go @@ -171,9 +171,9 @@ func (c command) wrapError(err error) error { 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%w", exitErr.ExitCode(), c.PrintableCommandArgs(), errors.New(strings.Join(c.errorCollector.errorLines, "\n")), origErr) + return fmt.Errorf("[%w] command failed with exit status %d (%s): %w", origErr, 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("check the command's output for details"), origErr) + return fmt.Errorf("[%w] command failed with exit status %d (%s): %w", origErr, exitErr.ExitCode(), c.PrintableCommandArgs(), errors.New("check the command's output for details")) } return fmt.Errorf("executing command failed (%s): %w", c.PrintableCommandArgs(), err) } diff --git a/command/command_test.go b/command/command_test.go index 99fa00e..77dbea8 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -20,7 +20,7 @@ func TestRunErrors(t *testing.T) { { name: "command without stdout set", cmd: command{cmd: exec.Command("bash", "testdata/exit_with_message.sh")}, - wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): check the command's output for details`, + wantErr: `[*exec.ExitError reworded] command failed with exit status 1 (bash "testdata/exit_with_message.sh"): check the command's output for details`, }, { name: "command with stdout set", @@ -30,7 +30,7 @@ func TestRunErrors(t *testing.T) { c.Stdout = &out return command{cmd: c} }(), - wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): check the command's output for details`, + wantErr: `[*exec.ExitError reworded] command failed with exit status 1 (bash "testdata/exit_with_message.sh"): check the command's output for details`, }, { name: "command with error finder", @@ -51,7 +51,7 @@ func TestRunErrors(t *testing.T) { errorCollector: &errorCollector{errorFinder: errorFinder}, } }(), - wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: first error + wantErr: `[*exec.ExitError reworded] command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: first error Error: second error Error: third error Error: fourth error`, @@ -144,7 +144,7 @@ func TestRunAndReturnTrimmedOutput(t *testing.T) { cmd: c, } }(), - wantErr: "command failed with exit status 1 (bash \"testdata/exit_with_message.sh\"): check the command's output for details", + wantErr: "[*exec.ExitError reworded] command failed with exit status 1 (bash \"testdata/exit_with_message.sh\"): check the command's output for details", }, { name: "command with error finder", @@ -165,7 +165,7 @@ func TestRunAndReturnTrimmedOutput(t *testing.T) { errorCollector: &errorCollector{errorFinder: errorFinder}, } }(), - wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: first error + wantErr: `[*exec.ExitError reworded] command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: first error Error: second error`, }, } @@ -198,7 +198,7 @@ func TestRunAndReturnTrimmedCombinedOutput(t *testing.T) { cmd: c, } }(), - wantErr: "command failed with exit status 1 (bash \"testdata/exit_with_message.sh\"): check the command's output for details ", + wantErr: "[*exec.ExitError reworded] command failed with exit status 1 (bash \"testdata/exit_with_message.sh\"): check the command's output for details", }, { name: "command with error finder", @@ -219,7 +219,7 @@ func TestRunAndReturnTrimmedCombinedOutput(t *testing.T) { errorCollector: &errorCollector{errorFinder: errorFinder}, } }(), - wantErr: `command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: first error + wantErr: `[*exec.ExitError reworded] command failed with exit status 1 (bash "testdata/exit_with_message.sh"): Error: first error Error: second error Error: third error Error: fourth error`, diff --git a/errorutil/hiddenerror.go b/errorutil/hiddenerror.go index 85c9838..1650bea 100644 --- a/errorutil/hiddenerror.go +++ b/errorutil/hiddenerror.go @@ -1,5 +1,7 @@ package errorutil +import "fmt" + // 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 @@ -16,7 +18,7 @@ func NewHiddenOriginalError(originalErr error) *HiddenOriginalError { // Error ... func (h HiddenOriginalError) Error() string { - return "" + return fmt.Sprintf("%T reworded", h.originalErr) } // Unwrap ...