Skip to content

Commit f3e687c

Browse files
committed
GODRIVER-2774 Refactor replaceErrors to always wrap errors instead of replacing them.
1 parent 4a4e6dc commit f3e687c

File tree

10 files changed

+138
-71
lines changed

10 files changed

+138
-71
lines changed

internal/codecutil/encoding.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ type MarshalError struct {
2828

2929
// Error implements the error interface.
3030
func (e MarshalError) Error() string {
31-
return fmt.Sprintf("cannot transform type %s to a BSON Document: %v",
31+
return fmt.Sprintf("cannot marshal type %q to a BSON Document: %v",
3232
reflect.TypeOf(e.Value), e.Err)
3333
}
3434

35+
func (e MarshalError) Unwrap() error { return e.Err }
36+
3537
// EncoderFn is used to functionally construct an encoder for marshaling values.
3638
type EncoderFn func(io.Writer) *bson.Encoder
3739

mongo/change_stream.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,15 @@ AggregateExecuteLoop:
376376
}
377377
}
378378
if err != nil {
379-
cs.err = replaceErrors(err)
379+
cs.err = wrapErrors(err)
380380
return cs.err
381381
}
382382

383383
cr := cs.aggregate.ResultCursorResponse()
384384
cr.Server = server
385385

386386
cs.cursor, cs.err = driver.NewBatchCursor(cr, cs.sess, cs.client.clock, cs.cursorOptions)
387-
if cs.err = replaceErrors(cs.err); cs.err != nil {
387+
if cs.err = wrapErrors(cs.err); cs.err != nil {
388388
return cs.Err()
389389
}
390390

@@ -597,13 +597,13 @@ func (cs *ChangeStream) Decode(val interface{}) error {
597597
// Err returns the last error seen by the change stream, or nil if no errors has occurred.
598598
func (cs *ChangeStream) Err() error {
599599
if cs.err != nil {
600-
return replaceErrors(cs.err)
600+
return wrapErrors(cs.err)
601601
}
602602
if cs.cursor == nil {
603603
return nil
604604
}
605605

606-
return replaceErrors(cs.cursor.Err())
606+
return wrapErrors(cs.cursor.Err())
607607
}
608608

609609
// Close closes this change stream and the underlying cursor. Next and TryNext must not be called after Close has been
@@ -619,7 +619,7 @@ func (cs *ChangeStream) Close(ctx context.Context) error {
619619
return nil // cursor is already closed
620620
}
621621

622-
cs.err = replaceErrors(cs.cursor.Close(ctx))
622+
cs.err = wrapErrors(cs.cursor.Close(ctx))
623623
cs.cursor = nil
624624
return cs.Err()
625625
}
@@ -678,7 +678,7 @@ func (cs *ChangeStream) next(ctx context.Context, nonBlocking bool) bool {
678678
if len(cs.batch) == 0 {
679679
cs.loopNext(ctx, nonBlocking)
680680
if cs.err != nil {
681-
cs.err = replaceErrors(cs.err)
681+
cs.err = wrapErrors(cs.err)
682682
return false
683683
}
684684
if len(cs.batch) == 0 {
@@ -719,7 +719,7 @@ func (cs *ChangeStream) loopNext(ctx context.Context, nonBlocking bool) {
719719
return
720720
}
721721

722-
cs.err = replaceErrors(cs.cursor.Err())
722+
cs.err = wrapErrors(cs.cursor.Err())
723723
if cs.err == nil {
724724
// Check if cursor is alive
725725
if cs.ID() == 0 {

mongo/client.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func newClient(opts ...*options.ClientOptions) (*Client, error) {
239239
if client.deployment == nil {
240240
client.deployment, err = topology.New(cfg)
241241
if err != nil {
242-
return nil, replaceErrors(err)
242+
return nil, wrapErrors(err)
243243
}
244244
}
245245

@@ -261,7 +261,7 @@ func (c *Client) connect() error {
261261
if connector, ok := c.deployment.(driver.Connector); ok {
262262
err := connector.Connect()
263263
if err != nil {
264-
return replaceErrors(err)
264+
return wrapErrors(err)
265265
}
266266
}
267267

@@ -293,7 +293,7 @@ func (c *Client) connect() error {
293293
if subscriber, ok := c.deployment.(driver.Subscriber); ok {
294294
sub, err := subscriber.Subscribe()
295295
if err != nil {
296-
return replaceErrors(err)
296+
return wrapErrors(err)
297297
}
298298
updateChan = sub.Updates
299299
}
@@ -350,7 +350,7 @@ func (c *Client) Disconnect(ctx context.Context) error {
350350
}
351351

352352
if disconnector, ok := c.deployment.(driver.Disconnector); ok {
353-
return replaceErrors(disconnector.Disconnect(ctx))
353+
return wrapErrors(disconnector.Disconnect(ctx))
354354
}
355355

356356
return nil
@@ -381,7 +381,7 @@ func (c *Client) Ping(ctx context.Context, rp *readpref.ReadPref) error {
381381
{"ping", 1},
382382
}, options.RunCmd().SetReadPreference(rp))
383383

384-
return replaceErrors(res.Err())
384+
return wrapErrors(res.Err())
385385
}
386386

387387
// StartSession starts a new session configured with the given options.
@@ -434,7 +434,7 @@ func (c *Client) StartSession(opts ...options.Lister[options.SessionOptions]) (*
434434

435435
sess, err := session.NewClientSession(c.sessionPool, c.id, coreOpts)
436436
if err != nil {
437-
return nil, replaceErrors(err)
437+
return nil, wrapErrors(err)
438438
}
439439

440440
return &Session{
@@ -741,7 +741,7 @@ func (c *Client) ListDatabases(ctx context.Context, filter interface{}, opts ...
741741

742742
err = op.Execute(ctx)
743743
if err != nil {
744-
return ListDatabasesResult{}, replaceErrors(err)
744+
return ListDatabasesResult{}, wrapErrors(err)
745745
}
746746

747747
return newListDatabasesResultFromOperation(op.Result()), nil
@@ -965,7 +965,7 @@ func (c *Client) BulkWrite(ctx context.Context, writes []ClientBulkWrite,
965965
op.result.Acknowledged = acknowledged
966966
op.result.HasVerboseResults = !op.errorsOnly
967967
err = op.execute(ctx)
968-
return &op.result, replaceErrors(err)
968+
return &op.result, wrapErrors(err)
969969
}
970970

971971
// newLogger will use the LoggerOptions to create an internal logger and publish

mongo/collection.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (coll *Collection) BulkWrite(ctx context.Context, models []WriteModel,
248248

249249
err = op.execute(ctx)
250250

251-
return &op.result, replaceErrors(err)
251+
return &op.result, wrapErrors(err)
252252
}
253253

254254
func (coll *Collection) insert(
@@ -1049,15 +1049,15 @@ func aggregate(a aggregateParams, opts ...options.Lister[options.AggregateOption
10491049
if errors.As(err, &wce) && wce.WriteConcernError != nil {
10501050
return nil, *convertDriverWriteConcernError(wce.WriteConcernError)
10511051
}
1052-
return nil, replaceErrors(err)
1052+
return nil, wrapErrors(err)
10531053
}
10541054

10551055
bc, err := op.Result(cursorOpts)
10561056
if err != nil {
1057-
return nil, replaceErrors(err)
1057+
return nil, wrapErrors(err)
10581058
}
10591059
cursor, err := newCursorWithSession(bc, a.client.bsonOpts, a.registry, sess)
1060-
return cursor, replaceErrors(err)
1060+
return cursor, wrapErrors(err)
10611061
}
10621062

10631063
// CountDocuments returns the number of documents in the collection. For a fast count of the documents in the
@@ -1132,7 +1132,7 @@ func (coll *Collection) CountDocuments(ctx context.Context, filter interface{},
11321132

11331133
err = op.Execute(ctx)
11341134
if err != nil {
1135-
return 0, replaceErrors(err)
1135+
return 0, wrapErrors(err)
11361136
}
11371137

11381138
batch := op.ResultCursorResponse().FirstBatch
@@ -1213,7 +1213,7 @@ func (coll *Collection) EstimatedDocumentCount(
12131213
op.Retry(retry)
12141214

12151215
err = op.Execute(ctx)
1216-
return op.Result().N, replaceErrors(err)
1216+
return op.Result().N, wrapErrors(err)
12171217
}
12181218

12191219
// Distinct executes a distinct command to find the unique values for a specified field in the collection.
@@ -1302,7 +1302,7 @@ func (coll *Collection) Distinct(
13021302

13031303
err = op.Execute(ctx)
13041304
if err != nil {
1305-
return &DistinctResult{err: replaceErrors(err)}
1305+
return &DistinctResult{err: wrapErrors(err)}
13061306
}
13071307

13081308
arr, ok := op.Result().Values.ArrayOK()
@@ -1504,12 +1504,12 @@ func (coll *Collection) find(
15041504
op = op.Retry(retry)
15051505

15061506
if err = op.Execute(ctx); err != nil {
1507-
return nil, replaceErrors(err)
1507+
return nil, wrapErrors(err)
15081508
}
15091509

15101510
bc, err := op.Result(cursorOpts)
15111511
if err != nil {
1512-
return nil, replaceErrors(err)
1512+
return nil, wrapErrors(err)
15131513
}
15141514
return newCursorWithSession(bc, coll.bsonOpts, coll.registry, sess)
15151515
}
@@ -1560,7 +1560,7 @@ func (coll *Collection) FindOne(ctx context.Context, filter interface{},
15601560
cur: cursor,
15611561
bsonOpts: coll.bsonOpts,
15621562
reg: coll.registry,
1563-
err: replaceErrors(err),
1563+
err: wrapErrors(err),
15641564
}
15651565
}
15661566

@@ -2044,7 +2044,7 @@ func (coll *Collection) drop(ctx context.Context) error {
20442044
// ignore namespace not found errors
20452045
var driverErr driver.Error
20462046
if !errors.As(err, &driverErr) || !driverErr.NamespaceNotFound() {
2047-
return replaceErrors(err)
2047+
return wrapErrors(err)
20482048
}
20492049
return nil
20502050
}

mongo/cursor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (c *Cursor) next(ctx context.Context, nonBlocking bool) bool {
194194
// If we don't have a next batch
195195
if !c.bc.Next(ctx) {
196196
// Do we have an error? If so we return false.
197-
c.err = replaceErrors(c.bc.Err())
197+
c.err = wrapErrors(c.bc.Err())
198198
if c.err != nil {
199199
return false
200200
}
@@ -289,7 +289,7 @@ func (c *Cursor) Err() error { return c.err }
289289
// the first call, any subsequent calls will not change the state.
290290
func (c *Cursor) Close(ctx context.Context) error {
291291
defer c.closeImplicitSession()
292-
return replaceErrors(c.bc.Close(ctx))
292+
return wrapErrors(c.bc.Close(ctx))
293293
}
294294

295295
// All iterates the cursor and decodes each document into results. The results parameter must be a pointer to a slice.
@@ -336,7 +336,7 @@ func (c *Cursor) All(ctx context.Context, results interface{}) error {
336336
batch = c.bc.Batch()
337337
}
338338

339-
if err = replaceErrors(c.bc.Err()); err != nil {
339+
if err = wrapErrors(c.bc.Err()); err != nil {
340340
return err
341341
}
342342

mongo/database.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (db *Database) RunCommandCursor(
288288
op, sess, err := db.processRunCommand(ctx, runCommand, true, opts...)
289289
if err != nil {
290290
closeImplicitSession(sess)
291-
return nil, replaceErrors(err)
291+
return nil, wrapErrors(err)
292292
}
293293

294294
if err = op.Execute(ctx); err != nil {
@@ -297,16 +297,16 @@ func (db *Database) RunCommandCursor(
297297
return nil, errors.New(
298298
"database response does not contain a cursor; try using RunCommand instead")
299299
}
300-
return nil, replaceErrors(err)
300+
return nil, wrapErrors(err)
301301
}
302302

303303
bc, err := op.ResultCursor()
304304
if err != nil {
305305
closeImplicitSession(sess)
306-
return nil, replaceErrors(err)
306+
return nil, wrapErrors(err)
307307
}
308308
cursor, err := newCursorWithSession(bc, db.bsonOpts, db.registry, sess)
309-
return cursor, replaceErrors(err)
309+
return cursor, wrapErrors(err)
310310
}
311311

312312
// Drop drops the database on the server. This method ignores "namespace not found" errors so it is safe to drop
@@ -347,7 +347,7 @@ func (db *Database) Drop(ctx context.Context) error {
347347

348348
var driverErr driver.Error
349349
if err != nil && (!errors.As(err, &driverErr) || !driverErr.NamespaceNotFound()) {
350-
return replaceErrors(err)
350+
return wrapErrors(err)
351351
}
352352
return nil
353353
}
@@ -497,16 +497,16 @@ func (db *Database) ListCollections(
497497
err = op.Execute(ctx)
498498
if err != nil {
499499
closeImplicitSession(sess)
500-
return nil, replaceErrors(err)
500+
return nil, wrapErrors(err)
501501
}
502502

503503
bc, err := op.Result(cursorOpts)
504504
if err != nil {
505505
closeImplicitSession(sess)
506-
return nil, replaceErrors(err)
506+
return nil, wrapErrors(err)
507507
}
508508
cursor, err := newCursorWithSession(bc, db.bsonOpts, db.registry, sess)
509-
return cursor, replaceErrors(err)
509+
return cursor, wrapErrors(err)
510510
}
511511

512512
// ListCollectionNames executes a listCollections command and returns a slice containing the names of the collections
@@ -944,7 +944,7 @@ func (db *Database) executeCreateOperation(ctx context.Context, op *operation.Cr
944944
Deployment(db.client.deployment).
945945
Crypt(db.client.cryptFLE)
946946

947-
return replaceErrors(op.Execute(ctx))
947+
return wrapErrors(op.Execute(ctx))
948948
}
949949

950950
// GridFSBucket is used to construct a GridFS bucket which can be used as a

0 commit comments

Comments
 (0)