Skip to content

Commit 85a77af

Browse files
committed
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 <[email protected]>
1 parent d48fb9f commit 85a77af

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
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
}

0 commit comments

Comments
 (0)