treewide: replace error assertions with errors.As
Change-Id: I369cc1dd8f745203f6f24093049d60d971acdf11
Reviewed-on: https://review.monogon.dev/c/monogon/+/3038
Reviewed-by: Serge Bazanski <serge@monogon.tech>
Tested-by: Jenkins CI
diff --git a/build/savestdout/savestdout.go b/build/savestdout/savestdout.go
index a0fb709..e8c8e75 100644
--- a/build/savestdout/savestdout.go
+++ b/build/savestdout/savestdout.go
@@ -17,6 +17,7 @@
package main
import (
+ "errors"
"log"
"os"
"os/exec"
@@ -43,7 +44,8 @@
return
}
- if e, ok := err.(*exec.ExitError); ok {
+ var e *exec.ExitError
+ if errors.As(err, &e) {
os.Exit(e.ExitCode())
}
diff --git a/metropolis/node/build/mkpayload/mkpayload.go b/metropolis/node/build/mkpayload/mkpayload.go
index aee3a81..a66d458 100644
--- a/metropolis/node/build/mkpayload/mkpayload.go
+++ b/metropolis/node/build/mkpayload/mkpayload.go
@@ -20,6 +20,7 @@
package main
import (
+ "errors"
"flag"
"fmt"
"io"
@@ -179,7 +180,8 @@
return
}
// Exit with objcopy's return code.
- if e, ok := err.(*exec.ExitError); ok {
+ var e *exec.ExitError
+ if errors.As(err, &e) {
os.Exit(e.ExitCode())
}
log.Fatalf("Could not start command: %v", err)
diff --git a/metropolis/node/core/network/dhcp4c/transport/transport.go b/metropolis/node/core/network/dhcp4c/transport/transport.go
index 32b2bd7..c3e6e0b 100644
--- a/metropolis/node/core/network/dhcp4c/transport/transport.go
+++ b/metropolis/node/core/network/dhcp4c/transport/transport.go
@@ -43,7 +43,8 @@
}
func deadlineFromTimeout(err error) error {
- if timeoutErr, ok := err.(net.Error); ok && timeoutErr.Timeout() {
+ var timeoutErr net.Error
+ if errors.As(err, &timeoutErr) && timeoutErr.Timeout() {
return DeadlineExceededErr
}
return err
diff --git a/metropolis/pkg/fsquota/fsquota_test.go b/metropolis/pkg/fsquota/fsquota_test.go
index 243dbf6..4044b60 100644
--- a/metropolis/pkg/fsquota/fsquota_test.go
+++ b/metropolis/pkg/fsquota/fsquota_test.go
@@ -17,6 +17,7 @@
package fsquota
import (
+ "errors"
"fmt"
"math"
"os"
@@ -92,11 +93,10 @@
for {
n, err := testfile.Write(testdata)
if err != nil {
- if pathErr, ok := err.(*os.PathError); ok {
- if pathErr.Err == syscall.ENOSPC {
- // Running out of space is the only acceptable error to continue execution
- break
- }
+ var pathErr *os.PathError
+ if errors.As(err, &pathErr) && errors.Is(pathErr.Err, syscall.ENOSPC) {
+ // Running out of space is the only acceptable error to continue execution
+ break
}
t.Fatal(err)
}
diff --git a/metropolis/pkg/loop/loop.go b/metropolis/pkg/loop/loop.go
index a4974a8..a8be907 100644
--- a/metropolis/pkg/loop/loop.go
+++ b/metropolis/pkg/loop/loop.go
@@ -150,11 +150,10 @@
return nil, fmt.Errorf("failed to allocate loop device: %w", os.NewSyscallError("ioctl(LOOP_CTL_GET_FREE)", errno))
}
dev, err := os.OpenFile(fmt.Sprintf("/dev/loop%v", devNum), os.O_RDWR|os.O_EXCL, 0)
- if pe, ok := err.(*os.PathError); ok {
- if pe.Err == unix.EBUSY {
- // We have lost the race, get a new device
- continue
- }
+ var pe *os.PathError
+ if errors.As(err, &pe) && errors.Is(pe.Err, unix.EBUSY) {
+ // We have lost the race, get a new device
+ continue
}
if err != nil {
return nil, fmt.Errorf("failed to open newly-allocated loop device: %w", err)
diff --git a/metropolis/pkg/tpm/eventlog/internal/events.go b/metropolis/pkg/tpm/eventlog/internal/events.go
index 78d305c..c95e9dc 100644
--- a/metropolis/pkg/tpm/eventlog/internal/events.go
+++ b/metropolis/pkg/tpm/eventlog/internal/events.go
@@ -391,7 +391,8 @@
} else {
// A bug in shim may cause an event to be missing the SignatureOwner GUID.
// We handle this, but signal back to the caller using ErrSigMissingGUID.
- if _, isStructuralErr := err.(asn1.StructuralError); isStructuralErr {
+ var structuralError asn1.StructuralError
+ if errors.As(err, &structuralError) {
var err2 error
cert, err2 = x509.ParseCertificate(b)
if err2 == nil {
diff --git a/metropolis/pkg/tpm/tpm.go b/metropolis/pkg/tpm/tpm.go
index a5a0ee1..785553d 100644
--- a/metropolis/pkg/tpm/tpm.go
+++ b/metropolis/pkg/tpm/tpm.go
@@ -476,7 +476,8 @@
// Use a full policy session for the EK
{Session: endorsementSession, Attributes: tpm2.AttrContinueSession},
}, tpm.akHandleCache, ekHandle, credBlob, secretChallenge)
- if warn, ok := err.(tpm2.Warning); ok && warn.Code == tpm2.RCRetry {
+ var warn tpm2.Warning
+ if errors.As(err, &warn) && warn.Code == tpm2.RCRetry {
time.Sleep(100 * time.Millisecond)
continue
}
diff --git a/metropolis/test/util/runners.go b/metropolis/test/util/runners.go
index a34c070..bb7aa91 100644
--- a/metropolis/test/util/runners.go
+++ b/metropolis/test/util/runners.go
@@ -70,8 +70,8 @@
}
func (p *PermanentError) Is(o error) bool {
- op, ok := o.(*PermanentError)
- if !ok {
+ var op *PermanentError
+ if !errors.As(o, &op) {
return false
}
if p.Err == nil || op.Err == nil {