@@ -31,6 +31,9 @@ const errorCursorKilled int32 = 237
31
31
// contain a resume token.
32
32
var ErrMissingResumeToken = errors .New ("cannot provide resume functionality when the resume token is missing" )
33
33
34
+ // ErrNilCursor indicates that the cursor for the change stream is nil.
35
+ var ErrNilCursor = errors .New ("cursor is nil" )
36
+
34
37
type changeStream struct {
35
38
cmd bsonx.Doc // aggregate command to run to create stream and rebuild cursor
36
39
pipeline bsonx.Arr
@@ -396,10 +399,18 @@ func newClientChangeStream(ctx context.Context, client *Client, pipeline interfa
396
399
}
397
400
398
401
func (cs * changeStream ) ID () int64 {
402
+ if cs .cursor == nil {
403
+ return 0
404
+ }
405
+
399
406
return cs .cursor .ID ()
400
407
}
401
408
402
409
func (cs * changeStream ) Next (ctx context.Context ) bool {
410
+ if cs .cursor == nil {
411
+ return false
412
+ }
413
+
403
414
if cs .cursor .Next (ctx ) {
404
415
return true
405
416
}
@@ -431,6 +442,10 @@ func (cs *changeStream) Next(ctx context.Context) bool {
431
442
}
432
443
433
444
func (cs * changeStream ) Decode (out interface {}) error {
445
+ if cs .cursor == nil {
446
+ return ErrNilCursor
447
+ }
448
+
434
449
br , err := cs .DecodeBytes ()
435
450
if err != nil {
436
451
return err
@@ -440,6 +455,10 @@ func (cs *changeStream) Decode(out interface{}) error {
440
455
}
441
456
442
457
func (cs * changeStream ) DecodeBytes () (bson.Raw , error ) {
458
+ if cs .cursor == nil {
459
+ return nil , ErrNilCursor
460
+ }
461
+
443
462
br , err := cs .cursor .DecodeBytes ()
444
463
if err != nil {
445
464
return nil , err
@@ -471,11 +490,18 @@ func (cs *changeStream) Err() error {
471
490
if cs .err != nil {
472
491
return cs .err
473
492
}
493
+ if cs .cursor == nil {
494
+ return nil
495
+ }
474
496
475
497
return cs .cursor .Err ()
476
498
}
477
499
478
500
func (cs * changeStream ) Close (ctx context.Context ) error {
501
+ if cs .cursor == nil {
502
+ return nil // cursor is already closed
503
+ }
504
+
479
505
return cs .cursor .Close (ctx )
480
506
}
481
507
0 commit comments