-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shove the original command error in the error tree. This allows checking for the specific exit code by code like this: var exitError *exec.ExitError if errors.As(err, &exitError) { Added command.FormattedError type to work around issue of not returning the original exec.ExitError type. The Unwrap() method returns the original error, so the above example will now work.
- Loading branch information
Showing
5 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package command | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// ExitStatusError ... | ||
type ExitStatusError struct { | ||
readableReason error | ||
originalExitErr error | ||
} | ||
|
||
// NewExitStatusError ... | ||
func NewExitStatusError(printableCmdArgs string, exitErr *exec.ExitError, errorLines []string) error { | ||
reasonMsg := fmt.Sprintf("command failed with exit status %d (%s)", exitErr.ExitCode(), printableCmdArgs) | ||
if len(errorLines) == 0 { | ||
return &ExitStatusError{ | ||
readableReason: fmt.Errorf("%s: %w", reasonMsg, errors.New("check the command's output for details")), | ||
originalExitErr: exitErr, | ||
} | ||
} | ||
|
||
return &ExitStatusError{ | ||
readableReason: fmt.Errorf("%s: %w", reasonMsg, errors.New(strings.Join(errorLines, "\n"))), | ||
originalExitErr: exitErr, | ||
} | ||
} | ||
|
||
// Error returns the formatted error message. Does not include the original error message (`exit status 1`). | ||
func (e *ExitStatusError) Error() string { | ||
return e.readableReason.Error() | ||
} | ||
|
||
// Unwrap is needed for errors.Is and errors.As to work correctly. | ||
func (e *ExitStatusError) Unwrap() error { | ||
return e.originalExitErr | ||
} | ||
|
||
// Reason returns the user-friendly error, to be used by errorutil.ErrorFormatter. | ||
func (e *ExitStatusError) Reason() error { | ||
return e.readableReason | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters