Skip to content

Commit 11999b1

Browse files
authored
Merge pull request #5778 from laurazard/add-cause-statuserr
Don't print "context canceled" if user terminated
2 parents eec977b + 76e0088 commit 11999b1

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

cli/command/container/run.go

+3
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,22 @@ func toStatusError(err error) error {
324324

325325
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") {
326326
return cli.StatusError{
327+
Cause: err,
327328
Status: withHelp(err, "run").Error(),
328329
StatusCode: 127,
329330
}
330331
}
331332

332333
if strings.Contains(errMsg, syscall.EACCES.Error()) || strings.Contains(errMsg, syscall.EISDIR.Error()) {
333334
return cli.StatusError{
335+
Cause: err,
334336
Status: withHelp(err, "run").Error(),
335337
StatusCode: 126,
336338
}
337339
}
338340

339341
return cli.StatusError{
342+
Cause: err,
340343
Status: withHelp(err, "run").Error(),
341344
StatusCode: 125,
342345
}

cli/command/container/run_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ func TestRunPullTermination(t *testing.T) {
290290
select {
291291
case cmdErr := <-cmdErrC:
292292
assert.Equal(t, cmdErr, cli.StatusError{
293+
Cause: context.Canceled,
293294
StatusCode: 125,
294295
Status: "docker: context canceled\n\nRun 'docker run --help' for more information",
295296
})

cli/error.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
// StatusError reports an unsuccessful exit by a command.
88
type StatusError struct {
9+
Cause error
910
Status string
1011
StatusCode int
1112
}
@@ -14,8 +15,15 @@ type StatusError struct {
1415
// it is returned as-is, otherwise it generates a generic error-message
1516
// based on the StatusCode.
1617
func (e StatusError) Error() string {
17-
if e.Status == "" {
18-
return "exit status " + strconv.Itoa(e.StatusCode)
18+
if e.Status != "" {
19+
return e.Status
1920
}
20-
return e.Status
21+
if e.Cause != nil {
22+
return e.Cause.Error()
23+
}
24+
return "exit status " + strconv.Itoa(e.StatusCode)
25+
}
26+
27+
func (e StatusError) Unwrap() error {
28+
return e.Cause
2129
}

cmd/docker/docker.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@ func (e errCtxSignalTerminated) Error() string {
3737
}
3838

3939
func main() {
40-
ctx := context.Background()
41-
err := dockerMain(ctx)
42-
40+
err := dockerMain(context.Background())
4341
if errors.As(err, &errCtxSignalTerminated{}) {
4442
os.Exit(getExitCode(err))
45-
return
4643
}
4744

4845
if err != nil && !errdefs.IsCancelled(err) {

0 commit comments

Comments
 (0)