Skip to content

Commit 512d203

Browse files
authored
Replace errors.As with errors.AsType (#8779)
This PR replaces all uses of `errors.As()` with `errors.AsType[]()`. One testing assertion, `AssertErrorWraps`, was updating the error supplied to the errors.As call with the unwrapped error result. Changing to errors.AsType, I have NOT preserved that behavior, which seemed spooky-at-a-distance. `AssertErrorWraps` was regularly followed-up with additional testing assertions on the presumably-unwrapped error, which cases just now have an additional errors.AsType call in their immediate vicinity to provide the unwrapped error. Fixes #8541
1 parent e4f4650 commit 512d203

36 files changed

Lines changed: 122 additions & 116 deletions

bdns/dns.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ func (c *impl) exchangeOne(ctx context.Context, hostname string, qtype uint16) (
230230

231231
// Check if the error is a network timeout, rather than a local context
232232
// timeout. If it is, retry instead of giving up.
233-
var netErr net.Error
234-
isRetryable := ctx.Err() == nil && errors.As(err, &netErr) && netErr.Timeout()
233+
netErr, isNetError := errors.AsType[net.Error](err)
234+
isRetryable := ctx.Err() == nil && isNetError && netErr.Timeout()
235235
hasRetriesLeft := tries < c.maxTries
236236
if isRetryable && hasRetriesLeft {
237237
continue

bdns/problem.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,17 @@ var extendedErrorCodeToString = map[uint16]string{
9898
func (d Error) Error() string {
9999
var detail, additional string
100100
if d.underlying != nil {
101-
var netErr *net.OpError
102-
var urlErr *url.Error
103-
if errors.As(d.underlying, &netErr) {
101+
netErr, netOk := errors.AsType[*net.OpError](d.underlying)
102+
urlErr, urlOk := errors.AsType[*url.Error](d.underlying)
103+
if netOk {
104104
if netErr.Timeout() {
105105
detail = detailDNSTimeout
106106
} else {
107107
detail = detailDNSNetFailure
108108
}
109109
// Note: we check d.underlying here even though `Timeout()` does this because the call to `netErr.Timeout()` above only
110110
// happens for `*net.OpError` underlying types!
111-
} else if errors.As(d.underlying, &urlErr) && urlErr.Timeout() {
111+
} else if urlOk && urlErr.Timeout() {
112112
// For DOH queries, we can get back a `*url.Error` that wraps the unexported type
113113
// `http.httpError`. Unfortunately `http.httpError` doesn't wrap any errors (like
114114
// context.DeadlineExceeded), we can't check for that; instead we need to call Timeout().

ca/ca.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ func NewCAMetrics(stats prometheus.Registerer) *caMetrics {
104104
}
105105

106106
func (m *caMetrics) noteSignError(err error) {
107-
var pkcs11Error pkcs11.Error
108-
if errors.As(err, &pkcs11Error) {
107+
_, ok := errors.AsType[pkcs11.Error](err)
108+
if ok {
109109
m.signErrorCount.WithLabelValues("HSM").Inc()
110110
}
111111
}

cmd/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ func (t *TLSConfig) Load(scope prometheus.Registerer) (*tls.Config, error) {
178178
[]string{"serial"})
179179
err = scope.Register(tlsNotBefore)
180180
if err != nil {
181-
are := prometheus.AlreadyRegisteredError{}
182-
if errors.As(err, &are) {
181+
are, ok := errors.AsType[prometheus.AlreadyRegisteredError](err)
182+
if ok {
183183
tlsNotBefore = are.ExistingCollector.(*prometheus.GaugeVec)
184184
} else {
185185
return nil, err
@@ -194,8 +194,8 @@ func (t *TLSConfig) Load(scope prometheus.Registerer) (*tls.Config, error) {
194194
[]string{"serial"})
195195
err = scope.Register(tlsNotAfter)
196196
if err != nil {
197-
are := prometheus.AlreadyRegisteredError{}
198-
if errors.As(err, &are) {
197+
are, ok := errors.AsType[prometheus.AlreadyRegisteredError](err)
198+
if ok {
199199
tlsNotAfter = are.ExistingCollector.(*prometheus.GaugeVec)
200200
} else {
201201
return nil, err

config/duration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
3535
s := ""
3636
err := json.Unmarshal(b, &s)
3737
if err != nil {
38-
var jsonUnmarshalTypeErr *json.UnmarshalTypeError
39-
if errors.As(err, &jsonUnmarshalTypeErr) {
38+
_, ok := errors.AsType[*json.UnmarshalTypeError](err)
39+
if ok {
4040
return ErrDurationMustBeString
4141
}
4242
return err

core/util_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,13 @@ func TestValidSerial(t *testing.T) {
269269
}
270270

271271
func TestLoadCert(t *testing.T) {
272-
var osPathErr *os.PathError
273272
_, err := LoadCert("")
274273
test.AssertError(t, err, "Loading empty path did not error")
275-
test.AssertErrorWraps(t, err, &osPathErr)
274+
test.AssertErrorWraps[*os.PathError](t, err)
276275

277276
_, err = LoadCert("totally/fake/path")
278277
test.AssertError(t, err, "Loading nonexistent path did not error")
279-
test.AssertErrorWraps(t, err, &osPathErr)
278+
test.AssertErrorWraps[*os.PathError](t, err)
280279

281280
_, err = LoadCert("../test/hierarchy/README.md")
282281
test.AssertError(t, err, "Loading non-PEM file did not error")

crl/storer/storer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ func (cs *crlStorer) UploadCRL(stream grpc.ClientStreamingServer[cspb.UploadCRLR
166166
Key: &filename,
167167
})
168168
if err != nil {
169-
var smithyErr *smithyhttp.ResponseError
170-
if !errors.As(err, &smithyErr) || smithyErr.HTTPStatusCode() != 404 {
169+
smithyErr, ok := errors.AsType[*smithyhttp.ResponseError](err)
170+
if !ok || smithyErr.HTTPStatusCode() != 404 {
171171
return fmt.Errorf("getting previous CRL for %s: %w", crlId, err)
172172
}
173173
cs.log.Infof("No previous CRL found for %s, proceeding", crlId)

db/map.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ func (e ErrDatabaseOp) Unwrap() error {
4747
// Error 1062: Duplicate entry. This error is returned when inserting a row
4848
// would violate a unique key constraint.
4949
func IsDuplicate(err error) bool {
50-
var dbErr *mysql.MySQLError
51-
return errors.As(err, &dbErr) && dbErr.Number == 1062
50+
dbErr, ok := errors.AsType[*mysql.MySQLError](err)
51+
if ok && dbErr.Number == 1062 {
52+
return true
53+
}
54+
return false
5255
}
5356

5457
// WrappedMap wraps a *borp.DbMap such that its major functions wrap error

db/map_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ func testDbMap(t *testing.T) *WrappedMap {
202202
func TestWrappedMap(t *testing.T) {
203203
mustDbErr := func(err error) ErrDatabaseOp {
204204
t.Helper()
205-
var dbOpErr ErrDatabaseOp
206-
test.AssertErrorWraps(t, err, &dbOpErr)
205+
test.AssertErrorWraps[ErrDatabaseOp](t, err)
206+
dbOpErr, _ := errors.AsType[ErrDatabaseOp](err)
207207
return dbOpErr
208208
}
209209

grpc/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func newClientMetrics(stats prometheus.Registerer) (clientMetrics, error) {
106106
)
107107
err := stats.Register(grpcMetrics)
108108
if err != nil {
109-
are := prometheus.AlreadyRegisteredError{}
110-
if errors.As(err, &are) {
109+
are, ok := errors.AsType[prometheus.AlreadyRegisteredError](err)
110+
if ok {
111111
grpcMetrics = are.ExistingCollector.(*grpc_prometheus.ClientMetrics)
112112
} else {
113113
return clientMetrics{}, err
@@ -121,8 +121,8 @@ func newClientMetrics(stats prometheus.Registerer) (clientMetrics, error) {
121121
}, []string{"method", "service"})
122122
err = stats.Register(inFlightGauge)
123123
if err != nil {
124-
are := prometheus.AlreadyRegisteredError{}
125-
if errors.As(err, &are) {
124+
are, ok := errors.AsType[prometheus.AlreadyRegisteredError](err)
125+
if ok {
126126
inFlightGauge = are.ExistingCollector.(*prometheus.GaugeVec)
127127
} else {
128128
return clientMetrics{}, err

0 commit comments

Comments
 (0)