Skip to content

Commit da1260d

Browse files
authored
Merge pull request #1755 from AkihiroSuda/fix-errors-join
Fix usage of `errors.Join`
2 parents c5bfa5f + 5e509be commit da1260d

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

pkg/hostagent/hostagent.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
399399
}
400400
return nil
401401
})
402-
var mErr error
402+
var errs []error
403403
if err := a.waitForRequirements("essential", a.essentialRequirements()); err != nil {
404-
mErr = errors.Join(mErr, err)
404+
errs = append(errs, err)
405405
}
406406
if *a.y.SSH.ForwardAgent {
407407
faScript := `#!/bin/bash
@@ -413,79 +413,79 @@ sudo chown -R "${USER}" /run/host-services`
413413
stdout, stderr, err := ssh.ExecuteScript("127.0.0.1", a.sshLocalPort, a.sshConfig, faScript, faDesc)
414414
logrus.Debugf("stdout=%q, stderr=%q, err=%v", stdout, stderr, err)
415415
if err != nil {
416-
mErr = errors.Join(mErr, fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err))
416+
errs = append(errs, fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err))
417417
}
418418
}
419419
if *a.y.MountType == limayaml.REVSSHFS {
420420
mounts, err := a.setupMounts()
421421
if err != nil {
422-
mErr = errors.Join(mErr, err)
422+
errs = append(errs, err)
423423
}
424424
a.onClose = append(a.onClose, func() error {
425-
var unmountMErr error
425+
var unmountErrs []error
426426
for _, m := range mounts {
427427
if unmountErr := m.close(); unmountErr != nil {
428-
unmountMErr = errors.Join(unmountMErr, unmountErr)
428+
unmountErrs = append(unmountErrs, unmountErr)
429429
}
430430
}
431-
return unmountMErr
431+
return errors.Join(unmountErrs...)
432432
})
433433
}
434434
if len(a.y.AdditionalDisks) > 0 {
435435
a.onClose = append(a.onClose, func() error {
436-
var unlockMErr error
436+
var unlockErrs []error
437437
for _, d := range a.y.AdditionalDisks {
438438
disk, inspectErr := store.InspectDisk(d.Name)
439439
if inspectErr != nil {
440-
unlockMErr = errors.Join(unlockMErr, inspectErr)
440+
unlockErrs = append(unlockErrs, inspectErr)
441441
continue
442442
}
443443
logrus.Infof("Unmounting disk %q", disk.Name)
444444
if unlockErr := disk.Unlock(); unlockErr != nil {
445-
unlockMErr = errors.Join(unlockMErr, unlockErr)
445+
unlockErrs = append(unlockErrs, unlockErr)
446446
}
447447
}
448-
return unlockMErr
448+
return errors.Join(unlockErrs...)
449449
})
450450
}
451451
go a.watchGuestAgentEvents(ctx)
452452
if err := a.waitForRequirements("optional", a.optionalRequirements()); err != nil {
453-
mErr = errors.Join(mErr, err)
453+
errs = append(errs, err)
454454
}
455455
if err := a.waitForRequirements("final", a.finalRequirements()); err != nil {
456-
mErr = errors.Join(mErr, err)
456+
errs = append(errs, err)
457457
}
458458
// Copy all config files _after_ the requirements are done
459459
for _, rule := range a.y.CopyToHost {
460460
if err := copyToHost(ctx, a.sshConfig, a.sshLocalPort, rule.HostFile, rule.GuestFile); err != nil {
461-
mErr = errors.Join(mErr, err)
461+
errs = append(errs, err)
462462
}
463463
}
464464
a.onClose = append(a.onClose, func() error {
465-
var mErr error
465+
var rmErrs []error
466466
for _, rule := range a.y.CopyToHost {
467467
if rule.DeleteOnStop {
468468
logrus.Infof("Deleting %s", rule.HostFile)
469469
if err := os.RemoveAll(rule.HostFile); err != nil {
470-
mErr = errors.Join(mErr, err)
470+
rmErrs = append(rmErrs, err)
471471
}
472472
}
473473
}
474-
return mErr
474+
return errors.Join(rmErrs...)
475475
})
476-
return mErr
476+
return errors.Join(errs...)
477477
}
478478

479479
func (a *HostAgent) close() error {
480480
logrus.Infof("Shutting down the host agent")
481-
var mErr error
481+
var errs []error
482482
for i := len(a.onClose) - 1; i >= 0; i-- {
483483
f := a.onClose[i]
484484
if err := f(); err != nil {
485-
mErr = errors.Join(mErr, err)
485+
errs = append(errs, err)
486486
}
487487
}
488-
return mErr
488+
return errors.Join(errs...)
489489
}
490490

491491
func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
@@ -505,20 +505,20 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
505505

506506
a.onClose = append(a.onClose, func() error {
507507
logrus.Debugf("Stop forwarding unix sockets")
508-
var mErr error
508+
var errs []error
509509
for _, rule := range a.y.PortForwards {
510510
if rule.GuestSocket != "" {
511511
local := hostAddress(rule, guestagentapi.IPPort{})
512512
// using ctx.Background() because ctx has already been cancelled
513513
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
514-
mErr = errors.Join(mErr, err)
514+
errs = append(errs, err)
515515
}
516516
}
517517
}
518518
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
519-
mErr = errors.Join(mErr, err)
519+
errs = append(errs, err)
520520
}
521-
return mErr
521+
return errors.Join(errs...)
522522
})
523523

524524
for {

pkg/hostagent/mount.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ type mount struct {
1818
func (a *HostAgent) setupMounts() ([]*mount, error) {
1919
var (
2020
res []*mount
21-
mErr error
21+
errs []error
2222
)
2323
for _, f := range a.y.Mounts {
2424
m, err := a.setupMount(f)
2525
if err != nil {
26-
mErr = errors.Join(mErr, err)
26+
errs = append(errs, err)
2727
continue
2828
}
2929
res = append(res, m)
3030
}
31-
return res, mErr
31+
return res, errors.Join(errs...)
3232
}
3333

3434
func (a *HostAgent) setupMount(m limayaml.Mount) (*mount, error) {

pkg/hostagent/requirements.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (a *HostAgent) waitForRequirements(label string, requirements []requirement
1515
retries = 60
1616
sleepDuration = 10 * time.Second
1717
)
18-
var mErr error
18+
var errs []error
1919

2020
for i, req := range requirements {
2121
retryLoop:
@@ -28,16 +28,17 @@ func (a *HostAgent) waitForRequirements(label string, requirements []requirement
2828
}
2929
if req.fatal {
3030
logrus.Infof("No further %s requirements will be checked", label)
31-
return errors.Join(mErr, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s; skipping further checks: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
31+
errs = append(errs, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s; skipping further checks: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
32+
return errors.Join(errs...)
3233
}
3334
if j == retries-1 {
34-
mErr = errors.Join(mErr, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
35+
errs = append(errs, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
3536
break retryLoop
3637
}
3738
time.Sleep(10 * time.Second)
3839
}
3940
}
40-
return mErr
41+
return errors.Join(errs...)
4142
}
4243

4344
func (a *HostAgent) waitForRequirement(r requirement) error {

pkg/qemu/qemu_driver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ func (l *LimaQemuDriver) removeVNCFiles() error {
282282
}
283283

284284
func (l *LimaQemuDriver) killVhosts() error {
285-
var mErr error
285+
var errs []error
286286
for i, vhost := range l.vhostCmds {
287287
if err := vhost.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
288-
mErr = errors.Join(mErr, fmt.Errorf("Failed to kill virtiofsd instance #%d: %w", i, err))
288+
errs = append(errs, fmt.Errorf("Failed to kill virtiofsd instance #%d: %w", i, err))
289289
}
290290
}
291291

292-
return mErr
292+
return errors.Join(errs...)
293293
}
294294

295295
func (l *LimaQemuDriver) shutdownQEMU(ctx context.Context, timeout time.Duration, qCmd *exec.Cmd, qWaitCh <-chan error) error {

0 commit comments

Comments
 (0)