Skip to content

Commit

Permalink
GODRIVER-2603 (Contd.) Revised error handling using Go 1.13 error APIs (
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarlokesh authored Dec 12, 2023
1 parent 2f0917c commit 4dbe540
Show file tree
Hide file tree
Showing 56 changed files with 219 additions and 217 deletions.
2 changes: 1 addition & 1 deletion bson/bsoncodec/default_value_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ func TestDefaultValueEncoders(t *testing.T) {
enc, err := reg.LookupEncoder(reflect.TypeOf(tc.value))
noerr(t, err)
err = enc.EncodeValue(EncodeContext{Registry: reg}, vw, reflect.ValueOf(tc.value))
if err != tc.err {
if !errors.Is(err, tc.err) {
t.Errorf("Did not receive expected error. got %v; want %v", err, tc.err)
}
if diff := cmp.Diff([]byte(b), tc.b); diff != "" {
Expand Down
2 changes: 1 addition & 1 deletion bson/bsoncodec/map_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (mc *MapCodec) mapEncodeValue(ec EncodeContext, dw bsonrw.DocumentWriter, v
}

currEncoder, currVal, lookupErr := defaultValueEncoders.lookupElementEncoder(ec, encoder, val.MapIndex(key))
if lookupErr != nil && lookupErr != errInvalidValue {
if lookupErr != nil && !errors.Is(lookupErr, errInvalidValue) {
return lookupErr
}

Expand Down
10 changes: 4 additions & 6 deletions bson/bsoncodec/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,7 @@ func TestRegistryBuilder(t *testing.T) {
})
t.Run("Decoder", func(t *testing.T) {
wanterr := tc.wanterr
var ene ErrNoEncoder
if errors.As(tc.wanterr, &ene) {
if ene, ok := tc.wanterr.(ErrNoEncoder); ok {
wanterr = ErrNoDecoder(ene)
}

Expand Down Expand Up @@ -424,7 +423,7 @@ func TestRegistryBuilder(t *testing.T) {
want = nil
wanterr := ErrNoTypeMapEntry{Type: bsontype.ObjectID}
got, err = reg.LookupTypeMapEntry(bsontype.ObjectID)
if err != wanterr {
if !errors.Is(err, wanterr) {
t.Errorf("did not get expected error: got %#v, want %#v", err, wanterr)
}
if got != want {
Expand Down Expand Up @@ -777,8 +776,7 @@ func TestRegistry(t *testing.T) {
t.Parallel()

wanterr := tc.wanterr
var ene ErrNoEncoder
if errors.As(tc.wanterr, &ene) {
if ene, ok := tc.wanterr.(ErrNoEncoder); ok {
wanterr = ErrNoDecoder(ene)
}

Expand Down Expand Up @@ -884,7 +882,7 @@ func TestRegistry(t *testing.T) {
want = nil
wanterr := ErrNoTypeMapEntry{Type: bsontype.ObjectID}
got, err = reg.LookupTypeMapEntry(bsontype.ObjectID)
if err != wanterr {
if !errors.Is(err, wanterr) {
t.Errorf("unexpected error: got %#v, want %#v", err, wanterr)
}
if got != want {
Expand Down
5 changes: 3 additions & 2 deletions bson/bsoncodec/slice_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bsoncodec

import (
"errors"
"fmt"
"reflect"

Expand Down Expand Up @@ -93,7 +94,7 @@ func (sc SliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val re

for idx := 0; idx < val.Len(); idx++ {
currEncoder, currVal, lookupErr := defaultValueEncoders.lookupElementEncoder(ec, encoder, val.Index(idx))
if lookupErr != nil && lookupErr != errInvalidValue {
if lookupErr != nil && !errors.Is(lookupErr, errInvalidValue) {
return lookupErr
}

Expand All @@ -102,7 +103,7 @@ func (sc SliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val re
return err
}

if lookupErr == errInvalidValue {
if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions bson/bsoncodec/struct_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ func (sc *StructCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val

desc.encoder, rv, err = defaultValueEncoders.lookupElementEncoder(ec, desc.encoder, rv)

if err != nil && err != errInvalidValue {
if err != nil && !errors.Is(err, errInvalidValue) {
return err
}

if err == errInvalidValue {
if errors.Is(err, errInvalidValue) {
if desc.omitEmpty {
continue
}
Expand Down Expand Up @@ -308,7 +308,7 @@ func (sc *StructCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val

for {
name, vr, err := dr.ReadElement()
if err == bsonrw.ErrEOD {
if errors.Is(err, bsonrw.ErrEOD) {
break
}
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions bson/bsonrw/extjson_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bsonrw

import (
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -131,7 +132,7 @@ func readAllDocuments(vr ValueReader) ([][]byte, error) {
for {
result, err := c.CopyDocumentToBytes(vr)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, err
Expand All @@ -147,7 +148,7 @@ func readAllDocuments(vr ValueReader) ([][]byte, error) {
for {
evr, err := ar.ReadValue()
if err != nil {
if err == ErrEOA {
if errors.Is(err, ErrEOA) {
break
}
return nil, err
Expand Down
14 changes: 7 additions & 7 deletions bson/bsonrw/json_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,17 @@ func (js *jsonScanner) scanLiteral(first byte) (*jsonToken, error) {

c5, err := js.readNextByte()

if bytes.Equal([]byte("true"), lit) && (isValueTerminator(c5) || err == io.EOF) {
if bytes.Equal([]byte("true"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)) {
js.pos = int(math.Max(0, float64(js.pos-1)))
return &jsonToken{t: jttBool, v: true, p: p}, nil
} else if bytes.Equal([]byte("null"), lit) && (isValueTerminator(c5) || err == io.EOF) {
} else if bytes.Equal([]byte("null"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)) {
js.pos = int(math.Max(0, float64(js.pos-1)))
return &jsonToken{t: jttNull, v: nil, p: p}, nil
} else if bytes.Equal([]byte("fals"), lit) {
if c5 == 'e' {
c5, err = js.readNextByte()

if isValueTerminator(c5) || err == io.EOF {
if isValueTerminator(c5) || errors.Is(err, io.EOF) {
js.pos = int(math.Max(0, float64(js.pos-1)))
return &jsonToken{t: jttBool, v: false, p: p}, nil
}
Expand Down Expand Up @@ -413,7 +413,7 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || err == io.EOF {
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
s = nssDone
} else {
s = nssInvalid
Expand All @@ -430,7 +430,7 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || err == io.EOF {
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
s = nssDone
} else if isDigit(c) {
s = nssSawIntegerDigits
Expand All @@ -455,7 +455,7 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || err == io.EOF {
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
s = nssDone
} else if isDigit(c) {
s = nssSawFractionDigits
Expand Down Expand Up @@ -490,7 +490,7 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || err == io.EOF {
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
s = nssDone
} else if isDigit(c) {
s = nssSawExponentDigits
Expand Down
2 changes: 1 addition & 1 deletion bson/bsonrw/value_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ func errequal(t *testing.T, err1, err2 error) bool {
return false
}

if err1 == err2 { // They are the same error, they are equal
if errors.Is(err1, err2) { // They are the same error, they are equal
return true
}

Expand Down
8 changes: 4 additions & 4 deletions bson/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestRaw(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.r.Validate()
if err != tc.err {
if !errors.Is(err, tc.err) {
t.Errorf("Returned error does not match. got %v; want %v", err, tc.err)
}
})
Expand All @@ -128,7 +128,7 @@ func TestRaw(t *testing.T) {
t.Run("empty-key", func(t *testing.T) {
rdr := Raw{'\x05', '\x00', '\x00', '\x00', '\x00'}
_, err := rdr.LookupErr()
if err != bsoncore.ErrEmptyKey {
if !errors.Is(err, bsoncore.ErrEmptyKey) {
t.Errorf("Empty key lookup did not return expected result. got %v; want %v", err, bsoncore.ErrEmptyKey)
}
})
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestRaw(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := tc.r.LookupErr(tc.key...)
if err != tc.err {
if !errors.Is(err, tc.err) {
t.Errorf("Returned error does not match. got %v; want %v", err, tc.err)
}
if !cmp.Equal(got, tc.want) {
Expand All @@ -224,7 +224,7 @@ func TestRaw(t *testing.T) {
t.Run("Out of bounds", func(t *testing.T) {
rdr := Raw{0xe, 0x0, 0x0, 0x0, 0xa, 0x78, 0x0, 0xa, 0x79, 0x0, 0xa, 0x7a, 0x0, 0x0}
_, err := rdr.IndexErr(3)
if err != bsoncore.ErrOutOfBounds {
if !errors.Is(err, bsoncore.ErrOutOfBounds) {
t.Errorf("Out of bounds should be returned when accessing element beyond end of document. got %v; want %v", err, bsoncore.ErrOutOfBounds)
}
})
Expand Down
5 changes: 3 additions & 2 deletions bson/raw_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bson

import (
"errors"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestRawValue(t *testing.T) {
want := ErrNilRegistry
var val RawValue
got := val.UnmarshalWithRegistry(nil, &D{})
if got != want {
if !errors.Is(got, want) {
t.Errorf("Expected errors to match. got %v; want %v", got, want)
}
})
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestRawValue(t *testing.T) {
want := ErrNilContext
var val RawValue
got := val.UnmarshalWithContext(nil, &D{})
if got != want {
if !errors.Is(got, want) {
t.Errorf("Expected errors to match. got %v; want %v", got, want)
}
})
Expand Down
3 changes: 2 additions & 1 deletion bson/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bson

import (
"errors"
"math/rand"
"reflect"
"sync"
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestUnmarshalExtJSONWithRegistry(t *testing.T) {
t.Run("UnmarshalExtJSONInvalidInput", func(t *testing.T) {
data := []byte("invalid")
err := UnmarshalExtJSONWithRegistry(DefaultRegistry, data, true, &M{})
if err != bsonrw.ErrInvalidJSON {
if !errors.Is(err, bsonrw.ErrInvalidJSON) {
t.Fatalf("wanted ErrInvalidJSON, got %v", err)
}
})
Expand Down
7 changes: 2 additions & 5 deletions examples/documentation_examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package documentation_examples

import (
"context"
"errors"
"fmt"
"io/ioutil"
logger "log"
Expand Down Expand Up @@ -1817,8 +1816,7 @@ func RunTransactionWithRetry(sctx mongo.SessionContext, txnFn func(mongo.Session
log.Println("Transaction aborted. Caught exception during transaction.")

// If transient error, retry the whole transaction
var cmdErr mongo.CommandError
if errors.As(err, &cmdErr) && cmdErr.HasErrorLabel("TransientTransactionError") {
if cmdErr, ok := err.(mongo.CommandError); ok && cmdErr.HasErrorLabel("TransientTransactionError") {
log.Println("TransientTransactionError, retrying transaction...")
continue
}
Expand Down Expand Up @@ -1885,8 +1883,7 @@ func TransactionsExamples(ctx context.Context, client *mongo.Client) error {
log.Println("Transaction aborted. Caught exception during transaction.")

// If transient error, retry the whole transaction
var cmdErr mongo.CommandError
if errors.As(err, &cmdErr) && cmdErr.HasErrorLabel("TransientTransactionError") {
if cmdErr, ok := err.(mongo.CommandError); ok && cmdErr.HasErrorLabel("TransientTransactionError") {
log.Println("TransientTransactionError, retrying transaction...")
continue
}
Expand Down
4 changes: 1 addition & 3 deletions internal/aws/awserr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package awserr

import (
"errors"
"fmt"
)

Expand Down Expand Up @@ -107,8 +106,7 @@ func (b baseError) OrigErr() error {
case 1:
return b.errs[0]
default:
var err Error
if errors.As(b.errs[0], &err) {
if err, ok := b.errs[0].(Error); ok {
return NewBatchError(err.Code(), err.Message(), b.errs[1:])
}
return NewBatchError("BatchedErrors",
Expand Down
2 changes: 1 addition & 1 deletion mongo/bulk_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (bw *bulkWrite) execute(ctx context.Context) error {

bwErr.WriteErrors = append(bwErr.WriteErrors, batchErr.WriteErrors...)

commandErrorOccurred := err != nil && err != driver.ErrUnacknowledgedWrite
commandErrorOccurred := err != nil && !errors.Is(err, driver.ErrUnacknowledgedWrite)
writeErrorOccurred := len(batchErr.WriteErrors) > 0 || batchErr.WriteConcernError != nil
if !continueOnError && (commandErrorOccurred || writeErrorOccurred) {
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions mongo/crud_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package mongo_test

import (
"context"
"errors"
"fmt"
"log"
"sync"
Expand Down Expand Up @@ -387,7 +388,7 @@ func ExampleCollection_FindOne() {
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in
// the collection.
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
return
}
log.Fatal(err)
Expand All @@ -413,7 +414,7 @@ func ExampleCollection_FindOneAndDelete() {
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in
// the collection.
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
return
}
log.Fatal(err)
Expand Down Expand Up @@ -442,7 +443,7 @@ func ExampleCollection_FindOneAndReplace() {
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in
// the collection.
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
return
}
log.Fatal(err)
Expand Down Expand Up @@ -471,7 +472,7 @@ func ExampleCollection_FindOneAndUpdate() {
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in
// the collection.
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
return
}
log.Fatal(err)
Expand Down
Loading

0 comments on commit 4dbe540

Please sign in to comment.