diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-09-18 13:36:08 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-09-19 08:32:49 +0300 |
commit | ebec60281e44f2e2da3f28d014bd09b0703dec9b (patch) | |
tree | b7472cc81ae78c6d5338e1fb223783be27aec0be | |
parent | 0bef3b7e4677bb820c8ab8946024688f27fc5282 (diff) |
command: Recognize nested exit status
The `command.ExitStatus()` function is responsible for extracting an
exit code from an `exec.ExitError`. The way this is done is extremely
fragile though as we try to directly cast the passed-in error to the
target error type. It's quite likely though that the error may be
wrapped somewhere in the callchain, which would break the functionality.
Refactor the function to instead use `errors.As()` such that it knows to
peel the error chain correctly.
-rw-r--r-- | internal/command/command.go | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/internal/command/command.go b/internal/command/command.go index 5f19dc1c3..ad8906e9d 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -585,12 +585,12 @@ func AllowedEnvironment(envs []string) []string { // ExitStatus will return the exit-code from an error returned by Wait(). func ExitStatus(err error) (int, bool) { - exitError, ok := err.(*exec.ExitError) - if !ok { - return 0, false + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + return exitErr.ExitCode(), true } - return exitError.ExitCode(), true + return 0, false } func methodFromContext(ctx context.Context) (service string, method string) { |