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 ExitError type #196

Merged
merged 12 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 21 additions & 2 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ type Command interface {
Wait() error
}

// FormattedError ...
type FormattedError struct {
godrei marked this conversation as resolved.
Show resolved Hide resolved
formattedErr error
originalCommandErr error
}

// Error returns the formatted error message. Does not include the original error message (`exit status 1`).
func (c *FormattedError) Error() string {
return c.formattedErr.Error()
}

// Unwrap is needed for errors.Is and errors.As to work correctly.
// It does not change errorutil.FormattedError's behavior, as it uses Unwrap() error (not []error).
func (c *FormattedError) Unwrap() []error {
return []error{c.originalCommandErr}
}

type command struct {
cmd *exec.Cmd
errorCollector *errorCollector
Expand Down Expand Up @@ -169,9 +186,11 @@ func (c command) wrapError(err error) error {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
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")))
formattedErr := fmt.Errorf("command failed with exit status %d (%s): %w", exitErr.ExitCode(), c.PrintableCommandArgs(), errors.New(strings.Join(c.errorCollector.errorLines, "\n")))
return &FormattedError{formattedErr: formattedErr, originalCommandErr: err}
}
return fmt.Errorf("command failed with exit status %d (%s): %w", exitErr.ExitCode(), c.PrintableCommandArgs(), errors.New("check the command's output for details"))
formattedErr := fmt.Errorf("command failed with exit status %d (%s): %w", exitErr.ExitCode(), c.PrintableCommandArgs(), errors.New("check the command's output for details"))
return &FormattedError{formattedErr: formattedErr, originalCommandErr: err}
}
return fmt.Errorf("executing command failed (%s): %w", c.PrintableCommandArgs(), err)
}
Expand Down
29 changes: 28 additions & 1 deletion command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package command

import (
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
"testing"

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

Expand Down Expand Up @@ -65,9 +67,14 @@ Error: fourth error`,
gotErrMsg = err.Error()
}
if gotErrMsg != tt.wantErr {
t.Errorf("command.Run() error = %v, wantErr %v", gotErrMsg, tt.wantErr)
t.Errorf("command.Run() error = \n%v\n, wantErr \n%v\n", gotErrMsg, tt.wantErr)
return
}

gotFormattedMsg := errorutil.FormattedError(err)
if gotFormattedMsg != tt.wantErr {
t.Errorf("FormattedError() error = \n%v\n, wantErr \n%v\n", gotFormattedMsg, tt.wantErr)
}
})
}
}
Expand Down Expand Up @@ -123,6 +130,18 @@ func TestRunCmdAndReturnExitCode(t *testing.T) {
t.Errorf("command.RunAndReturnExitCode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && tt.wantExitCode > 0 {
var exitErr *exec.ExitError

if ok := errors.As(err, &exitErr); !ok {
t.Errorf("command.RunAndReturnExitCode() did nor return ExitError type: %s", err)
return
}

if exitErr.ExitCode() != tt.wantExitCode {
t.Errorf("command.RunAndReturnExitCode() exit code = %v, want %v", exitErr.ExitCode(), tt.wantExitCode)
}
}
if gotExitCode != tt.wantExitCode {
t.Errorf("command.RunAndReturnExitCode() = %v, want %v", gotExitCode, tt.wantExitCode)
}
Expand Down Expand Up @@ -180,6 +199,10 @@ Error: second error`,
t.Errorf("command.Run() error = %v, wantErr %v", gotErrMsg, tt.wantErr)
return
}
gotFormattedMsg := errorutil.FormattedError(err)
if gotFormattedMsg != tt.wantErr {
t.Errorf("FormattedError() error = \n%v\n, wantErr \n%v\n", gotFormattedMsg, tt.wantErr)
}
})
}
}
Expand Down Expand Up @@ -236,6 +259,10 @@ Error: fourth error`,
t.Errorf("command.Run() error = %v, wantErr %v", gotErrMsg, tt.wantErr)
return
}
gotFormattedMsg := errorutil.FormattedError(err)
if gotFormattedMsg != tt.wantErr {
godrei marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("FormattedError() error = \n%v\n, wantErr \n%v\n", gotFormattedMsg, tt.wantErr)
}
})
}
}
Expand Down