Skip to content

Commit

Permalink
Add a note that original error is present
Browse files Browse the repository at this point in the history
  • Loading branch information
lpusok committed Mar 20, 2024
1 parent 1c5c6b7 commit b9c0ad4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 7 additions & 7 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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`,
Expand Down Expand Up @@ -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",
Expand All @@ -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`,
},
}
Expand Down Expand Up @@ -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",
Expand All @@ -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`,
Expand Down
4 changes: 3 additions & 1 deletion errorutil/hiddenerror.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,7 +18,7 @@ func NewHiddenOriginalError(originalErr error) *HiddenOriginalError {

// Error ...
func (h HiddenOriginalError) Error() string {
return ""
return fmt.Sprintf("%T reworded", h.originalErr)
}

// Unwrap ...
Expand Down

0 comments on commit b9c0ad4

Please sign in to comment.