Skip to content

Commit e657ce8

Browse files
committed
DOCSP-30350 code blocks and update write/bulkwrite sections
1 parent ede2dcd commit e657ce8

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

source/crud/operation-error-handling.txt

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ The ``WriteError`` type contains the following methods:
5252
The `BulkWriteError <{+core-api+}/bulk/BulkWriteError.html>`__ type extends
5353
``WriteError`` and inherits the above methods. This type is used for bulk
5454
operations and includes an ``index`` field that identifies the item in the bulk
55-
operation that led to the error.
55+
operation that caused the error.
5656

5757
The `WriteConcernError <{+core-api+}/bulk/WriteConcernError.html>`__ is not
58-
related to the ``WriteError`` type and details errors relating to the write
59-
concern. In addition to the ``getCode``, ``getMessage``, and ``getDetails``
58+
related to the ``WriteError`` type and details errors relating to the :ref:`write
59+
concern </reference/write-concern/>`. In addition to the ``getCode``, ``getMessage``, and ``getDetails``
6060
methods, ``WriteConcernError`` has the ``getCodeName`` method, which returns the
6161
name associated with the error code. This type does not have the ``getCategory``
6262
method.
@@ -65,35 +65,40 @@ Write Error
6565
~~~~~~~~~~~
6666

6767
The driver raises a ``WriteError`` error for any errors that it
68-
encounters when performing a write operation that are not related to
69-
satisfying the write concern. Because there are many causes for this
70-
error, the ``WriteError`` type contains fields that describe the type of
71-
write error and reason for the error.
72-
73-
For example, the driver raises a ``WriteError`` error if you attempt to
74-
insert a document into a collection that violates the collection's
75-
schema validation rules. Suppose the collection has a rule where the
76-
value of the ``quantity`` field must be an ``int`` type. If you
77-
attempt to insert a document where the value of ``quantity`` is
78-
``"three"``, the driver prints the following error message:
68+
encounters when performing single write operations that are not related to
69+
satisfying the write concern.
70+
71+
For example, the driver raises a ``WriteError`` error if you attempt to insert a
72+
document into a collection with an ObjectId value that already exists in the
73+
collection. Suppose the collection has an existing document with an ``ObjectId`` of `1`. If you attempt to insert a document
74+
where the value of ``ObjectId`` is ``1``, the driver prints the following
75+
error message:
7976

8077
.. code-block:: none
8178
:copyable: false
82-
:emphasize-lines: 2-3
83-
84-
Error: Error { kind: Write(WriteError(WriteError { code: 121, code_name:
85-
None, message: "Document failed validation", details:
86-
Some(Document({"failingDocumentId": Int32(1), "details":
87-
Document({"operatorName": String("$jsonSchema"), "title":
88-
String("Numerical Validation"), "schemaRulesNotSatisfied":
89-
Array(...)})})) })), labels: {},
90-
wire_version: None, source: None }
91-
92-
In the preceding error message, the ``message`` field describes the
93-
reason for the error, and the ``details`` field provides specific
94-
details about the failing operation. To address this error, you must
95-
either revise the document to adhere to the schema validation rules or
96-
bypass validation.
79+
80+
WriteError: { code: 11000, message: "E11000 duplicate key error collection:
81+
testDB.testCollection index: _id_ dup key: { _id: 1 }", details: {} }
82+
83+
In the preceding error message, the ``message`` field describes the reason for
84+
the error, and the ``details`` field provides specific details about the failing
85+
operation. To address this error, you must revise the document to have a unique
86+
``ObjectId``.
87+
88+
Bulk Write Error
89+
----------------
90+
91+
If this same example was run in during bulk write operation, an additional field
92+
would be added to the error detailing the index of the item with the duplicate
93+
``ObjectId``
94+
95+
.. code-block:: none
96+
:copyable: false
97+
:emphasize-lines: 1
98+
99+
BulkWriteError{index=2, code=11000, message='E11000 duplicate key error
100+
collection: testDB.testCollection index: _id_ dup key: { _id: 1 }',
101+
details=Document{{}}}
97102

98103
Write Concern Error
99104
~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)