Return asserted value from `import("./src/mongo/shell/assert.js").assert`

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • DevProd Test Infrastructure
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Currently, our assert function just asserts on the value passed in, without returning.
      src/mongo/shell/assert.js:251-263

      function assert(value, msg, attr) {
          if (arguments.length > 3) {
              _doassert("Too many parameters to assert().");
          }
      
          _validateAssertionMessage(msg, attr);
      
          if (value) {
              return;
          }
      
          _doassert(msg, "assert failed", attr);
      }
      

      This makes using assert sometimes clunky, because a segment of code such as:

      const db = assert(rst.getPrimary(), "could not get primary").getDB("test");
      

      needs to be 3x as many lines long:

      const primary = rst.getPrimary();
      assert(primary, "could not get primary");
      const db = primary.getDB("test");
      

      We should refactor assert to return the asserted value. If this is for some reason undesired, we could also provide a variant of assert with this behavior named asserted(...) or something similar.

            Assignee:
            Unassigned
            Reporter:
            Joseph Obaraye
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: