From 85a77af5910e5757e4a0868a3a3b68bd1c0fcb0c Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Wed, 29 Jan 2025 17:02:03 +0100 Subject: [PATCH 1/2] Don't print "context canceled" if user terminated Without breaking API compatibility, this patch allows us to know whether a returned `cli/StatusError` was caused by a context cancellation or not, which we can use to provide a nicer UX and not print the Go "context canceled" error message if this is the cause. Signed-off-by: Laura Brehm --- cli/command/container/run.go | 3 +++ cli/command/container/run_test.go | 1 + cli/error.go | 14 +++++++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 5ef3e8b7bf7b..ac780533bfd3 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -324,6 +324,7 @@ func toStatusError(err error) error { if strings.Contains(errMsg, "executable file not found") || strings.Contains(errMsg, "no such file or directory") || strings.Contains(errMsg, "system cannot find the file specified") { return cli.StatusError{ + Cause: err, Status: withHelp(err, "run").Error(), StatusCode: 127, } @@ -331,12 +332,14 @@ func toStatusError(err error) error { if strings.Contains(errMsg, syscall.EACCES.Error()) || strings.Contains(errMsg, syscall.EISDIR.Error()) { return cli.StatusError{ + Cause: err, Status: withHelp(err, "run").Error(), StatusCode: 126, } } return cli.StatusError{ + Cause: err, Status: withHelp(err, "run").Error(), StatusCode: 125, } diff --git a/cli/command/container/run_test.go b/cli/command/container/run_test.go index 4c2ced4f564f..9704c8a042ea 100644 --- a/cli/command/container/run_test.go +++ b/cli/command/container/run_test.go @@ -290,6 +290,7 @@ func TestRunPullTermination(t *testing.T) { select { case cmdErr := <-cmdErrC: assert.Equal(t, cmdErr, cli.StatusError{ + Cause: context.Canceled, StatusCode: 125, Status: "docker: context canceled\n\nRun 'docker run --help' for more information", }) diff --git a/cli/error.go b/cli/error.go index 8c4a5f952bce..1d35b4e77d65 100644 --- a/cli/error.go +++ b/cli/error.go @@ -6,6 +6,7 @@ import ( // StatusError reports an unsuccessful exit by a command. type StatusError struct { + Cause error Status string StatusCode int } @@ -14,8 +15,15 @@ type StatusError struct { // it is returned as-is, otherwise it generates a generic error-message // based on the StatusCode. func (e StatusError) Error() string { - if e.Status == "" { - return "exit status " + strconv.Itoa(e.StatusCode) + if e.Status != "" { + return e.Status } - return e.Status + if e.Cause != nil { + return e.Cause.Error() + } + return "exit status " + strconv.Itoa(e.StatusCode) +} + +func (e StatusError) Unwrap() error { + return e.Cause } From 76e0088b5ca3e8e30f6df82d1f3750c27229c6e0 Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Thu, 6 Feb 2025 14:16:45 +0100 Subject: [PATCH 2/2] minor cleanup Signed-off-by: Laura Brehm --- cmd/docker/docker.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index bb411a7cd2ec..b5cb621923b4 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -37,12 +37,9 @@ func (e errCtxSignalTerminated) Error() string { } func main() { - ctx := context.Background() - err := dockerMain(ctx) - + err := dockerMain(context.Background()) if errors.As(err, &errCtxSignalTerminated{}) { os.Exit(getExitCode(err)) - return } if err != nil && !errdefs.IsCancelled(err) {