Kaynağa Gözat

ContainerTop: improve error message

If "ps" fails, in many cases it prints a meaningful error message
which a user can benefit from. Let's use it.

While at it, let's use errdefs.System to classify the error,
as well as errors.Wrap.

Before:

> $ docker top $CT <any bad ps options>
> Error response from daemon: Error running ps: exit status 1

After:

> $ docker top $CT auxm
> Error response from daemon: ps: error: thread display conflicts with forest display

or

> $ docker top $CT saur
> Error response from daemon: ps: error: conflicting format options

or, if there's no meaningful error on stderr, same as before:

> $ docker top $CT 1234
> Error response from daemon: ps: exit status 1

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Kir Kolyshkin 7 yıl önce
ebeveyn
işleme
a41328d570
1 değiştirilmiş dosya ile 11 ekleme ve 1 silme
  1. 11 1
      daemon/top_unix.go

+ 11 - 1
daemon/top_unix.go

@@ -3,6 +3,7 @@
 package daemon // import "github.com/docker/docker/daemon"
 package daemon // import "github.com/docker/docker/daemon"
 
 
 import (
 import (
+	"bytes"
 	"context"
 	"context"
 	"fmt"
 	"fmt"
 	"os/exec"
 	"os/exec"
@@ -11,6 +12,8 @@ import (
 	"strings"
 	"strings"
 
 
 	"github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/container"
+	"github.com/docker/docker/errdefs"
+	"github.com/pkg/errors"
 )
 )
 
 
 func validatePSArgs(psArgs string) error {
 func validatePSArgs(psArgs string) error {
@@ -167,7 +170,14 @@ func (daemon *Daemon) ContainerTop(name string, psArgs string) (*container.Conta
 		// so retry without it
 		// so retry without it
 		output, err = exec.Command("ps", args...).Output()
 		output, err = exec.Command("ps", args...).Output()
 		if err != nil {
 		if err != nil {
-			return nil, fmt.Errorf("Error running ps: %v", err)
+			if ee, ok := err.(*exec.ExitError); ok {
+				// first line of stderr shows why ps failed
+				line := bytes.SplitN(ee.Stderr, []byte{'\n'}, 2)
+				if len(line) > 0 && len(line[0]) > 0 {
+					err = errors.New(string(line[0]))
+				}
+			}
+			return nil, errdefs.System(errors.Wrap(err, "ps"))
 		}
 		}
 	}
 	}
 	procList, err := parsePSOutput(output, procs)
 	procList, err := parsePSOutput(output, procs)