Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a73418

Browse files
author
Doug Davis
committed
Move more 'daemon' errors to the new error package
Signed-off-by: Doug Davis <[email protected]>
1 parent b0dc111 commit 0a73418

12 files changed

Lines changed: 395 additions & 47 deletions

File tree

api/server/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/docker/docker/context"
1919
"github.com/docker/docker/daemon"
2020
"github.com/docker/docker/pkg/sockets"
21+
"github.com/docker/docker/utils"
2122
)
2223

2324
// Config provides the configuration for the API server
@@ -248,7 +249,7 @@ func httpError(w http.ResponseWriter, err error) {
248249
statusCode = http.StatusInternalServerError
249250
}
250251

251-
logrus.WithFields(logrus.Fields{"statusCode": statusCode, "err": err}).Error("HTTP Error")
252+
logrus.WithFields(logrus.Fields{"statusCode": statusCode, "err": utils.GetErrorMessage(err)}).Error("HTTP Error")
252253
http.Error(w, errMsg, statusCode)
253254
}
254255

@@ -305,7 +306,7 @@ func (s *Server) makeHTTPHandler(localMethod string, localRoute string, localHan
305306
handlerFunc := s.handleWithGlobalMiddlewares(localHandler)
306307

307308
if err := handlerFunc(ctx, w, r, mux.Vars(r)); err != nil {
308-
logrus.Errorf("Handler for %s %s returned error: %s", localMethod, localRoute, err)
309+
logrus.Errorf("Handler for %s %s returned error: %s", localMethod, localRoute, utils.GetErrorMessage(err))
309310
httpError(w, err)
310311
}
311312
}

daemon/container_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ func (container *Container) setNetworkNamespaceKey(pid int) error {
973973
search := libnetwork.SandboxContainerWalker(&sandbox, container.ID)
974974
container.daemon.netController.WalkSandboxes(search)
975975
if sandbox == nil {
976-
return fmt.Errorf("no sandbox present for %s", container.ID)
976+
return derr.ErrorCodeNoSandbox.WithArgs(container.ID)
977977
}
978978

979979
return sandbox.SetKey(path)

daemon/delete.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path"
77

88
"github.com/Sirupsen/logrus"
9+
derr "github.com/docker/docker/errors"
910
"github.com/docker/docker/volume/store"
1011
)
1112

@@ -31,11 +32,11 @@ func (daemon *Daemon) ContainerRm(name string, config *ContainerRmConfig) error
3132
}
3233
parent, n := path.Split(name)
3334
if parent == "/" {
34-
return fmt.Errorf("Conflict, cannot remove the default name of the container")
35+
return derr.ErrorCodeDefaultName
3536
}
3637
pe := daemon.containerGraph().Get(parent)
3738
if pe == nil {
38-
return fmt.Errorf("Cannot get parent %s for name %s", parent, name)
39+
return derr.ErrorCodeNoParent.WithArgs(parent, name)
3940
}
4041

4142
if err := daemon.containerGraph().Delete(name); err != nil {
@@ -53,7 +54,8 @@ func (daemon *Daemon) ContainerRm(name string, config *ContainerRmConfig) error
5354
}
5455

5556
if err := daemon.rm(container, config.ForceRemove); err != nil {
56-
return fmt.Errorf("Cannot destroy container %s: %v", name, err)
57+
// return derr.ErrorCodeCantDestroy.WithArgs(name, utils.GetErrorMessage(err))
58+
return err
5759
}
5860

5961
if err := container.removeMountPoints(config.RemoveVolume); err != nil {
@@ -67,10 +69,10 @@ func (daemon *Daemon) ContainerRm(name string, config *ContainerRmConfig) error
6769
func (daemon *Daemon) rm(container *Container, forceRemove bool) (err error) {
6870
if container.IsRunning() {
6971
if !forceRemove {
70-
return fmt.Errorf("Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f")
72+
return derr.ErrorCodeRmRunning
7173
}
7274
if err := container.Kill(); err != nil {
73-
return fmt.Errorf("Could not kill running container, cannot remove - %v", err)
75+
return derr.ErrorCodeRmFailed.WithArgs(err)
7476
}
7577
}
7678

@@ -80,12 +82,12 @@ func (daemon *Daemon) rm(container *Container, forceRemove bool) (err error) {
8082

8183
element := daemon.containers.Get(container.ID)
8284
if element == nil {
83-
return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.ID)
85+
return derr.ErrorCodeRmNotFound.WithArgs(container.ID)
8486
}
8587

8688
// Container state RemovalInProgress should be used to avoid races.
8789
if err = container.setRemovalInProgress(); err != nil {
88-
return fmt.Errorf("Failed to set container state to RemovalInProgress: %s", err)
90+
return derr.ErrorCodeRmState.WithArgs(err)
8991
}
9092

9193
defer container.resetRemovalInProgress()
@@ -120,20 +122,20 @@ func (daemon *Daemon) rm(container *Container, forceRemove bool) (err error) {
120122
}
121123

122124
if err = daemon.driver.Remove(container.ID); err != nil {
123-
return fmt.Errorf("Driver %s failed to remove root filesystem %s: %s", daemon.driver, container.ID, err)
125+
return derr.ErrorCodeRmDriverFS.WithArgs(daemon.driver, container.ID, err)
124126
}
125127

126128
initID := fmt.Sprintf("%s-init", container.ID)
127129
if err := daemon.driver.Remove(initID); err != nil {
128-
return fmt.Errorf("Driver %s failed to remove init filesystem %s: %s", daemon.driver, initID, err)
130+
return derr.ErrorCodeRmInit.WithArgs(daemon.driver, initID, err)
129131
}
130132

131133
if err = os.RemoveAll(container.root); err != nil {
132-
return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
134+
return derr.ErrorCodeRmFS.WithArgs(container.ID, err)
133135
}
134136

135137
if err = daemon.execDriver.Clean(container.ID); err != nil {
136-
return fmt.Errorf("Unable to remove execdriver data for %s: %s", container.ID, err)
138+
return derr.ErrorCodeRmExecDriver.WithArgs(container.ID, err)
137139
}
138140

139141
selinuxFreeLxcContexts(container.ProcessLabel)
@@ -154,9 +156,9 @@ func (daemon *Daemon) VolumeRm(name string) error {
154156
}
155157
if err := daemon.volumes.Remove(v); err != nil {
156158
if err == store.ErrVolumeInUse {
157-
return fmt.Errorf("Conflict: %v", err)
159+
return derr.ErrorCodeRmVolumeInUse.WithArgs(err)
158160
}
159-
return fmt.Errorf("Error while removing volume %s: %v", name, err)
161+
return derr.ErrorCodeRmVolume.WithArgs(name, err)
160162
}
161163
return nil
162164
}

daemon/exec.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package daemon
22

33
import (
4-
"fmt"
54
"io"
65
"io/ioutil"
76
"strings"
@@ -10,6 +9,7 @@ import (
109

1110
"github.com/Sirupsen/logrus"
1211
"github.com/docker/docker/daemon/execdriver"
12+
derr "github.com/docker/docker/errors"
1313
"github.com/docker/docker/pkg/broadcastwriter"
1414
"github.com/docker/docker/pkg/ioutils"
1515
"github.com/docker/docker/pkg/pools"
@@ -80,7 +80,7 @@ func (ExecConfig *ExecConfig) resize(h, w int) error {
8080
select {
8181
case <-ExecConfig.waitStart:
8282
case <-time.After(time.Second):
83-
return fmt.Errorf("Exec %s is not running, so it can not be resized.", ExecConfig.ID)
83+
return derr.ErrorCodeExecResize.WithArgs(ExecConfig.ID)
8484
}
8585
return ExecConfig.ProcessConfig.Terminal.Resize(h, w)
8686
}
@@ -104,12 +104,12 @@ func (d *Daemon) getExecConfig(name string) (*ExecConfig, error) {
104104
if ExecConfig != nil && d.containers.Get(ExecConfig.Container.ID) != nil {
105105

106106
if !ExecConfig.Container.IsRunning() {
107-
return nil, fmt.Errorf("Container %s is not running", ExecConfig.Container.ID)
107+
return nil, derr.ErrorCodeContainerNotRunning.WithArgs(ExecConfig.Container.ID)
108108
}
109109
return ExecConfig, nil
110110
}
111111

112-
return nil, fmt.Errorf("No such exec instance '%s' found in daemon", name)
112+
return nil, derr.ErrorCodeNoExecID.WithArgs(name)
113113
}
114114

115115
func (d *Daemon) unregisterExecCommand(ExecConfig *ExecConfig) {
@@ -124,10 +124,10 @@ func (d *Daemon) getActiveContainer(name string) (*Container, error) {
124124
}
125125

126126
if !container.IsRunning() {
127-
return nil, fmt.Errorf("Container %s is not running", name)
127+
return nil, derr.ErrorCodeNotRunning.WithArgs(name)
128128
}
129129
if container.isPaused() {
130-
return nil, fmt.Errorf("Container %s is paused, unpause the container before exec", name)
130+
return nil, derr.ErrorCodeExecPaused.WithArgs(name)
131131
}
132132
return container, nil
133133
}
@@ -196,7 +196,7 @@ func (d *Daemon) ContainerExecStart(execName string, stdin io.ReadCloser, stdout
196196
ExecConfig.Lock()
197197
defer ExecConfig.Unlock()
198198
if ExecConfig.Running {
199-
err = fmt.Errorf("Error: Exec command %s is already running", execName)
199+
err = derr.ErrorCodeExecRunning.WithArgs(execName)
200200
}
201201
ExecConfig.Running = true
202202
}()
@@ -244,13 +244,13 @@ func (d *Daemon) ContainerExecStart(execName string, stdin io.ReadCloser, stdout
244244

245245
go func() {
246246
if err := container.exec(ExecConfig); err != nil {
247-
execErr <- fmt.Errorf("Cannot run exec command %s in container %s: %s", execName, container.ID, err)
247+
execErr <- derr.ErrorCodeExecCantRun.WithArgs(execName, container.ID, err)
248248
}
249249
}()
250250
select {
251251
case err := <-attachErr:
252252
if err != nil {
253-
return fmt.Errorf("attach failed with error: %s", err)
253+
return derr.ErrorCodeExecAttach.WithArgs(err)
254254
}
255255
return nil
256256
case err := <-execErr:
@@ -260,7 +260,7 @@ func (d *Daemon) ContainerExecStart(execName string, stdin io.ReadCloser, stdout
260260

261261
// Maybe the container stopped while we were trying to exec
262262
if !container.IsRunning() {
263-
return fmt.Errorf("container stopped while running exec")
263+
return derr.ErrorCodeExecContainerStopped
264264
}
265265
return err
266266
}

daemon/export.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package daemon
22

33
import (
4-
"fmt"
54
"io"
5+
6+
derr "github.com/docker/docker/errors"
67
)
78

89
// ContainerExport writes the contents of the container to the given
@@ -15,13 +16,13 @@ func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
1516

1617
data, err := container.export()
1718
if err != nil {
18-
return fmt.Errorf("%s: %s", name, err)
19+
return derr.ErrorCodeExportFailed.WithArgs(name, err)
1920
}
2021
defer data.Close()
2122

2223
// Stream the entire contents of the container (basically a volatile snapshot)
2324
if _, err := io.Copy(out, data); err != nil {
24-
return fmt.Errorf("%s: %s", name, err)
25+
return derr.ErrorCodeExportFailed.WithArgs(name, err)
2526
}
2627
return nil
2728
}

daemon/image_delete.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/docker/docker/api/types"
8+
derr "github.com/docker/docker/errors"
89
"github.com/docker/docker/graph/tags"
910
"github.com/docker/docker/image"
1011
"github.com/docker/docker/pkg/parsers"
@@ -69,7 +70,7 @@ func (daemon *Daemon) ImageDelete(imageRef string, force, prune bool) ([]types.I
6970
// this image would remain "dangling" and since
7071
// we really want to avoid that the client must
7172
// explicitly force its removal.
72-
return nil, fmt.Errorf("conflict: unable to remove repository reference %q (must force) - container %s is using its referenced image %s", imageRef, stringid.TruncateID(container.ID), stringid.TruncateID(img.ID))
73+
return nil, derr.ErrorCodeImgDelUsed.WithArgs(imageRef, stringid.TruncateID(container.ID), stringid.TruncateID(img.ID))
7374
}
7475
}
7576

@@ -238,7 +239,7 @@ func (daemon *Daemon) imageDeleteHelper(img *image.Image, records *[]types.Image
238239
// either running or stopped).
239240
parentImg, err := daemon.Graph().Get(img.Parent)
240241
if err != nil {
241-
return fmt.Errorf("unable to get parent image: %v", err)
242+
return derr.ErrorCodeImgNoParent.WithArgs(err)
242243
}
243244

244245
// Do not force prunings, but do so quietly (stopping on any encountered

daemon/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/Sirupsen/logrus"
1010
"github.com/docker/docker/api/types"
11+
derr "github.com/docker/docker/errors"
1112
"github.com/docker/docker/image"
1213
"github.com/docker/docker/pkg/graphdb"
1314
"github.com/docker/docker/pkg/nat"
@@ -370,7 +371,7 @@ func (daemon *Daemon) Volumes(filter string) ([]*types.Volume, error) {
370371
filterUsed := false
371372
if i, ok := volFilters["dangling"]; ok {
372373
if len(i) > 1 {
373-
return nil, fmt.Errorf("Conflict: cannot use more than 1 value for `dangling` filter")
374+
return nil, derr.ErrorCodeDanglingOne
374375
}
375376

376377
filterValue := i[0]

daemon/logs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package daemon
22

33
import (
4-
"fmt"
54
"io"
65
"strconv"
76
"time"
87

98
"github.com/Sirupsen/logrus"
109
"github.com/docker/docker/daemon/logger"
10+
derr "github.com/docker/docker/errors"
1111
"github.com/docker/docker/pkg/stdcopy"
1212
)
1313

@@ -32,7 +32,7 @@ type ContainerLogsConfig struct {
3232
// configured with the given struct.
3333
func (daemon *Daemon) ContainerLogs(container *Container, config *ContainerLogsConfig) error {
3434
if !(config.UseStdout || config.UseStderr) {
35-
return fmt.Errorf("You must choose at least one stream")
35+
return derr.ErrorCodeNeedStream
3636
}
3737

3838
outStream := config.OutStream

daemon/pause.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package daemon
22

3-
import "fmt"
3+
import (
4+
derr "github.com/docker/docker/errors"
5+
)
46

57
// ContainerPause pauses a container
68
func (daemon *Daemon) ContainerPause(name string) error {
@@ -10,7 +12,7 @@ func (daemon *Daemon) ContainerPause(name string) error {
1012
}
1113

1214
if err := container.pause(); err != nil {
13-
return fmt.Errorf("Cannot pause container %s: %s", name, err)
15+
return derr.ErrorCodePauseError.WithArgs(name, err)
1416
}
1517

1618
return nil

daemon/rename.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package daemon
22

33
import (
4-
"fmt"
4+
derr "github.com/docker/docker/errors"
55
)
66

77
// ContainerRename changes the name of a container, using the oldName
88
// to find the container. An error is returned if newName is already
99
// reserved.
1010
func (daemon *Daemon) ContainerRename(oldName, newName string) error {
1111
if oldName == "" || newName == "" {
12-
return fmt.Errorf("Neither old nor new names may be empty")
12+
return derr.ErrorCodeEmptyRename
1313
}
1414

1515
container, err := daemon.Get(oldName)
@@ -22,7 +22,7 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
2222
container.Lock()
2323
defer container.Unlock()
2424
if newName, err = daemon.reserveName(container.ID, newName); err != nil {
25-
return fmt.Errorf("Error when allocating new name: %s", err)
25+
return derr.ErrorCodeRenameTaken.WithArgs(err)
2626
}
2727

2828
container.Name = newName
@@ -35,7 +35,7 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
3535

3636
if err := daemon.containerGraphDB.Delete(oldName); err != nil {
3737
undo()
38-
return fmt.Errorf("Failed to delete container %q: %v", oldName, err)
38+
return derr.ErrorCodeRenameDelete.WithArgs(oldName, err)
3939
}
4040

4141
if err := container.toDisk(); err != nil {

0 commit comments

Comments
 (0)