Browse code

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

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2020/04/17 19:01:01
Showing 23 changed files
... ...
@@ -591,7 +591,7 @@ func endsInSlash(driver containerfs.Driver, path string) bool {
591 591
 func isExistingDirectory(point *copyEndpoint) (bool, error) {
592 592
 	destStat, err := point.driver.Stat(point.path)
593 593
 	switch {
594
-	case os.IsNotExist(err):
594
+	case errors.Is(err, os.ErrNotExist):
595 595
 		return false, nil
596 596
 	case err != nil:
597 597
 		return false, err
... ...
@@ -62,7 +62,7 @@ func newArchiveRemote(rc io.ReadCloser, dockerfilePath string) (builder.Source,
62 62
 func withDockerfileFromContext(c modifiableContext, dockerfilePath string) (builder.Source, *parser.Result, error) {
63 63
 	df, err := openAt(c, dockerfilePath)
64 64
 	if err != nil {
65
-		if os.IsNotExist(err) {
65
+		if errors.Is(err, os.ErrNotExist) {
66 66
 			if dockerfilePath == builder.DefaultDockerfileName {
67 67
 				lowercase := strings.ToLower(dockerfilePath)
68 68
 				if _, err := StatAt(c, lowercase); err == nil {
... ...
@@ -38,7 +38,7 @@ func TestCloseRootDirectory(t *testing.T) {
38 38
 
39 39
 	_, err = os.Stat(src.Root().Path())
40 40
 
41
-	if !os.IsNotExist(err) {
41
+	if !errors.Is(err, os.ErrNotExist) {
42 42
 		t.Fatal("Directory should not exist at this point")
43 43
 	}
44 44
 }
... ...
@@ -131,7 +131,7 @@ func TestRemoveDirectory(t *testing.T) {
131 131
 	}
132 132
 
133 133
 	_, err = src.Root().Stat(src.Root().Join(src.Root().Path(), relativePath))
134
-	if !os.IsNotExist(errors.Cause(err)) {
134
+	if !errors.Is(err, os.ErrNotExist) {
135 135
 		t.Fatalf("Directory should not exist at this point: %+v ", err)
136 136
 	}
137 137
 }
... ...
@@ -24,8 +24,7 @@ func (err errConnectionFailed) Error() string {
24 24
 
25 25
 // IsErrConnectionFailed returns true if the error is caused by connection failed.
26 26
 func IsErrConnectionFailed(err error) bool {
27
-	_, ok := errors.Cause(err).(errConnectionFailed)
28
-	return ok
27
+	return errors.As(err, &errConnectionFailed{})
29 28
 }
30 29
 
31 30
 // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
... ...
@@ -42,8 +41,9 @@ type notFound interface {
42 42
 // IsErrNotFound returns true if the error is a NotFound error, which is returned
43 43
 // by the API when some object is not found.
44 44
 func IsErrNotFound(err error) bool {
45
-	if _, ok := err.(notFound); ok {
46
-		return ok
45
+	var e notFound
46
+	if errors.As(err, &e) {
47
+		return true
47 48
 	}
48 49
 	return errdefs.IsNotFound(err)
49 50
 }
... ...
@@ -190,7 +190,7 @@ func (container *Container) UnmountIpcMount() error {
190 190
 	if shmPath == "" {
191 191
 		return nil
192 192
 	}
193
-	if err = mount.Unmount(shmPath); err != nil && !os.IsNotExist(errors.Cause(err)) {
193
+	if err = mount.Unmount(shmPath); err != nil && !errors.Is(err, os.ErrNotExist) {
194 194
 		return err
195 195
 	}
196 196
 	return nil
... ...
@@ -395,7 +395,7 @@ func (container *Container) DetachAndUnmount(volumeEventLog func(name, action st
395 395
 // are not supported
396 396
 func ignoreUnsupportedXAttrs() fs.CopyDirOpt {
397 397
 	xeh := func(dst, src, xattrKey string, err error) error {
398
-		if errors.Cause(err) != syscall.ENOTSUP {
398
+		if !errors.Is(err, syscall.ENOTSUP) {
399 399
 			return err
400 400
 		}
401 401
 		return nil
... ...
@@ -176,7 +176,8 @@ func (daemon *Daemon) containerAttach(c *container.Container, cfg *stream.Attach
176 176
 	ctx := c.InitAttachContext()
177 177
 	err := <-c.StreamConfig.CopyStreams(ctx, cfg)
178 178
 	if err != nil {
179
-		if _, ok := errors.Cause(err).(term.EscapeError); ok || err == context.Canceled {
179
+		var ierr term.EscapeError
180
+		if errors.Is(err, context.Canceled) || errors.As(err, &ierr) {
180 181
 			daemon.LogContainerEvent(c, "detach")
181 182
 		} else {
182 183
 			logrus.Errorf("attach failed with error: %v", err)
... ...
@@ -353,7 +353,7 @@ func (c *Cluster) currentNodeState() nodeState {
353 353
 // Call with read lock.
354 354
 func (c *Cluster) errNoManager(st nodeState) error {
355 355
 	if st.swarmNode == nil {
356
-		if errors.Cause(st.err) == errSwarmLocked {
356
+		if errors.Is(st.err, errSwarmLocked) {
357 357
 			return errSwarmLocked
358 358
 		}
359 359
 		if st.err == errSwarmCertificatesExpired {
... ...
@@ -222,12 +222,17 @@ func (c *containerAdapter) createNetworks(ctx context.Context) error {
222 222
 }
223 223
 
224 224
 func (c *containerAdapter) removeNetworks(ctx context.Context) error {
225
+	var (
226
+		activeEndpointsError *libnetwork.ActiveEndpointsError
227
+		errNoSuchNetwork     libnetwork.ErrNoSuchNetwork
228
+	)
229
+
225 230
 	for name, v := range c.container.networksAttachments {
226 231
 		if err := c.backend.DeleteManagedNetwork(v.Network.ID); err != nil {
227
-			switch errors.Cause(err).(type) {
228
-			case *libnetwork.ActiveEndpointsError:
232
+			switch {
233
+			case errors.As(err, &activeEndpointsError):
229 234
 				continue
230
-			case libnetwork.ErrNoSuchNetwork:
235
+			case errors.As(err, &errNoSuchNetwork):
231 236
 				continue
232 237
 			default:
233 238
 				log.G(ctx).Errorf("network %s remove failed: %v", name, err)
... ...
@@ -204,9 +204,10 @@ func (r *controller) Start(ctx context.Context) error {
204 204
 		return exec.ErrTaskStarted
205 205
 	}
206 206
 
207
+	var lnErr libnetwork.ErrNoSuchNetwork
207 208
 	for {
208 209
 		if err := r.adapter.start(ctx); err != nil {
209
-			if _, ok := errors.Cause(err).(libnetwork.ErrNoSuchNetwork); ok {
210
+			if errors.As(err, &lnErr) {
210 211
 				// Retry network creation again if we
211 212
 				// failed because some of the networks
212 213
 				// were not found.
... ...
@@ -327,7 +327,7 @@ func (n *nodeRunner) State() nodeState {
327 327
 	ns := n.nodeState
328 328
 
329 329
 	if ns.err != nil || n.cancelReconnect != nil {
330
-		if errors.Cause(ns.err) == errSwarmLocked {
330
+		if errors.Is(ns.err, errSwarmLocked) {
331 331
 			ns.status = types.LocalNodeStateLocked
332 332
 		} else {
333 333
 			ns.status = types.LocalNodeStateError
... ...
@@ -347,7 +347,7 @@ func (c *Cluster) UnlockSwarm(req types.UnlockRequest) error {
347 347
 	c.mu.Unlock()
348 348
 
349 349
 	if err := <-nr.Ready(); err != nil {
350
-		if errors.Cause(err) == errSwarmLocked {
350
+		if errors.Is(err, errSwarmLocked) {
351 351
 			return invalidUnlockKey{}
352 352
 		}
353 353
 		return errors.Errorf("swarm component could not be started: %v", err)
... ...
@@ -371,7 +371,7 @@ func (c *Cluster) Leave(force bool) error {
371 371
 
372 372
 	c.mu.Unlock()
373 373
 
374
-	if errors.Cause(state.err) == errSwarmLocked && !force {
374
+	if errors.Is(state.err, errSwarmLocked) && !force {
375 375
 		// leave a locked swarm without --force is not allowed
376 376
 		return errors.WithStack(notAvailableError("Swarm is encrypted and locked. Please unlock it first or use `--force` to ignore this message."))
377 377
 	}
... ...
@@ -312,7 +312,8 @@ func TestValidateContainerIsolation(t *testing.T) {
312 312
 func TestFindNetworkErrorType(t *testing.T) {
313 313
 	d := Daemon{}
314 314
 	_, err := d.FindNetwork("fakeNet")
315
-	_, ok := errors.Cause(err).(libnetwork.ErrNoSuchNetwork)
315
+	var nsn libnetwork.ErrNoSuchNetwork
316
+	ok := errors.As(err, &nsn)
316 317
 	if !errdefs.IsNotFound(err) || !ok {
317 318
 		t.Error("The FindNetwork method MUST always return an error that implements the NotFound interface and is ErrNoSuchNetwork")
318 319
 	}
... ...
@@ -324,7 +324,7 @@ func (a *Driver) Remove(id string) error {
324 324
 	// way (so that docker doesn't find it anymore) before doing removal of
325 325
 	// the whole tree.
326 326
 	if err := atomicRemove(mountpoint); err != nil {
327
-		if errors.Cause(err) == unix.EBUSY {
327
+		if errors.Is(err, unix.EBUSY) {
328 328
 			logger.WithField("dir", mountpoint).WithError(err).Warn("error performing atomic remove due to EBUSY")
329 329
 		}
330 330
 		return errors.Wrapf(err, "could not remove mountpoint for id %s", id)
... ...
@@ -42,18 +42,14 @@ func Unmount(target string) error {
42 42
 	var err error
43 43
 	for i := 0; i < retries; i++ {
44 44
 		err = mount.Unmount(target)
45
-		switch errors.Cause(err) {
46
-		case nil:
47
-			return nil
48
-		case unix.EBUSY:
45
+		if err != nil && errors.Is(err, unix.EBUSY) {
49 46
 			logger.Debugf("aufs unmount %s failed with EBUSY (retrying %d/%d)", target, i+1, retries)
50 47
 			time.Sleep(100 * time.Millisecond)
51 48
 			continue // try again
52
-		default:
53
-			// any other error is fatal
54
-			return err
55 49
 		}
50
+		break
56 51
 	}
57 52
 
53
+	// either no error occurred, or another error
58 54
 	return err
59 55
 }
... ...
@@ -184,7 +184,7 @@ func (i *ImageService) GraphDriverForOS(os string) string {
184 184
 func (i *ImageService) ReleaseLayer(rwlayer layer.RWLayer, containerOS string) error {
185 185
 	metadata, err := i.layerStores[containerOS].ReleaseRWLayer(rwlayer)
186 186
 	layer.LogReleaseMetadata(metadata)
187
-	if err != nil && err != layer.ErrMountDoesNotExist && !os.IsNotExist(errors.Cause(err)) {
187
+	if err != nil && !errors.Is(err, layer.ErrMountDoesNotExist) && !errors.Is(err, os.ErrNotExist) {
188 188
 		return errors.Wrapf(err, "driver %q failed to remove root filesystem",
189 189
 			i.layerStores[containerOS].DriverName())
190 190
 	}
... ...
@@ -428,7 +428,7 @@ func (w *LogFile) openRotatedFiles(config logger.ReadConfig) (files []*os.File,
428 428
 			})
429 429
 
430 430
 			if err != nil {
431
-				if !os.IsNotExist(errors.Cause(err)) {
431
+				if !errors.Is(err, os.ErrNotExist) {
432 432
 					return nil, errors.Wrap(err, "error getting reference to decompressed log file")
433 433
 				}
434 434
 				continue
... ...
@@ -533,7 +533,7 @@ func tailFiles(files []SizeReaderAt, watcher *logger.LogWatcher, dec Decoder, ge
533 533
 	for {
534 534
 		msg, err := dec.Decode()
535 535
 		if err != nil {
536
-			if errors.Cause(err) != io.EOF {
536
+			if !errors.Is(err, io.EOF) {
537 537
 				watcher.Err <- err
538 538
 			}
539 539
 			return
... ...
@@ -633,7 +633,7 @@ func followLogs(f *os.File, logWatcher *logger.LogWatcher, notifyRotate chan int
633 633
 
634 634
 	oldSize := int64(-1)
635 635
 	handleDecodeErr := func(err error) error {
636
-		if errors.Cause(err) != io.EOF {
636
+		if !errors.Is(err, io.EOF) {
637 637
 			return err
638 638
 		}
639 639
 
... ...
@@ -207,8 +207,9 @@ func (s *DockerSuite) TestPostContainersAttach(c *testing.T) {
207 207
 	assert.NilError(c, err)
208 208
 
209 209
 	var outBuf, errBuf bytes.Buffer
210
+	var nErr net.Error
210 211
 	_, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
211
-	if err != nil && errors.Cause(err).(net.Error).Timeout() {
212
+	if errors.As(err, &nErr) && nErr.Timeout() {
212 213
 		// ignore the timeout error as it is expected
213 214
 		err = nil
214 215
 	}
... ...
@@ -27,7 +27,6 @@ import (
27 27
 	testdaemon "github.com/docker/docker/testutil/daemon"
28 28
 	"github.com/docker/docker/testutil/request"
29 29
 	"github.com/docker/swarmkit/ca"
30
-	"github.com/pkg/errors"
31 30
 	"gotest.tools/v3/assert"
32 31
 	is "gotest.tools/v3/assert/cmp"
33 32
 	"gotest.tools/v3/poll"
... ...
@@ -323,7 +322,7 @@ func (s *DockerSwarmSuite) TestAPISwarmLeaderElection(c *testing.T) {
323 323
 			followers = nil
324 324
 			for _, d := range nodes {
325 325
 				n := d.GetNode(c, d.NodeID(), func(err error) bool {
326
-					if strings.Contains(errors.Cause(err).Error(), context.DeadlineExceeded.Error()) || strings.Contains(err.Error(), "swarm does not have a leader") {
326
+					if strings.Contains(err.Error(), context.DeadlineExceeded.Error()) || strings.Contains(err.Error(), "swarm does not have a leader") {
327 327
 						lastErr = err
328 328
 						return true
329 329
 					}
... ...
@@ -253,9 +253,9 @@ func TestClientWithRequestTimeout(t *testing.T) {
253 253
 	_, err := client.callWithRetry("/Plugin.Hello", nil, false, WithRequestTimeout(timeout))
254 254
 	assert.Assert(t, is.ErrorContains(err, ""), "expected error")
255 255
 
256
-	err = errors.Cause(err)
257
-	assert.ErrorType(t, err, (*timeoutError)(nil))
258
-	assert.Equal(t, err.(timeoutError).Timeout(), true)
256
+	var tErr timeoutError
257
+	assert.Assert(t, errors.As(err, &tErr))
258
+	assert.Assert(t, tErr.Timeout())
259 259
 }
260 260
 
261 261
 type testRequestWrapper struct {
... ...
@@ -77,11 +77,11 @@ func TestGet(t *testing.T) {
77 77
 
78 78
 	// check negative case where plugin fruit doesn't implement banana
79 79
 	_, err = Get("fruit", "banana")
80
-	assert.Equal(t, errors.Cause(err), ErrNotImplements)
80
+	assert.Assert(t, errors.Is(err, ErrNotImplements))
81 81
 
82 82
 	// check negative case where plugin vegetable doesn't exist
83 83
 	_, err = Get("vegetable", "potato")
84
-	assert.Equal(t, errors.Cause(err), ErrNotFound)
84
+	assert.Assert(t, errors.Is(err, ErrNotFound))
85 85
 }
86 86
 
87 87
 func TestPluginWithNoManifest(t *testing.T) {
... ...
@@ -172,7 +172,7 @@ func handleLoadError(err error, id string) {
172 172
 		return
173 173
 	}
174 174
 	logger := logrus.WithError(err).WithField("id", id)
175
-	if os.IsNotExist(errors.Cause(err)) {
175
+	if errors.Is(err, os.ErrNotExist) {
176 176
 		// Likely some error while removing on an older version of docker
177 177
 		logger.Warn("missing plugin config, skipping: this may be caused due to a failed remove and requires manual cleanup.")
178 178
 		return
... ...
@@ -153,7 +153,8 @@ func (ps *Store) Get(name, capability string, mode int) (plugingetter.CompatPlug
153 153
 			// but we should error out right away
154 154
 			return nil, errDisabled(name)
155 155
 		}
156
-		if _, ok := errors.Cause(err).(errNotFound); !ok {
156
+		var ierr errNotFound
157
+		if !errors.As(err, &ierr) {
157 158
 			return nil, err
158 159
 		}
159 160
 	}
... ...
@@ -166,7 +167,7 @@ func (ps *Store) Get(name, capability string, mode int) (plugingetter.CompatPlug
166 166
 	if err == nil {
167 167
 		return p, nil
168 168
 	}
169
-	if errors.Cause(err) == plugins.ErrNotFound {
169
+	if errors.Is(err, plugins.ErrNotFound) {
170 170
 		return nil, errNotFound(name)
171 171
 	}
172 172
 	return nil, errors.Wrap(errdefs.System(err), "legacy plugin")
... ...
@@ -743,8 +743,8 @@ func lookupVolume(ctx context.Context, store *drivers.Store, driverName, volumeN
743 743
 	}
744 744
 	v, err := vd.Get(volumeName)
745 745
 	if err != nil {
746
-		err = errors.Cause(err)
747
-		if _, ok := err.(net.Error); ok {
746
+		var nErr net.Error
747
+		if errors.As(err, &nErr) {
748 748
 			if v != nil {
749 749
 				volumeName = v.Name()
750 750
 				driverName = v.DriverName()