Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-51839

Add error context when document validator with $expr fails by throwing an exception

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 4.9.0
    • Component/s: Querying
    • Labels:
      None
    • Backwards Compatibility:
      Minor Change
    • Sprint:
      Query 2020-11-30, Query 2020-12-14

      Description

      The evaluation of an $expr can throw query-fatal exceptions in a variety of conditions, with one classic example being division by zero. When this happens during the evaluation of a collection validator, the insert or update operation will be rejected by the validator. For example, let's suppose that we create a collection validator that always divides by zero:

       > db.createCollection("c", {validator: {$expr: {$divide: [10, 0]}}})
      { "ok" : 1 }
       > db.c.insert({})
      WriteResult({
      	"nInserted" : 0,
      	"writeError" : {
      		"code" : 16608,
      		"errmsg" : "can't $divide by zero"
      	}
      })
      

      The insert is rejected when the system runs the validator to ensure that the write is valid, and the validator itself fails.

      Note, however, that the error message doesn't indicate that the problem occurred when checking the document validator. We should add context to the error message so that it says something like "Document validation failed :: caused by :: can't $divide by zero".

      As an implementation note, we have a convenient method for adding context to an exception before rethrowing it.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mihai.andrei Mihai Andrei
              Reporter:
              david.storch David Storch
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: