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

Replace errors.Cause() with errors.Is() / errors.As()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-04-17 12:01:01 +02:00
parent 45369c61a4
commit 07d60bc257
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
23 changed files with 50 additions and 45 deletions

View file

@ -591,7 +591,7 @@ func endsInSlash(driver containerfs.Driver, path string) bool {
func isExistingDirectory(point *copyEndpoint) (bool, error) {
destStat, err := point.driver.Stat(point.path)
switch {
case os.IsNotExist(err):
case errors.Is(err, os.ErrNotExist):
return false, nil
case err != nil:
return false, err

View file

@ -62,7 +62,7 @@ func newArchiveRemote(rc io.ReadCloser, dockerfilePath string) (builder.Source,
func withDockerfileFromContext(c modifiableContext, dockerfilePath string) (builder.Source, *parser.Result, error) {
df, err := openAt(c, dockerfilePath)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
if dockerfilePath == builder.DefaultDockerfileName {
lowercase := strings.ToLower(dockerfilePath)
if _, err := StatAt(c, lowercase); err == nil {

View file

@ -38,7 +38,7 @@ func TestCloseRootDirectory(t *testing.T) {
_, err = os.Stat(src.Root().Path())
if !os.IsNotExist(err) {
if !errors.Is(err, os.ErrNotExist) {
t.Fatal("Directory should not exist at this point")
}
}
@ -131,7 +131,7 @@ func TestRemoveDirectory(t *testing.T) {
}
_, err = src.Root().Stat(src.Root().Join(src.Root().Path(), relativePath))
if !os.IsNotExist(errors.Cause(err)) {
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("Directory should not exist at this point: %+v ", err)
}
}

View file

@ -24,8 +24,7 @@ func (err errConnectionFailed) Error() string {
// IsErrConnectionFailed returns true if the error is caused by connection failed.
func IsErrConnectionFailed(err error) bool {
_, ok := errors.Cause(err).(errConnectionFailed)
return ok
return errors.As(err, &errConnectionFailed{})
}
// ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
@ -42,8 +41,9 @@ type notFound interface {
// IsErrNotFound returns true if the error is a NotFound error, which is returned
// by the API when some object is not found.
func IsErrNotFound(err error) bool {
if _, ok := err.(notFound); ok {
return ok
var e notFound
if errors.As(err, &e) {
return true
}
return errdefs.IsNotFound(err)
}

View file

@ -190,7 +190,7 @@ func (container *Container) UnmountIpcMount() error {
if shmPath == "" {
return nil
}
if err = mount.Unmount(shmPath); err != nil && !os.IsNotExist(errors.Cause(err)) {
if err = mount.Unmount(shmPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
@ -395,7 +395,7 @@ func (container *Container) DetachAndUnmount(volumeEventLog func(name, action st
// are not supported
func ignoreUnsupportedXAttrs() fs.CopyDirOpt {
xeh := func(dst, src, xattrKey string, err error) error {
if errors.Cause(err) != syscall.ENOTSUP {
if !errors.Is(err, syscall.ENOTSUP) {
return err
}
return nil

View file

@ -176,7 +176,8 @@ func (daemon *Daemon) containerAttach(c *container.Container, cfg *stream.Attach
ctx := c.InitAttachContext()
err := <-c.StreamConfig.CopyStreams(ctx, cfg)
if err != nil {
if _, ok := errors.Cause(err).(term.EscapeError); ok || err == context.Canceled {
var ierr term.EscapeError
if errors.Is(err, context.Canceled) || errors.As(err, &ierr) {
daemon.LogContainerEvent(c, "detach")
} else {
logrus.Errorf("attach failed with error: %v", err)

View file

@ -353,7 +353,7 @@ func (c *Cluster) currentNodeState() nodeState {
// Call with read lock.
func (c *Cluster) errNoManager(st nodeState) error {
if st.swarmNode == nil {
if errors.Cause(st.err) == errSwarmLocked {
if errors.Is(st.err, errSwarmLocked) {
return errSwarmLocked
}
if st.err == errSwarmCertificatesExpired {

View file

@ -222,12 +222,17 @@ func (c *containerAdapter) createNetworks(ctx context.Context) error {
}
func (c *containerAdapter) removeNetworks(ctx context.Context) error {
var (
activeEndpointsError *libnetwork.ActiveEndpointsError
errNoSuchNetwork libnetwork.ErrNoSuchNetwork
)
for name, v := range c.container.networksAttachments {
if err := c.backend.DeleteManagedNetwork(v.Network.ID); err != nil {
switch errors.Cause(err).(type) {
case *libnetwork.ActiveEndpointsError:
switch {
case errors.As(err, &activeEndpointsError):
continue
case libnetwork.ErrNoSuchNetwork:
case errors.As(err, &errNoSuchNetwork):
continue
default:
log.G(ctx).Errorf("network %s remove failed: %v", name, err)

View file

@ -204,9 +204,10 @@ func (r *controller) Start(ctx context.Context) error {
return exec.ErrTaskStarted
}
var lnErr libnetwork.ErrNoSuchNetwork
for {
if err := r.adapter.start(ctx); err != nil {
if _, ok := errors.Cause(err).(libnetwork.ErrNoSuchNetwork); ok {
if errors.As(err, &lnErr) {
// Retry network creation again if we
// failed because some of the networks
// were not found.

View file

@ -327,7 +327,7 @@ func (n *nodeRunner) State() nodeState {
ns := n.nodeState
if ns.err != nil || n.cancelReconnect != nil {
if errors.Cause(ns.err) == errSwarmLocked {
if errors.Is(ns.err, errSwarmLocked) {
ns.status = types.LocalNodeStateLocked
} else {
ns.status = types.LocalNodeStateError

View file

@ -347,7 +347,7 @@ func (c *Cluster) UnlockSwarm(req types.UnlockRequest) error {
c.mu.Unlock()
if err := <-nr.Ready(); err != nil {
if errors.Cause(err) == errSwarmLocked {
if errors.Is(err, errSwarmLocked) {
return invalidUnlockKey{}
}
return errors.Errorf("swarm component could not be started: %v", err)
@ -371,7 +371,7 @@ func (c *Cluster) Leave(force bool) error {
c.mu.Unlock()
if errors.Cause(state.err) == errSwarmLocked && !force {
if errors.Is(state.err, errSwarmLocked) && !force {
// leave a locked swarm without --force is not allowed
return errors.WithStack(notAvailableError("Swarm is encrypted and locked. Please unlock it first or use `--force` to ignore this message."))
}

View file

@ -312,7 +312,8 @@ func TestValidateContainerIsolation(t *testing.T) {
func TestFindNetworkErrorType(t *testing.T) {
d := Daemon{}
_, err := d.FindNetwork("fakeNet")
_, ok := errors.Cause(err).(libnetwork.ErrNoSuchNetwork)
var nsn libnetwork.ErrNoSuchNetwork
ok := errors.As(err, &nsn)
if !errdefs.IsNotFound(err) || !ok {
t.Error("The FindNetwork method MUST always return an error that implements the NotFound interface and is ErrNoSuchNetwork")
}

View file

@ -324,7 +324,7 @@ func (a *Driver) Remove(id string) error {
// way (so that docker doesn't find it anymore) before doing removal of
// the whole tree.
if err := atomicRemove(mountpoint); err != nil {
if errors.Cause(err) == unix.EBUSY {
if errors.Is(err, unix.EBUSY) {
logger.WithField("dir", mountpoint).WithError(err).Warn("error performing atomic remove due to EBUSY")
}
return errors.Wrapf(err, "could not remove mountpoint for id %s", id)

View file

@ -42,18 +42,14 @@ func Unmount(target string) error {
var err error
for i := 0; i < retries; i++ {
err = mount.Unmount(target)
switch errors.Cause(err) {
case nil:
return nil
case unix.EBUSY:
if err != nil && errors.Is(err, unix.EBUSY) {
logger.Debugf("aufs unmount %s failed with EBUSY (retrying %d/%d)", target, i+1, retries)
time.Sleep(100 * time.Millisecond)
continue // try again
default:
// any other error is fatal
return err
}
break
}
// either no error occurred, or another error
return err
}

View file

@ -184,7 +184,7 @@ func (i *ImageService) GraphDriverForOS(os string) string {
func (i *ImageService) ReleaseLayer(rwlayer layer.RWLayer, containerOS string) error {
metadata, err := i.layerStores[containerOS].ReleaseRWLayer(rwlayer)
layer.LogReleaseMetadata(metadata)
if err != nil && err != layer.ErrMountDoesNotExist && !os.IsNotExist(errors.Cause(err)) {
if err != nil && !errors.Is(err, layer.ErrMountDoesNotExist) && !errors.Is(err, os.ErrNotExist) {
return errors.Wrapf(err, "driver %q failed to remove root filesystem",
i.layerStores[containerOS].DriverName())
}

View file

@ -428,7 +428,7 @@ func (w *LogFile) openRotatedFiles(config logger.ReadConfig) (files []*os.File,
})
if err != nil {
if !os.IsNotExist(errors.Cause(err)) {
if !errors.Is(err, os.ErrNotExist) {
return nil, errors.Wrap(err, "error getting reference to decompressed log file")
}
continue
@ -533,7 +533,7 @@ func tailFiles(files []SizeReaderAt, watcher *logger.LogWatcher, dec Decoder, ge
for {
msg, err := dec.Decode()
if err != nil {
if errors.Cause(err) != io.EOF {
if !errors.Is(err, io.EOF) {
watcher.Err <- err
}
return
@ -633,7 +633,7 @@ func followLogs(f *os.File, logWatcher *logger.LogWatcher, notifyRotate chan int
oldSize := int64(-1)
handleDecodeErr := func(err error) error {
if errors.Cause(err) != io.EOF {
if !errors.Is(err, io.EOF) {
return err
}

View file

@ -207,8 +207,9 @@ func (s *DockerSuite) TestPostContainersAttach(c *testing.T) {
assert.NilError(c, err)
var outBuf, errBuf bytes.Buffer
var nErr net.Error
_, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
if err != nil && errors.Cause(err).(net.Error).Timeout() {
if errors.As(err, &nErr) && nErr.Timeout() {
// ignore the timeout error as it is expected
err = nil
}

View file

@ -27,7 +27,6 @@ import (
testdaemon "github.com/docker/docker/testutil/daemon"
"github.com/docker/docker/testutil/request"
"github.com/docker/swarmkit/ca"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/poll"
@ -323,7 +322,7 @@ func (s *DockerSwarmSuite) TestAPISwarmLeaderElection(c *testing.T) {
followers = nil
for _, d := range nodes {
n := d.GetNode(c, d.NodeID(), func(err error) bool {
if strings.Contains(errors.Cause(err).Error(), context.DeadlineExceeded.Error()) || strings.Contains(err.Error(), "swarm does not have a leader") {
if strings.Contains(err.Error(), context.DeadlineExceeded.Error()) || strings.Contains(err.Error(), "swarm does not have a leader") {
lastErr = err
return true
}

View file

@ -253,9 +253,9 @@ func TestClientWithRequestTimeout(t *testing.T) {
_, err := client.callWithRetry("/Plugin.Hello", nil, false, WithRequestTimeout(timeout))
assert.Assert(t, is.ErrorContains(err, ""), "expected error")
err = errors.Cause(err)
assert.ErrorType(t, err, (*timeoutError)(nil))
assert.Equal(t, err.(timeoutError).Timeout(), true)
var tErr timeoutError
assert.Assert(t, errors.As(err, &tErr))
assert.Assert(t, tErr.Timeout())
}
type testRequestWrapper struct {

View file

@ -77,11 +77,11 @@ func TestGet(t *testing.T) {
// check negative case where plugin fruit doesn't implement banana
_, err = Get("fruit", "banana")
assert.Equal(t, errors.Cause(err), ErrNotImplements)
assert.Assert(t, errors.Is(err, ErrNotImplements))
// check negative case where plugin vegetable doesn't exist
_, err = Get("vegetable", "potato")
assert.Equal(t, errors.Cause(err), ErrNotFound)
assert.Assert(t, errors.Is(err, ErrNotFound))
}
func TestPluginWithNoManifest(t *testing.T) {

View file

@ -172,7 +172,7 @@ func handleLoadError(err error, id string) {
return
}
logger := logrus.WithError(err).WithField("id", id)
if os.IsNotExist(errors.Cause(err)) {
if errors.Is(err, os.ErrNotExist) {
// Likely some error while removing on an older version of docker
logger.Warn("missing plugin config, skipping: this may be caused due to a failed remove and requires manual cleanup.")
return

View file

@ -153,7 +153,8 @@ func (ps *Store) Get(name, capability string, mode int) (plugingetter.CompatPlug
// but we should error out right away
return nil, errDisabled(name)
}
if _, ok := errors.Cause(err).(errNotFound); !ok {
var ierr errNotFound
if !errors.As(err, &ierr) {
return nil, err
}
}
@ -166,7 +167,7 @@ func (ps *Store) Get(name, capability string, mode int) (plugingetter.CompatPlug
if err == nil {
return p, nil
}
if errors.Cause(err) == plugins.ErrNotFound {
if errors.Is(err, plugins.ErrNotFound) {
return nil, errNotFound(name)
}
return nil, errors.Wrap(errdefs.System(err), "legacy plugin")

View file

@ -743,8 +743,8 @@ func lookupVolume(ctx context.Context, store *drivers.Store, driverName, volumeN
}
v, err := vd.Get(volumeName)
if err != nil {
err = errors.Cause(err)
if _, ok := err.(net.Error); ok {
var nErr net.Error
if errors.As(err, &nErr) {
if v != nil {
volumeName = v.Name()
driverName = v.DriverName()