[SERVER-51839] Add error context when document validator with $expr fails by throwing an exception Created: 26/Oct/20  Updated: 29/Oct/23  Resolved: 03/Dec/20

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: 4.9.0

Type: Improvement Priority: Major - P3
Reporter: David Storch Assignee: Mihai Andrei
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Documented
is documented by DOCS-14028 Investigate changes in SERVER-51839: ... Closed
Related
is related to SERVER-20547 Expose the reason an operation fails ... Closed
Backwards Compatibility: Minor Change
Sprint: Query 2020-11-30, Query 2020-12-14
Participants:

 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.



 Comments   
Comment by Githook User [ 03/Dec/20 ]

Author:

{'name': 'Mihai Andrei', 'email': 'mihai.andrei@10gen.com', 'username': 'mtandrei'}

Message: SERVER-51839 Add error context when document validator with $expr fails by throwing an exception

Document validators which use $expr will now generate detailed errors
when $expr throws. When the validationAction is set to error, the write
will fail with a DocumentValidationError instead of the exception's
error code. When the validationAction is set to warn, the write will be
accepted and a detailed error will be logged.
Branch: master
https://github.com/mongodb/mongo/commit/0ffd5e7243e27928a1023cce73a5cc4264f7a8e2

Comment by David Storch [ 26/Oct/20 ]

Although the change suggested here is a small usability improvement, there is a broader question at play here about whether $expr should throw exceptions at all in the context of document validation. By doing so, it fails to play nicely with SERVER-20547, the feature to expose a detailed reason for document validation failure, since the operation is terminated by throwing an exception before we can generate the detailed errInfo object. Perhaps we would configure any ExprMatchExpression nodes for a collection validator to swallow exceptions and return false rather than propagate exceptions?

Generated at Thu Feb 08 05:26:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.