Merge pull request #5854 from Benehiko/fix-exec-msg

cmd/docker: do not print error status on exec/run
This commit is contained in:
Paweł Gronowski 2025-02-21 12:20:31 +00:00 committed by GitHub
commit 77a8a8c6ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 9 additions and 13 deletions

View File

@ -3,6 +3,7 @@ package network
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
@ -68,7 +69,7 @@ func runRemove(ctx context.Context, dockerCLI command.Cli, networks []string, op
} }
if status != 0 { if status != 0 {
return cli.StatusError{StatusCode: status} return cli.StatusError{StatusCode: status, Status: "exit status " + strconv.Itoa(status)}
} }
return nil return nil
} }

View File

@ -1,9 +1,5 @@
package cli package cli
import (
"strconv"
)
// StatusError reports an unsuccessful exit by a command. // StatusError reports an unsuccessful exit by a command.
type StatusError struct { type StatusError struct {
Cause error Cause error
@ -21,7 +17,9 @@ func (e StatusError) Error() string {
if e.Cause != nil { if e.Cause != nil {
return e.Cause.Error() return e.Cause.Error()
} }
return "exit status " + strconv.Itoa(e.StatusCode) // we don't want to set a default message here,
// some commands might want to be explicit about the error message
return ""
} }
func (e StatusError) Unwrap() error { func (e StatusError) Unwrap() error {

View File

@ -43,7 +43,9 @@ func main() {
} }
if err != nil && !errdefs.IsCancelled(err) { if err != nil && !errdefs.IsCancelled(err) {
if err.Error() != "" {
_, _ = fmt.Fprintln(os.Stderr, err) _, _ = fmt.Fprintln(os.Stderr, err)
}
os.Exit(getExitCode(err)) os.Exit(getExitCode(err))
} }
} }

View File

@ -137,7 +137,6 @@ func TestPluginSocketBackwardsCompatible(t *testing.T) {
assert.Assert(t, errors.As(err, &exitError)) assert.Assert(t, errors.As(err, &exitError))
assert.Check(t, exitError.Exited()) assert.Check(t, exitError.Exited())
assert.Check(t, is.Equal(exitError.ExitCode(), 1)) assert.Check(t, is.Equal(exitError.ExitCode(), 1))
assert.Check(t, is.ErrorContains(err, "exit status 1"))
// the plugin process does not receive a SIGINT and does // the plugin process does not receive a SIGINT and does
// the CLI cannot cancel it over the socket, so it kills // the CLI cannot cancel it over the socket, so it kills
@ -199,11 +198,10 @@ func TestPluginSocketCommunication(t *testing.T) {
assert.Assert(t, errors.As(err, &exitError)) assert.Assert(t, errors.As(err, &exitError))
assert.Check(t, exitError.Exited()) assert.Check(t, exitError.Exited())
assert.Check(t, is.Equal(exitError.ExitCode(), 2)) assert.Check(t, is.Equal(exitError.ExitCode(), 2))
assert.Check(t, is.ErrorContains(err, "exit status 2"))
// the plugin does not get signalled, but it does get its // the plugin does not get signalled, but it does get its
// context canceled by the CLI through the socket // context canceled by the CLI through the socket
const expected = "test-socket: exiting after context was done\nexit status 2" const expected = "test-socket: exiting after context was done"
actual := strings.TrimSpace(string(out)) actual := strings.TrimSpace(string(out))
assert.Check(t, is.Equal(actual, expected)) assert.Check(t, is.Equal(actual, expected))
}) })
@ -238,7 +236,6 @@ func TestPluginSocketCommunication(t *testing.T) {
assert.Assert(t, errors.As(err, &exitError)) assert.Assert(t, errors.As(err, &exitError))
assert.Check(t, exitError.Exited()) assert.Check(t, exitError.Exited())
assert.Check(t, is.Equal(exitError.ExitCode(), 1)) assert.Check(t, is.Equal(exitError.ExitCode(), 1))
assert.Check(t, is.ErrorContains(err, "exit status 1"))
// the plugin process does not receive a SIGINT and does // the plugin process does not receive a SIGINT and does
// not exit after having it's context canceled, so the CLI // not exit after having it's context canceled, so the CLI

View File

@ -58,5 +58,4 @@ func TestAttachInterrupt(t *testing.T) {
// the CLI should exit with 33 (the SIGINT was forwarded to the container), and the // the CLI should exit with 33 (the SIGINT was forwarded to the container), and the
// CLI process waited for the container exit and properly captured/set the exit code // CLI process waited for the container exit and properly captured/set the exit code
assert.Equal(t, c.ProcessState.ExitCode(), 33) assert.Equal(t, c.ProcessState.ExitCode(), 33)
assert.Equal(t, d.String(), "exit status 33\n")
} }

View File

@ -68,7 +68,6 @@ func TestRunAttach(t *testing.T) {
} }
assert.Equal(t, c.ProcessState.ExitCode(), 7) assert.Equal(t, c.ProcessState.ExitCode(), 7)
assert.Check(t, is.Contains(d.String(), "exit status 7"))
}) })
} }
} }