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

Don't print "context canceled" if user terminated #5778

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,22 @@ 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,
}
}

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,
}
Expand Down
1 change: 1 addition & 0 deletions cli/command/container/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down
14 changes: 11 additions & 3 deletions cli/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

// StatusError reports an unsuccessful exit by a command.
type StatusError struct {
Cause error
Status string
StatusCode int
}
Expand All @@ -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
}
5 changes: 1 addition & 4 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading