1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #19959 from WeiZhang555/fix-cli-print-err

Remove redundant error message
This commit is contained in:
Alexander Morozov 2016-02-03 10:56:19 -08:00
commit 28a7577a02
17 changed files with 58 additions and 32 deletions

View file

@ -21,7 +21,7 @@ func (cli *DockerCli) CmdKill(args ...string) error {
var errs []string var errs []string
for _, name := range cmd.Args() { for _, name := range cmd.Args() {
if err := cli.client.ContainerKill(name, *signal); err != nil { if err := cli.client.ContainerKill(name, *signal); err != nil {
errs = append(errs, fmt.Sprintf("Failed to kill container (%s): %s", name, err)) errs = append(errs, err.Error())
} else { } else {
fmt.Fprintf(cli.out, "%s\n", name) fmt.Fprintf(cli.out, "%s\n", name)
} }

View file

@ -20,7 +20,7 @@ func (cli *DockerCli) CmdPause(args ...string) error {
var errs []string var errs []string
for _, name := range cmd.Args() { for _, name := range cmd.Args() {
if err := cli.client.ContainerPause(name); err != nil { if err := cli.client.ContainerPause(name); err != nil {
errs = append(errs, fmt.Sprintf("Failed to pause container (%s): %s", name, err)) errs = append(errs, err.Error())
} else { } else {
fmt.Fprintf(cli.out, "%s\n", name) fmt.Fprintf(cli.out, "%s\n", name)
} }

View file

@ -21,7 +21,7 @@ func (cli *DockerCli) CmdRestart(args ...string) error {
var errs []string var errs []string
for _, name := range cmd.Args() { for _, name := range cmd.Args() {
if err := cli.client.ContainerRestart(name, *nSeconds); err != nil { if err := cli.client.ContainerRestart(name, *nSeconds); err != nil {
errs = append(errs, fmt.Sprintf("Failed to kill container (%s): %s", name, err)) errs = append(errs, err.Error())
} else { } else {
fmt.Fprintf(cli.out, "%s\n", name) fmt.Fprintf(cli.out, "%s\n", name)
} }

View file

@ -48,7 +48,7 @@ func (cli *DockerCli) removeContainer(containerID string, removeVolumes, removeL
Force: force, Force: force,
} }
if err := cli.client.ContainerRemove(options); err != nil { if err := cli.client.ContainerRemove(options); err != nil {
return fmt.Errorf("Failed to remove container (%s): %v", containerID, err) return err
} }
return nil return nil
} }

View file

@ -39,7 +39,7 @@ func (cli *DockerCli) CmdRmi(args ...string) error {
dels, err := cli.client.ImageRemove(options) dels, err := cli.client.ImageRemove(options)
if err != nil { if err != nil {
errs = append(errs, fmt.Sprintf("Failed to remove image (%s): %s", name, err)) errs = append(errs, err.Error())
} else { } else {
for _, del := range dels { for _, del := range dels {
if del.Deleted != "" { if del.Deleted != "" {

View file

@ -23,7 +23,7 @@ func (cli *DockerCli) CmdStop(args ...string) error {
var errs []string var errs []string
for _, name := range cmd.Args() { for _, name := range cmd.Args() {
if err := cli.client.ContainerStop(name, *nSeconds); err != nil { if err := cli.client.ContainerStop(name, *nSeconds); err != nil {
errs = append(errs, fmt.Sprintf("Failed to stop container (%s): %s", name, err)) errs = append(errs, err.Error())
} else { } else {
fmt.Fprintf(cli.out, "%s\n", name) fmt.Fprintf(cli.out, "%s\n", name)
} }

View file

@ -20,7 +20,7 @@ func (cli *DockerCli) CmdUnpause(args ...string) error {
var errs []string var errs []string
for _, name := range cmd.Args() { for _, name := range cmd.Args() {
if err := cli.client.ContainerUnpause(name); err != nil { if err := cli.client.ContainerUnpause(name); err != nil {
errs = append(errs, fmt.Sprintf("Failed to unpause container (%s): %s", name, err)) errs = append(errs, err.Error())
} else { } else {
fmt.Fprintf(cli.out, "%s\n", name) fmt.Fprintf(cli.out, "%s\n", name)
} }

View file

@ -90,7 +90,7 @@ func (cli *DockerCli) CmdUpdate(args ...string) error {
var errs []string var errs []string
for _, name := range names { for _, name := range names {
if err := cli.client.ContainerUpdate(name, updateConfig); err != nil { if err := cli.client.ContainerUpdate(name, updateConfig); err != nil {
errs = append(errs, fmt.Sprintf("Failed to update container (%s): %s", name, err)) errs = append(errs, err.Error())
} else { } else {
fmt.Fprintf(cli.out, "%s\n", name) fmt.Fprintf(cli.out, "%s\n", name)
} }

View file

@ -23,7 +23,7 @@ func (cli *DockerCli) CmdWait(args ...string) error {
for _, name := range cmd.Args() { for _, name := range cmd.Args() {
status, err := cli.client.ContainerWait(name) status, err := cli.client.ContainerWait(name)
if err != nil { if err != nil {
errs = append(errs, fmt.Sprintf("Failed to wait container (%s): %s", name, err)) errs = append(errs, err.Error())
} else { } else {
fmt.Fprintf(cli.out, "%d\n", status) fmt.Fprintf(cli.out, "%d\n", status)
} }

View file

@ -30,7 +30,7 @@ func (daemon *Daemon) ContainerRm(name string, config *types.ContainerRmConfig)
// do not fail when the removal is in progress started by other request. // do not fail when the removal is in progress started by other request.
return nil return nil
} }
return derr.ErrorCodeRmState.WithArgs(err) return derr.ErrorCodeRmState.WithArgs(container.ID, err)
} }
defer container.ResetRemovalInProgress() defer container.ResetRemovalInProgress()
@ -84,10 +84,10 @@ func (daemon *Daemon) rmLink(container *container.Container, name string) error
func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemove bool) (err error) { func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemove bool) (err error) {
if container.IsRunning() { if container.IsRunning() {
if !forceRemove { if !forceRemove {
return derr.ErrorCodeRmRunning return derr.ErrorCodeRmRunning.WithArgs(container.ID)
} }
if err := daemon.Kill(container); err != nil { if err := daemon.Kill(container); err != nil {
return derr.ErrorCodeRmFailed.WithArgs(err) return derr.ErrorCodeRmFailed.WithArgs(container.ID, err)
} }
} }

View file

@ -62,7 +62,7 @@ func (daemon *Daemon) killWithSignal(container *container.Container, sig int) er
} }
if err := daemon.kill(container, sig); err != nil { if err := daemon.kill(container, sig); err != nil {
return err return derr.ErrorCodeCantKill.WithArgs(container.ID, err)
} }
attributes := map[string]string{ attributes := map[string]string{

View file

@ -13,7 +13,7 @@ func (daemon *Daemon) ContainerPause(name string) error {
} }
if err := daemon.containerPause(container); err != nil { if err := daemon.containerPause(container); err != nil {
return derr.ErrorCodePauseError.WithArgs(name, err) return err
} }
return nil return nil
@ -36,7 +36,7 @@ func (daemon *Daemon) containerPause(container *container.Container) error {
} }
if err := daemon.execDriver.Pause(container.Command); err != nil { if err := daemon.execDriver.Pause(container.Command); err != nil {
return err return derr.ErrorCodeCantPause.WithArgs(container.ID, err)
} }
container.Paused = true container.Paused = true
daemon.LogContainerEvent(container, "pause") daemon.LogContainerEvent(container, "pause")

View file

@ -20,7 +20,7 @@ func (daemon *Daemon) ContainerStop(name string, seconds int) error {
return err return err
} }
if !container.IsRunning() { if !container.IsRunning() {
return derr.ErrorCodeStopped return derr.ErrorCodeStopped.WithArgs(name)
} }
if err := daemon.containerStop(container, seconds); err != nil { if err := daemon.containerStop(container, seconds); err != nil {
return derr.ErrorCodeCantStop.WithArgs(name, err) return derr.ErrorCodeCantStop.WithArgs(name, err)

View file

@ -13,7 +13,7 @@ func (daemon *Daemon) ContainerUnpause(name string) error {
} }
if err := daemon.containerUnpause(container); err != nil { if err := daemon.containerUnpause(container); err != nil {
return derr.ErrorCodeCantUnpause.WithArgs(name, err) return err
} }
return nil return nil
@ -35,7 +35,7 @@ func (daemon *Daemon) containerUnpause(container *container.Container) error {
} }
if err := daemon.execDriver.Unpause(container.Command); err != nil { if err := daemon.execDriver.Unpause(container.Command); err != nil {
return err return derr.ErrorCodeCantUnpause.WithArgs(container.ID, err)
} }
container.Paused = false container.Paused = false

View file

@ -3,6 +3,7 @@ package daemon
import ( import (
"fmt" "fmt"
derr "github.com/docker/docker/errors"
"github.com/docker/engine-api/types/container" "github.com/docker/engine-api/types/container"
) )
@ -44,15 +45,17 @@ func (daemon *Daemon) update(name string, hostConfig *container.HostConfig) erro
} }
if container.RemovalInProgress || container.Dead { if container.RemovalInProgress || container.Dead {
return fmt.Errorf("Container is marked for removal and cannot be \"update\".") errMsg := fmt.Errorf("Container is marked for removal and cannot be \"update\".")
return derr.ErrorCodeCantUpdate.WithArgs(container.ID, errMsg)
} }
if container.IsRunning() && hostConfig.KernelMemory != 0 { if container.IsRunning() && hostConfig.KernelMemory != 0 {
return fmt.Errorf("Can not update kernel memory to a running container, please stop it first.") errMsg := fmt.Errorf("Can not update kernel memory to a running container, please stop it first.")
return derr.ErrorCodeCantUpdate.WithArgs(container.ID, errMsg)
} }
if err := container.UpdateContainer(hostConfig); err != nil { if err := container.UpdateContainer(hostConfig); err != nil {
return err return derr.ErrorCodeCantUpdate.WithArgs(container.ID, err.Error())
} }
// If container is not running, update hostConfig struct is enough, // If container is not running, update hostConfig struct is enough,
@ -61,7 +64,7 @@ func (daemon *Daemon) update(name string, hostConfig *container.HostConfig) erro
// to the real world. // to the real world.
if container.IsRunning() { if container.IsRunning() {
if err := daemon.execDriver.Update(container.Command); err != nil { if err := daemon.execDriver.Update(container.Command); err != nil {
return err return derr.ErrorCodeCantUpdate.WithArgs(container.ID, err.Error())
} }
} }

View file

@ -478,6 +478,15 @@ var (
HTTPStatusCode: http.StatusInternalServerError, HTTPStatusCode: http.StatusInternalServerError,
}) })
// ErrorCodeCantPause is generated when there's an error while trying
// to pause a container.
ErrorCodeCantPause = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "CANTPAUSE",
Message: "Cannot pause container %s: %s",
Description: "An error occurred while trying to pause the specified container",
HTTPStatusCode: http.StatusInternalServerError,
})
// ErrorCodeCantUnpause is generated when there's an error while trying // ErrorCodeCantUnpause is generated when there's an error while trying
// to unpause a container. // to unpause a container.
ErrorCodeCantUnpause = errcode.Register(errGroup, errcode.ErrorDescriptor{ ErrorCodeCantUnpause = errcode.Register(errGroup, errcode.ErrorDescriptor{
@ -487,6 +496,23 @@ var (
HTTPStatusCode: http.StatusInternalServerError, HTTPStatusCode: http.StatusInternalServerError,
}) })
// ErrorCodeCantKill is generated when there's an error while trying
// to kill a container.
ErrorCodeCantKill = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "CANTKILL",
Message: "Cannot kill container %s: %s",
Description: "An error occurred while trying to kill the specified container",
HTTPStatusCode: http.StatusInternalServerError,
})
// ErrorCodeCantUpdate is generated when there's an error while trying
// to update a container.
ErrorCodeCantUpdate = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "CANTUPDATE",
Message: "Cannot update container %s: %s",
Description: "An error occurred while trying to update the specified container",
HTTPStatusCode: http.StatusInternalServerError,
})
// ErrorCodePSError is generated when trying to run 'ps'. // ErrorCodePSError is generated when trying to run 'ps'.
ErrorCodePSError = errcode.Register(errGroup, errcode.ErrorDescriptor{ ErrorCodePSError = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "PSError", Value: "PSError",
@ -525,7 +551,7 @@ var (
// that is already stopped. // that is already stopped.
ErrorCodeStopped = errcode.Register(errGroup, errcode.ErrorDescriptor{ ErrorCodeStopped = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "STOPPED", Value: "STOPPED",
Message: "Container already stopped", Message: "Container %s is already stopped",
Description: "An attempt was made to stop a container, but the container is already stopped", Description: "An attempt was made to stop a container, but the container is already stopped",
HTTPStatusCode: http.StatusNotModified, HTTPStatusCode: http.StatusNotModified,
}) })
@ -818,7 +844,7 @@ var (
// but its still running. // but its still running.
ErrorCodeRmRunning = errcode.Register(errGroup, errcode.ErrorDescriptor{ ErrorCodeRmRunning = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "RMRUNNING", Value: "RMRUNNING",
Message: "Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f", Message: "You cannot remove a running container %s. Stop the container before attempting removal or use -f",
Description: "An attempt was made to delete a container but the container is still running, try to either stop it first or use '-f'", Description: "An attempt was made to delete a container but the container is still running, try to either stop it first or use '-f'",
HTTPStatusCode: http.StatusConflict, HTTPStatusCode: http.StatusConflict,
}) })
@ -827,7 +853,7 @@ var (
// but it failed for some reason. // but it failed for some reason.
ErrorCodeRmFailed = errcode.Register(errGroup, errcode.ErrorDescriptor{ ErrorCodeRmFailed = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "RMFAILED", Value: "RMFAILED",
Message: "Could not kill running container, cannot remove - %v", Message: "Could not kill running container %s, cannot remove - %v",
Description: "An error occurred while trying to delete a running container", Description: "An error occurred while trying to delete a running container",
HTTPStatusCode: http.StatusInternalServerError, HTTPStatusCode: http.StatusInternalServerError,
}) })
@ -845,7 +871,7 @@ var (
// but couldn't set its state to RemovalInProgress. // but couldn't set its state to RemovalInProgress.
ErrorCodeRmState = errcode.Register(errGroup, errcode.ErrorDescriptor{ ErrorCodeRmState = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "RMSTATE", Value: "RMSTATE",
Message: "Failed to set container state to RemovalInProgress: %s", Message: "Failed to set container %s state to RemovalInProgress: %s",
Description: "An attempt to delete a container was made, but there as an error trying to set its state to 'RemovalInProgress'", Description: "An attempt to delete a container was made, but there as an error trying to set its state to 'RemovalInProgress'",
HTTPStatusCode: http.StatusInternalServerError, HTTPStatusCode: http.StatusInternalServerError,
}) })

View file

@ -3,7 +3,6 @@ package main
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"github.com/docker/docker/pkg/integration/checker" "github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check" "github.com/go-check/check"
@ -77,11 +76,9 @@ func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
} }
func (s *DockerSuite) TestRmInvalidContainer(c *check.C) { func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
if out, _, err := dockerCmdWithError("rm", "unknown"); err == nil { out, _, err := dockerCmdWithError("rm", "unknown")
c.Fatal("Expected error on rm unknown container, got none") c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm unknown container, got none"))
} else if !strings.Contains(out, "Failed to remove container") { c.Assert(out, checker.Contains, "No such container")
c.Fatalf("Expected output to contain 'Failed to remove container', got %q", out)
}
} }
func createRunningContainer(c *check.C, name string) { func createRunningContainer(c *check.C, name string) {