Skip to content

Commit 6c53ccb

Browse files
authored
DOCSP-30562: operation error handling (#35)
1 parent e17987b commit 6c53ccb

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

source/index.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/quick-reference
1111
/fundamentals
1212
API Documentation <{+api+}/>
13+
/op-error-handling
1314
/issues-and-help
1415
/compatibility
1516
View the Source <https://github.com/mongodb/mongo-rust-driver>
@@ -69,6 +70,12 @@ FAQ
6970
.. For answers to commonly asked questions about the {+driver-long+}, see
7071
.. the :ref:`rust-faq` section.
7172

73+
Operation Error Handling
74+
------------------------
75+
76+
To learn about errors you might encounter when using the {+driver-long+}
77+
to perform MongoDB operations, see :ref:`rust-operation-errors`.
78+
7279
Issues & Help
7380
-------------
7481

source/op-error-handling.txt

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
.. _rust-operation-errors:
2+
3+
========================
4+
Operation Error Handling
5+
========================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
This page describes errors you might encounter when
17+
using the {+driver-long+} to perform MongoDB operations. Once you
18+
understand the types of operation errors that the driver raises, you can take
19+
appropriate actions to either handle them or correct the error-causing code.
20+
21+
.. note::
22+
23+
This page addresses only operation error handling. If you encounter
24+
any other issues with MongoDB or the driver, visit the following
25+
resources:
26+
27+
- The :ref:`Issues & Help <rust-issues-and-help>` page, which has
28+
information about reporting bugs, contributing to the driver, and
29+
finding more resources
30+
- The `MongoDB Community Forums <https://community.mongodb.com>`__ for
31+
questions, discussions, or general technical support
32+
33+
..
34+
- The :ref:`Frequently Asked Questions (FAQ) <golang-faq>` for the
35+
{+driver-short+}
36+
- :ref:`rust-connection-troubleshooting`
37+
38+
Error Type
39+
----------
40+
41+
If the driver encounters an error while performing an operation, it
42+
returns an error of the `Error <{+api+}/error/struct.Error.html>`__ type.
43+
44+
The ``Error`` type contains the ``kind`` field, which describes the type of
45+
error that occurred. The ``kind`` field has a value of enum `ErrorKind
46+
<{+api+}/error/enum.ErrorKind.html>`__. The ``ErrorKind`` enum has
47+
variants for different kinds of errors, including the following:
48+
49+
- ``InvalidArgument``: you provided an invalid argument to a method
50+
- ``Authentication``: the driver encountered an error during authentication
51+
- ``ServerSelection``: the client couldn't select a server for the operation
52+
- ``Write``: an error occurred during a write operation
53+
- ``Transaction``: an error occurred during a transaction
54+
55+
For example, if you attempt to perform an insert operation that
56+
duplicates an ``_id`` field value that is already in the collection,
57+
the driver returns an ``Error`` instance and prints the following error
58+
message:
59+
60+
.. code-block:: none
61+
:copyable: false
62+
:emphasize-lines: 1
63+
64+
Error: Error { kind: Write(WriteError(WriteError { code: 11000,
65+
code_name: None, message: "E11000 duplicate key error collection:
66+
db.test_coll index: _id_ dup key: { _id: 1 }", details: None })), labels:
67+
{}, wire_version: None, source: None }
68+
69+
In the preceding error message, the value of the ``kind`` field is
70+
``Write``. To learn more about this type of error, see the :ref:`Write
71+
Error Types <rust-error-write-errors>` section of this guide.
72+
73+
Connection Errors
74+
-----------------
75+
76+
A concurrent operation error might clear the connection pool,
77+
interrupting your connection to the server. In this
78+
situation, the driver raises an ``Error`` type where the value of the
79+
``kind`` field is ``ConnectionPoolCleared``. The error message describes
80+
the reason that the concurrent operation failed.
81+
82+
To learn how to adjust your connection pool to address this error, see
83+
:manual:`Tuning Your Connection Pool Settings
84+
</tutorial/connection-pool-performance-tuning/>` in the Server manual.
85+
86+
Depending on the circumstances that produce the error, the driver may
87+
add a ``RetryableWriteError`` label to the error, as shown in the
88+
following error message:
89+
90+
.. code-block:: none
91+
:copyable: false
92+
:emphasize-lines: 3
93+
94+
Error { kind: ConnectionPoolCleared { message: "Connection pool for
95+
localhost:27017 cleared because another operation failed with: Kind:
96+
I/O error: timed out, labels: {}" }, labels: {"RetryableWriteError"},
97+
wire_version: None, source: None }
98+
99+
This label indicates that the error is write-retryable, which means that
100+
the driver makes one retry attempt.
101+
102+
.. _rust-error-write-errors:
103+
104+
Write Error Types
105+
-----------------
106+
107+
When the driver experiences an error while performing a write operation,
108+
it raises an ``Error`` instance with a ``kind`` field value of ``Write``.
109+
The body of the ``Write`` variant is the enum `WriteFailure
110+
<{+api+}/error/enum.WriteFailure.html>`__, which
111+
takes a value of type `WriteError <{+api+}/error/struct.WriteError.html>`__ or
112+
`WriteConcernError <{+api+}/error/struct.WriteConcernError.html>`__.
113+
114+
Write Concern Error
115+
~~~~~~~~~~~~~~~~~~~
116+
117+
The driver raises a ``WriteConcernError`` error when you perform a write
118+
operation and the driver cannot satisfy the specified write concern. For
119+
example, if you specify a write concern of ``majority`` for
120+
operations on a replica set with three nodes, the driver returns
121+
this error if the write operation propagates only to one node.
122+
123+
To learn more about write concerns, see :manual:`Write Concern
124+
</reference/write-concern/>` in the Server manual.
125+
126+
Write Error
127+
~~~~~~~~~~~
128+
129+
The driver raises a ``WriteError`` error for any errors that it
130+
encounters when performing a write operation that are not related to
131+
satisfying the write concern. Because there are many causes for this
132+
error, the ``WriteError`` type contains fields that describe the type of
133+
write error and reason for the error.
134+
135+
For example, the driver raises a ``WriteError`` error if you attempt to
136+
insert a document into a collection that violates the collection's
137+
schema validation rules. Suppose the collection has a rule where the
138+
value of the ``quantity`` field must be an ``int`` type. If you
139+
attempt to insert a document where the value of ``quantity`` is
140+
``"three"``, the driver prints the following error message:
141+
142+
.. code-block:: none
143+
:copyable: false
144+
:emphasize-lines: 2-3
145+
146+
Error: Error { kind: Write(WriteError(WriteError { code: 121, code_name:
147+
None, message: "Document failed validation", details:
148+
Some(Document({"failingDocumentId": Int32(1), "details":
149+
Document({"operatorName": String("$jsonSchema"), "title":
150+
String("Numerical Validation"), "schemaRulesNotSatisfied":
151+
Array(...)})})) })), labels: {},
152+
wire_version: None, source: None }
153+
154+
In the preceding error message, the ``message`` field describes the
155+
reason for the error, and the ``details`` field provides specific
156+
details about the failing operation. To address this error, you must
157+
either revise the document to adhere to the schema validation rules or
158+
bypass validation.
159+
160+
.. TODO To learn more about schema validation, see <link>.

0 commit comments

Comments
 (0)