Currently some commands including `kill`, `pause`, `restart`, `rm`,
`rmi`, `stop`, `unpause`, `udpate`, `wait` will print a lot of error
message on client side, with a lot of redundant messages, this commit is
trying to remove the unuseful and redundant information for user.
Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
| ... | ... |
@@ -21,7 +21,7 @@ func (cli *DockerCli) CmdKill(args ...string) error {
|
| 21 | 21 |
var errs []string |
| 22 | 22 |
for _, name := range cmd.Args() {
|
| 23 | 23 |
if err := cli.client.ContainerKill(name, *signal); err != nil {
|
| 24 |
- errs = append(errs, fmt.Sprintf("Failed to kill container (%s): %s", name, err))
|
|
| 24 |
+ errs = append(errs, err.Error()) |
|
| 25 | 25 |
} else {
|
| 26 | 26 |
fmt.Fprintf(cli.out, "%s\n", name) |
| 27 | 27 |
} |
| ... | ... |
@@ -20,7 +20,7 @@ func (cli *DockerCli) CmdPause(args ...string) error {
|
| 20 | 20 |
var errs []string |
| 21 | 21 |
for _, name := range cmd.Args() {
|
| 22 | 22 |
if err := cli.client.ContainerPause(name); err != nil {
|
| 23 |
- errs = append(errs, fmt.Sprintf("Failed to pause container (%s): %s", name, err))
|
|
| 23 |
+ errs = append(errs, err.Error()) |
|
| 24 | 24 |
} else {
|
| 25 | 25 |
fmt.Fprintf(cli.out, "%s\n", name) |
| 26 | 26 |
} |
| ... | ... |
@@ -21,7 +21,7 @@ func (cli *DockerCli) CmdRestart(args ...string) error {
|
| 21 | 21 |
var errs []string |
| 22 | 22 |
for _, name := range cmd.Args() {
|
| 23 | 23 |
if err := cli.client.ContainerRestart(name, *nSeconds); err != nil {
|
| 24 |
- errs = append(errs, fmt.Sprintf("Failed to kill container (%s): %s", name, err))
|
|
| 24 |
+ errs = append(errs, err.Error()) |
|
| 25 | 25 |
} else {
|
| 26 | 26 |
fmt.Fprintf(cli.out, "%s\n", name) |
| 27 | 27 |
} |
| ... | ... |
@@ -48,7 +48,7 @@ func (cli *DockerCli) removeContainer(containerID string, removeVolumes, removeL |
| 48 | 48 |
Force: force, |
| 49 | 49 |
} |
| 50 | 50 |
if err := cli.client.ContainerRemove(options); err != nil {
|
| 51 |
- return fmt.Errorf("Failed to remove container (%s): %v", containerID, err)
|
|
| 51 |
+ return err |
|
| 52 | 52 |
} |
| 53 | 53 |
return nil |
| 54 | 54 |
} |
| ... | ... |
@@ -39,7 +39,7 @@ func (cli *DockerCli) CmdRmi(args ...string) error {
|
| 39 | 39 |
|
| 40 | 40 |
dels, err := cli.client.ImageRemove(options) |
| 41 | 41 |
if err != nil {
|
| 42 |
- errs = append(errs, fmt.Sprintf("Failed to remove image (%s): %s", name, err))
|
|
| 42 |
+ errs = append(errs, err.Error()) |
|
| 43 | 43 |
} else {
|
| 44 | 44 |
for _, del := range dels {
|
| 45 | 45 |
if del.Deleted != "" {
|
| ... | ... |
@@ -23,7 +23,7 @@ func (cli *DockerCli) CmdStop(args ...string) error {
|
| 23 | 23 |
var errs []string |
| 24 | 24 |
for _, name := range cmd.Args() {
|
| 25 | 25 |
if err := cli.client.ContainerStop(name, *nSeconds); err != nil {
|
| 26 |
- errs = append(errs, fmt.Sprintf("Failed to stop container (%s): %s", name, err))
|
|
| 26 |
+ errs = append(errs, err.Error()) |
|
| 27 | 27 |
} else {
|
| 28 | 28 |
fmt.Fprintf(cli.out, "%s\n", name) |
| 29 | 29 |
} |
| ... | ... |
@@ -20,7 +20,7 @@ func (cli *DockerCli) CmdUnpause(args ...string) error {
|
| 20 | 20 |
var errs []string |
| 21 | 21 |
for _, name := range cmd.Args() {
|
| 22 | 22 |
if err := cli.client.ContainerUnpause(name); err != nil {
|
| 23 |
- errs = append(errs, fmt.Sprintf("Failed to unpause container (%s): %s", name, err))
|
|
| 23 |
+ errs = append(errs, err.Error()) |
|
| 24 | 24 |
} else {
|
| 25 | 25 |
fmt.Fprintf(cli.out, "%s\n", name) |
| 26 | 26 |
} |
| ... | ... |
@@ -90,7 +90,7 @@ func (cli *DockerCli) CmdUpdate(args ...string) error {
|
| 90 | 90 |
var errs []string |
| 91 | 91 |
for _, name := range names {
|
| 92 | 92 |
if err := cli.client.ContainerUpdate(name, updateConfig); err != nil {
|
| 93 |
- errs = append(errs, fmt.Sprintf("Failed to update container (%s): %s", name, err))
|
|
| 93 |
+ errs = append(errs, err.Error()) |
|
| 94 | 94 |
} else {
|
| 95 | 95 |
fmt.Fprintf(cli.out, "%s\n", name) |
| 96 | 96 |
} |
| ... | ... |
@@ -23,7 +23,7 @@ func (cli *DockerCli) CmdWait(args ...string) error {
|
| 23 | 23 |
for _, name := range cmd.Args() {
|
| 24 | 24 |
status, err := cli.client.ContainerWait(name) |
| 25 | 25 |
if err != nil {
|
| 26 |
- errs = append(errs, fmt.Sprintf("Failed to wait container (%s): %s", name, err))
|
|
| 26 |
+ errs = append(errs, err.Error()) |
|
| 27 | 27 |
} else {
|
| 28 | 28 |
fmt.Fprintf(cli.out, "%d\n", status) |
| 29 | 29 |
} |
| ... | ... |
@@ -30,7 +30,7 @@ func (daemon *Daemon) ContainerRm(name string, config *types.ContainerRmConfig) |
| 30 | 30 |
// do not fail when the removal is in progress started by other request. |
| 31 | 31 |
return nil |
| 32 | 32 |
} |
| 33 |
- return derr.ErrorCodeRmState.WithArgs(err) |
|
| 33 |
+ return derr.ErrorCodeRmState.WithArgs(container.ID, err) |
|
| 34 | 34 |
} |
| 35 | 35 |
defer container.ResetRemovalInProgress() |
| 36 | 36 |
|
| ... | ... |
@@ -84,10 +84,10 @@ func (daemon *Daemon) rmLink(container *container.Container, name string) error |
| 84 | 84 |
func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemove bool) (err error) {
|
| 85 | 85 |
if container.IsRunning() {
|
| 86 | 86 |
if !forceRemove {
|
| 87 |
- return derr.ErrorCodeRmRunning |
|
| 87 |
+ return derr.ErrorCodeRmRunning.WithArgs(container.ID) |
|
| 88 | 88 |
} |
| 89 | 89 |
if err := daemon.Kill(container); err != nil {
|
| 90 |
- return derr.ErrorCodeRmFailed.WithArgs(err) |
|
| 90 |
+ return derr.ErrorCodeRmFailed.WithArgs(container.ID, err) |
|
| 91 | 91 |
} |
| 92 | 92 |
} |
| 93 | 93 |
|
| ... | ... |
@@ -62,7 +62,7 @@ func (daemon *Daemon) killWithSignal(container *container.Container, sig int) er |
| 62 | 62 |
} |
| 63 | 63 |
|
| 64 | 64 |
if err := daemon.kill(container, sig); err != nil {
|
| 65 |
- return err |
|
| 65 |
+ return derr.ErrorCodeCantKill.WithArgs(container.ID, err) |
|
| 66 | 66 |
} |
| 67 | 67 |
|
| 68 | 68 |
attributes := map[string]string{
|
| ... | ... |
@@ -13,7 +13,7 @@ func (daemon *Daemon) ContainerPause(name string) error {
|
| 13 | 13 |
} |
| 14 | 14 |
|
| 15 | 15 |
if err := daemon.containerPause(container); err != nil {
|
| 16 |
- return derr.ErrorCodePauseError.WithArgs(name, err) |
|
| 16 |
+ return err |
|
| 17 | 17 |
} |
| 18 | 18 |
|
| 19 | 19 |
return nil |
| ... | ... |
@@ -36,7 +36,7 @@ func (daemon *Daemon) containerPause(container *container.Container) error {
|
| 36 | 36 |
} |
| 37 | 37 |
|
| 38 | 38 |
if err := daemon.execDriver.Pause(container.Command); err != nil {
|
| 39 |
- return err |
|
| 39 |
+ return derr.ErrorCodeCantPause.WithArgs(container.ID, err) |
|
| 40 | 40 |
} |
| 41 | 41 |
container.Paused = true |
| 42 | 42 |
daemon.LogContainerEvent(container, "pause") |
| ... | ... |
@@ -20,7 +20,7 @@ func (daemon *Daemon) ContainerStop(name string, seconds int) error {
|
| 20 | 20 |
return err |
| 21 | 21 |
} |
| 22 | 22 |
if !container.IsRunning() {
|
| 23 |
- return derr.ErrorCodeStopped |
|
| 23 |
+ return derr.ErrorCodeStopped.WithArgs(name) |
|
| 24 | 24 |
} |
| 25 | 25 |
if err := daemon.containerStop(container, seconds); err != nil {
|
| 26 | 26 |
return derr.ErrorCodeCantStop.WithArgs(name, err) |
| ... | ... |
@@ -13,7 +13,7 @@ func (daemon *Daemon) ContainerUnpause(name string) error {
|
| 13 | 13 |
} |
| 14 | 14 |
|
| 15 | 15 |
if err := daemon.containerUnpause(container); err != nil {
|
| 16 |
- return derr.ErrorCodeCantUnpause.WithArgs(name, err) |
|
| 16 |
+ return err |
|
| 17 | 17 |
} |
| 18 | 18 |
|
| 19 | 19 |
return nil |
| ... | ... |
@@ -35,7 +35,7 @@ func (daemon *Daemon) containerUnpause(container *container.Container) error {
|
| 35 | 35 |
} |
| 36 | 36 |
|
| 37 | 37 |
if err := daemon.execDriver.Unpause(container.Command); err != nil {
|
| 38 |
- return err |
|
| 38 |
+ return derr.ErrorCodeCantUnpause.WithArgs(container.ID, err) |
|
| 39 | 39 |
} |
| 40 | 40 |
|
| 41 | 41 |
container.Paused = false |
| ... | ... |
@@ -3,6 +3,7 @@ package daemon |
| 3 | 3 |
import ( |
| 4 | 4 |
"fmt" |
| 5 | 5 |
|
| 6 |
+ derr "github.com/docker/docker/errors" |
|
| 6 | 7 |
"github.com/docker/engine-api/types/container" |
| 7 | 8 |
) |
| 8 | 9 |
|
| ... | ... |
@@ -44,15 +45,17 @@ func (daemon *Daemon) update(name string, hostConfig *container.HostConfig) erro |
| 44 | 44 |
} |
| 45 | 45 |
|
| 46 | 46 |
if container.RemovalInProgress || container.Dead {
|
| 47 |
- return fmt.Errorf("Container is marked for removal and cannot be \"update\".")
|
|
| 47 |
+ errMsg := fmt.Errorf("Container is marked for removal and cannot be \"update\".")
|
|
| 48 |
+ return derr.ErrorCodeCantUpdate.WithArgs(container.ID, errMsg) |
|
| 48 | 49 |
} |
| 49 | 50 |
|
| 50 | 51 |
if container.IsRunning() && hostConfig.KernelMemory != 0 {
|
| 51 |
- return fmt.Errorf("Can not update kernel memory to a running container, please stop it first.")
|
|
| 52 |
+ errMsg := fmt.Errorf("Can not update kernel memory to a running container, please stop it first.")
|
|
| 53 |
+ return derr.ErrorCodeCantUpdate.WithArgs(container.ID, errMsg) |
|
| 52 | 54 |
} |
| 53 | 55 |
|
| 54 | 56 |
if err := container.UpdateContainer(hostConfig); err != nil {
|
| 55 |
- return err |
|
| 57 |
+ return derr.ErrorCodeCantUpdate.WithArgs(container.ID, err.Error()) |
|
| 56 | 58 |
} |
| 57 | 59 |
|
| 58 | 60 |
// If container is not running, update hostConfig struct is enough, |
| ... | ... |
@@ -61,7 +64,7 @@ func (daemon *Daemon) update(name string, hostConfig *container.HostConfig) erro |
| 61 | 61 |
// to the real world. |
| 62 | 62 |
if container.IsRunning() {
|
| 63 | 63 |
if err := daemon.execDriver.Update(container.Command); err != nil {
|
| 64 |
- return err |
|
| 64 |
+ return derr.ErrorCodeCantUpdate.WithArgs(container.ID, err.Error()) |
|
| 65 | 65 |
} |
| 66 | 66 |
} |
| 67 | 67 |
|
| ... | ... |
@@ -478,6 +478,15 @@ var ( |
| 478 | 478 |
HTTPStatusCode: http.StatusInternalServerError, |
| 479 | 479 |
}) |
| 480 | 480 |
|
| 481 |
+ // ErrorCodeCantPause is generated when there's an error while trying |
|
| 482 |
+ // to pause a container. |
|
| 483 |
+ ErrorCodeCantPause = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
|
| 484 |
+ Value: "CANTPAUSE", |
|
| 485 |
+ Message: "Cannot pause container %s: %s", |
|
| 486 |
+ Description: "An error occurred while trying to pause the specified container", |
|
| 487 |
+ HTTPStatusCode: http.StatusInternalServerError, |
|
| 488 |
+ }) |
|
| 489 |
+ |
|
| 481 | 490 |
// ErrorCodeCantUnpause is generated when there's an error while trying |
| 482 | 491 |
// to unpause a container. |
| 483 | 492 |
ErrorCodeCantUnpause = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
| ... | ... |
@@ -487,6 +496,23 @@ var ( |
| 487 | 487 |
HTTPStatusCode: http.StatusInternalServerError, |
| 488 | 488 |
}) |
| 489 | 489 |
|
| 490 |
+ // ErrorCodeCantKill is generated when there's an error while trying |
|
| 491 |
+ // to kill a container. |
|
| 492 |
+ ErrorCodeCantKill = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
|
| 493 |
+ Value: "CANTKILL", |
|
| 494 |
+ Message: "Cannot kill container %s: %s", |
|
| 495 |
+ Description: "An error occurred while trying to kill the specified container", |
|
| 496 |
+ HTTPStatusCode: http.StatusInternalServerError, |
|
| 497 |
+ }) |
|
| 498 |
+ |
|
| 499 |
+ // ErrorCodeCantUpdate is generated when there's an error while trying |
|
| 500 |
+ // to update a container. |
|
| 501 |
+ ErrorCodeCantUpdate = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
|
| 502 |
+ Value: "CANTUPDATE", |
|
| 503 |
+ Message: "Cannot update container %s: %s", |
|
| 504 |
+ Description: "An error occurred while trying to update the specified container", |
|
| 505 |
+ HTTPStatusCode: http.StatusInternalServerError, |
|
| 506 |
+ }) |
|
| 490 | 507 |
// ErrorCodePSError is generated when trying to run 'ps'. |
| 491 | 508 |
ErrorCodePSError = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
| 492 | 509 |
Value: "PSError", |
| ... | ... |
@@ -525,7 +551,7 @@ var ( |
| 525 | 525 |
// that is already stopped. |
| 526 | 526 |
ErrorCodeStopped = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
| 527 | 527 |
Value: "STOPPED", |
| 528 |
- Message: "Container already stopped", |
|
| 528 |
+ Message: "Container %s is already stopped", |
|
| 529 | 529 |
Description: "An attempt was made to stop a container, but the container is already stopped", |
| 530 | 530 |
HTTPStatusCode: http.StatusNotModified, |
| 531 | 531 |
}) |
| ... | ... |
@@ -818,7 +844,7 @@ var ( |
| 818 | 818 |
// but its still running. |
| 819 | 819 |
ErrorCodeRmRunning = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
| 820 | 820 |
Value: "RMRUNNING", |
| 821 |
- Message: "Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f", |
|
| 821 |
+ Message: "You cannot remove a running container %s. Stop the container before attempting removal or use -f", |
|
| 822 | 822 |
Description: "An attempt was made to delete a container but the container is still running, try to either stop it first or use '-f'", |
| 823 | 823 |
HTTPStatusCode: http.StatusConflict, |
| 824 | 824 |
}) |
| ... | ... |
@@ -827,7 +853,7 @@ var ( |
| 827 | 827 |
// but it failed for some reason. |
| 828 | 828 |
ErrorCodeRmFailed = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
| 829 | 829 |
Value: "RMFAILED", |
| 830 |
- Message: "Could not kill running container, cannot remove - %v", |
|
| 830 |
+ Message: "Could not kill running container %s, cannot remove - %v", |
|
| 831 | 831 |
Description: "An error occurred while trying to delete a running container", |
| 832 | 832 |
HTTPStatusCode: http.StatusInternalServerError, |
| 833 | 833 |
}) |
| ... | ... |
@@ -845,7 +871,7 @@ var ( |
| 845 | 845 |
// but couldn't set its state to RemovalInProgress. |
| 846 | 846 |
ErrorCodeRmState = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
| 847 | 847 |
Value: "RMSTATE", |
| 848 |
- Message: "Failed to set container state to RemovalInProgress: %s", |
|
| 848 |
+ Message: "Failed to set container %s state to RemovalInProgress: %s", |
|
| 849 | 849 |
Description: "An attempt to delete a container was made, but there as an error trying to set its state to 'RemovalInProgress'", |
| 850 | 850 |
HTTPStatusCode: http.StatusInternalServerError, |
| 851 | 851 |
}) |
| ... | ... |
@@ -2,7 +2,6 @@ package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"os" |
| 5 |
- "strings" |
|
| 6 | 5 |
|
| 7 | 6 |
"github.com/docker/docker/pkg/integration/checker" |
| 8 | 7 |
"github.com/go-check/check" |
| ... | ... |
@@ -71,11 +70,9 @@ func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
|
| 71 | 71 |
} |
| 72 | 72 |
|
| 73 | 73 |
func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
|
| 74 |
- if out, _, err := dockerCmdWithError("rm", "unknown"); err == nil {
|
|
| 75 |
- c.Fatal("Expected error on rm unknown container, got none")
|
|
| 76 |
- } else if !strings.Contains(out, "Failed to remove container") {
|
|
| 77 |
- c.Fatalf("Expected output to contain 'Failed to remove container', got %q", out)
|
|
| 78 |
- } |
|
| 74 |
+ out, _, err := dockerCmdWithError("rm", "unknown")
|
|
| 75 |
+ c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm unknown container, got none"))
|
|
| 76 |
+ c.Assert(out, checker.Contains, "No such container") |
|
| 79 | 77 |
} |
| 80 | 78 |
|
| 81 | 79 |
func createRunningContainer(c *check.C, name string) {
|