Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1194

Confusing error using uninitialized bson.D as a document

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      If an uninitialized bson.D is passed to Find, the following error results:

      cannot transform type primitive.D to a BSON Document: WriteNull can only write while positioned on a Element or Value but is positioned on a TopLevel

      This is a very confusing error for users. As mentioned in GODRIVER-917, we differ from mgo in how we serialize a nil document, but regardless of how we resolve that ticket, I think it would be a better user experience to detect this case earlier in the call chain so we can return a meaningful error message that would help users quickly fix the problem.

      I suspect that any API method (or option) that takes a document could have the same problem as Find.

      Here is a repro (against master):

      package main
      
      import (
      	"go.mongodb.org/mongo-driver/bson"
      	"go.mongodb.org/mongo-driver/mongo"
      	"go.mongodb.org/mongo-driver/mongo/options"
      )
      
      func main() {
      	opts := options.Client().ApplyURI("mongodb://localhost:27017")
      	client, err := mongo.Connect(nil, opts)
      	if err != nil {
      		panic(err)
      	}
      
      	collection := client.Database("test").Collection("test")
      
      	var filter bson.D
      	cursor, err := collection.Find(nil, filter)
      	if err != nil {
      		panic(err)
      	}
      	_ = cursor
      }
      

        1. decode.go
          0.7 kB
          Divjot Arora

            Assignee:
            Unassigned Unassigned
            Reporter:
            david.golden@mongodb.com David Golden
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: