Skip to content

Commit 0c9cc9d

Browse files
committed
CDRIVER-2256 accurate document-validation errors
The error messages mostly say "invalid key", but invalid values can also cause errors.
1 parent 34165fb commit 0c9cc9d

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

src/mongoc/mongoc-util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ _mongoc_validate_new_document (const bson_t *doc, bson_error_t *error)
303303
bson_set_error (error,
304304
MONGOC_ERROR_COMMAND,
305305
MONGOC_ERROR_COMMAND_INVALID_ARG,
306-
"document to insert contains invalid key: %s",
306+
"invalid document for insert: %s",
307307
validate_err.message);
308308
return false;
309309
}
@@ -321,7 +321,7 @@ _mongoc_validate_replace (const bson_t *doc, bson_error_t *error)
321321
bson_set_error (error,
322322
MONGOC_ERROR_COMMAND,
323323
MONGOC_ERROR_COMMAND_INVALID_ARG,
324-
"replacement document contains invalid key: %s",
324+
"invalid argument for replace: %s",
325325
validate_err.message);
326326
return false;
327327
}
@@ -344,7 +344,7 @@ _mongoc_validate_update (const bson_t *update, bson_error_t *error)
344344
bson_set_error (error,
345345
MONGOC_ERROR_COMMAND,
346346
MONGOC_ERROR_COMMAND_INVALID_ARG,
347-
"update document contains invalid key: %s",
347+
"invalid selector for update: %s",
348348
validate_err.message);
349349
return false;
350350
}

tests/test-mongoc-bulk.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,7 @@ test_insert_check_keys (void)
423423
ASSERT_ERROR_CONTAINS (error,
424424
MONGOC_ERROR_COMMAND,
425425
MONGOC_ERROR_COMMAND_INVALID_ARG,
426-
"document to insert contains invalid key: keys "
427-
"cannot begin with \"$\": \"$dollar\"");
426+
"keys cannot begin with \"$\": \"$dollar\"");
428427

429428
BSON_ASSERT (bson_empty (&reply));
430429

@@ -442,8 +441,7 @@ test_insert_check_keys (void)
442441
ASSERT_ERROR_CONTAINS (error,
443442
MONGOC_ERROR_COMMAND,
444443
MONGOC_ERROR_COMMAND_INVALID_ARG,
445-
"document to insert contains invalid key: keys "
446-
"cannot begin with \"$\": \"$dollar\"");
444+
"keys cannot begin with \"$\": \"$dollar\"");
447445

448446
BSON_ASSERT (bson_empty (&reply));
449447

@@ -460,8 +458,7 @@ test_insert_check_keys (void)
460458
ASSERT_ERROR_CONTAINS (error,
461459
MONGOC_ERROR_COMMAND,
462460
MONGOC_ERROR_COMMAND_INVALID_ARG,
463-
"document to insert contains invalid key: keys "
464-
"cannot contain \".\": \"a.b\"");
461+
"keys cannot contain \".\": \"a.b\"");
465462

466463
BSON_ASSERT (bson_empty (&reply));
467464

@@ -890,8 +887,7 @@ _test_replace_one_check_keys (bool with_opts)
890887
ASSERT_ERROR_CONTAINS (error,
891888
MONGOC_ERROR_COMMAND,
892889
MONGOC_ERROR_COMMAND_INVALID_ARG,
893-
"replacement document contains invalid key: keys "
894-
"cannot begin with \"$\": \"$a\"");
890+
"keys cannot begin with \"$\": \"$a\"");
895891

896892
r = (bool) mongoc_bulk_operation_execute (bulk, &reply, &error);
897893
ASSERT (!r);
@@ -910,8 +906,7 @@ _test_replace_one_check_keys (bool with_opts)
910906
ASSERT_ERROR_CONTAINS (error,
911907
MONGOC_ERROR_COMMAND,
912908
MONGOC_ERROR_COMMAND_INVALID_ARG,
913-
"replacement document contains invalid key: keys "
914-
"cannot begin with \"$\": \"$a\"");
909+
"keys cannot begin with \"$\": \"$a\"");
915910
}
916911

917912
ASSERT (bson_empty (&reply));
@@ -1450,8 +1445,7 @@ _test_replace_one_invalid (bool first)
14501445
test.update = mongoc_bulk_operation_replace_one;
14511446
test.update_with_opts = NULL;
14521447
test.invalid_first = first;
1453-
test.error_message = "replacement document contains invalid key: keys "
1454-
"cannot begin with \"$\": \"$set\"";
1448+
test.error_message = "keys cannot begin with \"$\": \"$set\"";
14551449

14561450
_test_update_validate (&test);
14571451
}
@@ -1466,8 +1460,7 @@ _test_replace_one_with_opts_invalid (bool first)
14661460
test.update = NULL;
14671461
test.update_with_opts = mongoc_bulk_operation_replace_one_with_opts;
14681462
test.invalid_first = first;
1469-
test.error_message = "replacement document contains invalid key: keys "
1470-
"cannot begin with \"$\": \"$set\"";
1463+
test.error_message = "keys cannot begin with \"$\": \"$set\"";
14711464

14721465
_test_update_validate (&test);
14731466
}
@@ -1568,8 +1561,7 @@ _test_insert_invalid (bool with_opts, bool invalid_first)
15681561
bson_t reply;
15691562
bson_error_t error;
15701563
bool r;
1571-
const char *err = "document to insert contains invalid key: keys cannot "
1572-
"contain \".\": \"a.b\"";
1564+
const char *err = "keys cannot contain \".\": \"a.b\"";
15731565

15741566
client = test_framework_client_new ();
15751567
collection = get_test_collection (client, "test_insert_validate");
@@ -1725,7 +1717,7 @@ _test_remove_validate (remove_validate_test_t *test)
17251717
MONGOC_ERROR_COMMAND,
17261718
MONGOC_ERROR_COMMAND_INVALID_ARG,
17271719
"Bulk operation is invalid from prior error: "
1728-
"document to insert contains invalid key: keys "
1720+
"invalid document for insert: keys "
17291721
"cannot begin with \"$\": \"$a\"");
17301722
} else {
17311723
test->remove (bulk, tmp_bson (NULL));
@@ -1741,7 +1733,7 @@ _test_remove_validate (remove_validate_test_t *test)
17411733
ASSERT_ERROR_CONTAINS (error,
17421734
MONGOC_ERROR_COMMAND,
17431735
MONGOC_ERROR_COMMAND_INVALID_ARG,
1744-
"document to insert contains invalid key: keys "
1736+
"invalid document for insert: keys "
17451737
"cannot begin with \"$\": \"$a\"");
17461738

17471739
bson_destroy (&reply);

tests/test-mongoc-collection.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,15 +472,15 @@ test_insert (void)
472472
ASSERT_ERROR_CONTAINS (error,
473473
MONGOC_ERROR_COMMAND,
474474
MONGOC_ERROR_COMMAND_INVALID_ARG,
475-
"contains invalid key");
475+
"invalid document");
476476

477477
r = mongoc_collection_insert_one (
478478
collection, tmp_bson ("{'a.b': 1}"), NULL, NULL, &error);
479479
ASSERT (!r);
480480
ASSERT_ERROR_CONTAINS (error,
481481
MONGOC_ERROR_COMMAND,
482482
MONGOC_ERROR_COMMAND_INVALID_ARG,
483-
"contains invalid key");
483+
"invalid document");
484484

485485
ASSERT_OR_PRINT (mongoc_collection_drop (collection, &error), error);
486486

@@ -1020,7 +1020,7 @@ test_save (void)
10201020
ASSERT_ERROR_CONTAINS (error,
10211021
MONGOC_ERROR_COMMAND,
10221022
MONGOC_ERROR_COMMAND_INVALID_ARG,
1023-
"contains invalid key");
1023+
"invalid document");
10241024

10251025
r = mongoc_collection_save (
10261026
collection, tmp_bson ("{'a.b': 1}"), NULL, &error);
@@ -1031,7 +1031,7 @@ test_save (void)
10311031
ASSERT_ERROR_CONTAINS (error,
10321032
MONGOC_ERROR_COMMAND,
10331033
MONGOC_ERROR_COMMAND_INVALID_ARG,
1034-
"contains invalid key");
1034+
"invalid document");
10351035

10361036
mongoc_collection_destroy (collection);
10371037
mongoc_database_destroy (database);
@@ -3599,7 +3599,7 @@ _test_insert_validate (insert_fn_t insert_fn)
35993599
ASSERT_ERROR_CONTAINS (error,
36003600
MONGOC_ERROR_COMMAND,
36013601
MONGOC_ERROR_COMMAND_INVALID_ARG,
3602-
"document to insert contains invalid key");
3602+
"invalid document");
36033603

36043604
BSON_ASSERT (!insert_fn (collection,
36053605
tmp_bson ("{'$': 1}"),
@@ -5047,7 +5047,7 @@ _test_update_and_replace (bool is_replace, bool is_multi)
50475047
ASSERT_ERROR_CONTAINS (err,
50485048
MONGOC_ERROR_COMMAND,
50495049
MONGOC_ERROR_COMMAND_INVALID_ARG,
5050-
"invalid key");
5050+
"invalid argument for replace");
50515051
} else {
50525052
/* Test arrayFilters if the servers supports it */
50535053
if (test_framework_max_wire_version_at_least (6)) {
@@ -5128,7 +5128,7 @@ _test_update_validate (update_fn_t update_fn)
51285128
if (update_fn == mongoc_collection_replace_one) {
51295129
/* prohibited for replace */
51305130
update = tmp_bson ("{'$set': {'x': 1}}");
5131-
msg = "replacement document contains invalid key";
5131+
msg = "invalid argument for replace";
51325132
} else {
51335133
/* prohibited for update */
51345134
update = tmp_bson ("{'x': 1}");

0 commit comments

Comments
 (0)