Merge pull request #5778 from laurazard/add-cause-statuserr

Don't print "context canceled" if user terminated
This commit is contained in:
Paweł Gronowski 2025-02-06 14:20:38 +00:00 committed by GitHub
commit 11999b16e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 7 deletions

View File

@ -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,
}

View File

@ -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",
})

View File

@ -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
}

View File

@ -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) {