Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-2120

Handling of nonexistent document fields is frustrating

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.9.0
    • Affects Version/s: None
    • Component/s: None

      Consider a document doc =

      { "secret" : "message" }

      . If I attempt to access a non-existent field doc["sceret"], either because I made a typo in my code or for another reason, then use the resulting document::element, the driver throws a vague and unhelpful exception.

      The root problem here is that the code that throws the exception runs when I attempt to use the result of my field lookup, not when I perform the lookup itself:

      auto e = doc["sceret"]; // this returns an unset element e, but does not throw
      auto value = e.get_value(); // this line throws, because e is an unset element
      

      Once my unset element e is created, it loses any attachment to my original query (with the problematic nonexistent typo field "sceret"). This makes it very hard to improve the user experience with nonexistent fields. It makes it impossible for us to provide a more helpful exception message (like "document field 'sceret' does not exist").

      The most common use case probably looks like this, with method chaining after the field lookup:

      auto some_message = doc["sceret"].get_string().value;
      

      Exception stack tracing could help in this use case, because the line with the offending lookup would be included in the trace. However, the exception will still have been thrown from a different place, in the get_string() method, which could be confusing for users.

      document's operator[] used to throw, but several tickets removed that behavior:
      https://jira.mongodb.org/browse/CXX-862
      https://jira.mongodb.org/browse/CXX-1089

      Those tickets discuss their reasons for removing throwing from operator[], which include wanting to remove inconsistencies between different classes in the driver. However, we should revisit these discussions and decide whether those arguments outweigh the sheer frustration of debugging typos in document lookup, and how that plays into the overall frustration users experience with the driver.

            Assignee:
            josh.siegel@mongodb.com Josh Siegel
            Reporter:
            samantha.ritter@mongodb.com Samantha Ritter (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: