Skip to content

Commit a8ae1bf

Browse files
committed
*: use errors.HasType / HasInterface / As instead of casts
Release note: None
1 parent 15c7371 commit a8ae1bf

124 files changed

Lines changed: 431 additions & 523 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pkg/acceptance/localcluster/cluster.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ func (n *Node) startAsyncInnerLocked(ctx context.Context, joins ...string) error
588588

589589
log.Infof(ctx, "process %d: %s", cmd.Process.Pid, cmd.ProcessState)
590590

591-
execErr, _ := waitErr.(*exec.ExitError)
591+
var execErr *exec.ExitError
592+
_ = errors.As(waitErr, &execErr)
592593
n.Lock()
593594
n.setNotRunningLocked(execErr)
594595
n.Unlock()

pkg/base/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/cockroachdb/cockroach/pkg/util/envutil"
2525
"github.com/cockroachdb/cockroach/pkg/util/mon"
2626
"github.com/cockroachdb/cockroach/pkg/util/retry"
27+
"github.com/cockroachdb/errors"
2728
)
2829

2930
// Base config defaults.
@@ -219,7 +220,7 @@ type Config struct {
219220
}
220221

221222
func wrapError(err error) error {
222-
if _, ok := err.(*security.Error); !ok {
223+
if !errors.HasType(err, (*security.Error)(nil)) {
223224
return &security.Error{
224225
Message: "problem using security settings",
225226
Err: err,

pkg/ccl/backupccl/restore_job.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ func WriteTableDescs(
493493
b.InitPut(kv.Key, &kv.Value, false)
494494
}
495495
if err := txn.Run(ctx, b); err != nil {
496-
if _, ok := errors.UnwrapAll(err).(*roachpb.ConditionFailedError); ok {
496+
if errors.HasType(err, (*roachpb.ConditionFailedError)(nil)) {
497497
return pgerror.Newf(pgcode.DuplicateObject, "table already exists")
498498
}
499499
return err

pkg/ccl/changefeedccl/errors.go

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ package changefeedccl
1010

1111
import (
1212
"fmt"
13+
"reflect"
1314
"strings"
15+
16+
"github.com/cockroachdb/errors"
1417
)
1518

1619
const retryableErrorString = "retryable changefeed error"
@@ -40,40 +43,46 @@ func (e *retryableError) Unwrap() error { return e.wrapped }
4043
// IsRetryableError returns true if the supplied error, or any of its parent
4144
// causes, is a IsRetryableError.
4245
func IsRetryableError(err error) bool {
43-
for {
44-
if err == nil {
45-
return false
46-
}
47-
if _, ok := err.(*retryableError); ok {
48-
return true
49-
}
50-
errStr := err.Error()
51-
if strings.Contains(errStr, retryableErrorString) {
52-
// If a RetryableError occurs on a remote node, DistSQL serializes it such
53-
// that we can't recover the structure and we have to rely on this
54-
// unfortunate string comparison.
55-
return true
56-
}
57-
if strings.Contains(errStr, `rpc error`) {
58-
// When a crdb node dies, any DistSQL flows with processors scheduled on
59-
// it get an error with "rpc error" in the message from the call to
60-
// `(*DistSQLPlanner).Run`.
61-
return true
62-
}
63-
if e, ok := err.(interface{ Unwrap() error }); ok {
64-
err = e.Unwrap()
65-
continue
66-
}
46+
if err == nil {
6747
return false
6848
}
49+
if errors.HasType(err, (*retryableError)(nil)) {
50+
return true
51+
}
52+
53+
// TODO(knz): this is a bad implementation. Make it go away
54+
// by avoiding string comparisons.
55+
56+
errStr := err.Error()
57+
if strings.Contains(errStr, retryableErrorString) {
58+
// If a RetryableError occurs on a remote node, DistSQL serializes it such
59+
// that we can't recover the structure and we have to rely on this
60+
// unfortunate string comparison.
61+
return true
62+
}
63+
if strings.Contains(errStr, `rpc error`) {
64+
// When a crdb node dies, any DistSQL flows with processors scheduled on
65+
// it get an error with "rpc error" in the message from the call to
66+
// `(*DistSQLPlanner).Run`.
67+
return true
68+
}
69+
return false
6970
}
7071

7172
// MaybeStripRetryableErrorMarker performs some minimal attempt to clean the
7273
// RetryableError marker out. This won't do anything if the RetryableError
7374
// itself has been wrapped, but that's okay, we'll just have an uglier string.
7475
func MaybeStripRetryableErrorMarker(err error) error {
75-
if e, ok := err.(*retryableError); ok {
76-
err = e.wrapped
76+
// The following is a hack to work around the error cast linter.
77+
// What we're doing here is really not kosher; this function
78+
// has no business in assuming that the retryableError{} wrapper
79+
// has not been wrapped already. We could even expect that
80+
// it gets wrapped in the common case.
81+
// TODO(knz): Remove/replace this.
82+
if reflect.TypeOf(err) == retryableErrorType {
83+
err = errors.UnwrapOnce(err)
7784
}
7885
return err
7986
}
87+
88+
var retryableErrorType = reflect.TypeOf((*retryableError)(nil))

pkg/ccl/changefeedccl/kvfeed/kv_feed.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,16 +276,15 @@ func (f *kvFeed) runUntilTableEvent(
276276
// buffer, its seems that we could do this without having to destroy and
277277
// recreate the rangefeeds.
278278
err = g.Wait()
279-
switch err := err.(type) {
280-
case nil:
279+
if err == nil {
281280
log.Fatalf(ctx, "feed exited with no error and no scan boundary")
282281
return hlc.Timestamp{}, nil // unreachable
283-
case *errBoundaryReached:
282+
} else if tErr := (*errBoundaryReached)(nil); errors.As(err, &tErr) {
284283
// TODO(ajwerner): iterate the spans and add a Resolved timestamp.
285284
// We'll need to do this to ensure that a resolved timestamp propagates
286285
// when we're trying to exit.
287-
return err.Timestamp().Prev(), nil
288-
default:
286+
return tErr.Timestamp().Prev(), nil
287+
} else {
289288
return hlc.Timestamp{}, err
290289
}
291290
}

pkg/ccl/importccl/import_processor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,14 @@ func ingestKvs(
332332
// more efficient than parsing every kv.
333333
if indexID == 1 {
334334
if err := pkIndexAdder.Add(ctx, kv.Key, kv.Value.RawBytes); err != nil {
335-
if _, ok := err.(storagebase.DuplicateKeyError); ok {
335+
if errors.HasType(err, (*storagebase.DuplicateKeyError)(nil)) {
336336
return errors.Wrap(err, "duplicate key in primary index")
337337
}
338338
return err
339339
}
340340
} else {
341341
if err := indexAdder.Add(ctx, kv.Key, kv.Value.RawBytes); err != nil {
342-
if _, ok := err.(storagebase.DuplicateKeyError); ok {
342+
if errors.HasType(err, (*storagebase.DuplicateKeyError)(nil)) {
343343
return errors.Wrap(err, "duplicate key in index")
344344
}
345345
return err
@@ -363,14 +363,14 @@ func ingestKvs(
363363
}
364364

365365
if err := pkIndexAdder.Flush(ctx); err != nil {
366-
if err, ok := err.(storagebase.DuplicateKeyError); ok {
366+
if errors.HasType(err, (*storagebase.DuplicateKeyError)(nil)) {
367367
return nil, errors.Wrap(err, "duplicate key in primary index")
368368
}
369369
return nil, err
370370
}
371371

372372
if err := indexAdder.Flush(ctx); err != nil {
373-
if err, ok := err.(storagebase.DuplicateKeyError); ok {
373+
if errors.HasType(err, (*storagebase.DuplicateKeyError)(nil)) {
374374
return nil, errors.Wrap(err, "duplicate key in index")
375375
}
376376
return nil, err

pkg/ccl/importccl/read_import_base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ type importFileContext struct {
395395
func handleCorruptRow(ctx context.Context, fileCtx *importFileContext, err error) error {
396396
log.Errorf(ctx, "%v", err)
397397

398-
if rowErr, isRowErr := err.(*importRowError); isRowErr && fileCtx.rejected != nil {
398+
if rowErr := (*importRowError)(nil); errors.As(err, &rowErr) && fileCtx.rejected != nil {
399399
fileCtx.rejected <- rowErr.row + "\n"
400400
return nil
401401
}

pkg/cli/debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ func removeDeadReplicas(
11061106
return nil, errors.Wrap(err, "loading MVCCStats")
11071107
}
11081108
err = storage.MVCCPutProto(ctx, batch, &ms, key, clock.Now(), nil /* txn */, &desc)
1109-
if wiErr, ok := err.(*roachpb.WriteIntentError); ok {
1109+
if wiErr := (*roachpb.WriteIntentError)(nil); errors.As(err, &wiErr) {
11101110
if len(wiErr.Intents) != 1 {
11111111
return nil, errors.Errorf("expected 1 intent, found %d: %s", len(wiErr.Intents), wiErr)
11121112
}

pkg/cli/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (f *formattedError) Error() string {
6969

7070
// Extract the fields.
7171
var message, code, hint, detail, location string
72-
if pqErr, ok := errors.UnwrapAll(f.err).(*pq.Error); ok {
72+
if pqErr := (*pq.Error)(nil); errors.As(f.err, &pqErr) {
7373
if pqErr.Severity != "" {
7474
severity = pqErr.Severity
7575
}

pkg/cli/error_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func TestErrorReporting(t *testing.T) {
174174
checked := checkAndMaybeShoutTo(tt.err, got.Log)
175175
assert.Equal(t, tt.err, checked, "should return error unchanged")
176176
assert.Equal(t, tt.wantSeverity, got.Severity, "wrong severity log")
177-
_, gotCLI := got.Err.(*cliError)
177+
gotCLI := errors.HasType(got.Err, (*cliError)(nil))
178178
if tt.wantCLICause {
179179
assert.True(t, gotCLI, "logged cause should be *cliError, got %T", got.Err)
180180
} else {

0 commit comments

Comments
 (0)