Skip to content

Commit 402ce2e

Browse files
committed
DOCSP30350 add exceptions not just errors to doc
1 parent 2076d6b commit 402ce2e

File tree

1 file changed

+61
-72
lines changed

1 file changed

+61
-72
lines changed
Lines changed: 61 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
.. _java-operation-errors:
22

3-
=============================
3+
============================
44
Troubleshooting Write Errors
5-
=============================
5+
============================
66

77
.. meta::
8-
:description: Understand how to handle operation errors in the MongoDB Java Sync Driver, including error types like WriteErrror and ConnectionPoolCleared.
8+
:description: Understand how to handle write exceptions in the MongoDB Java Sync Driver, including error types like WriteError and BulkWriteError.
99

1010
.. contents::
1111
:local:
@@ -16,14 +16,14 @@ Troubleshooting Write Errors
1616
Overview
1717
--------
1818

19-
This page describes write errors you might encounter when
19+
This page describes write exceptions you might encounter when
2020
using the {+driver-long+} to perform MongoDB write operations. Once you
21-
understand the types of write operation errors that the driver raises, you can take
21+
understand the types of write exceptions that the driver raises, you can take
2222
appropriate actions to either handle them or correct the error-causing code.
2323

2424
.. note::
2525

26-
This page addresses only write operation error handling. If you encounter
26+
This page addresses only write exception handling. If you encounter
2727
any other issues with MongoDB or the driver, visit the following
2828
resources:
2929

@@ -34,42 +34,29 @@ appropriate actions to either handle them or correct the error-causing code.
3434
- :community-forum:`MongoDB Community Forums </tag/java>` for questions,
3535
discussions, or general technical support
3636

37-
Write Error Types
38-
-----------------
37+
Write Error
38+
-----------
3939

4040
If the driver encounters an error while performing a write operation, it
41-
returns an error of the `WriteError <{+core-api+}/WriteError.html>`__ type.
42-
43-
The ``WriteError`` type contains the following methods:
44-
45-
- ``getCode``: returns the code associated with this error
46-
- ``getMessage``: returns the message associated with this error
47-
- ``getDetails``: returns a BSON Document with details associated with this
48-
error, which may be empty
49-
- ``getCategory``: returns the category of the write error as an
50-
`ErrorCategory <{+core-api+}/ErrorCategory.html>`__ object
41+
creates an error of the `WriteError <{+core-api+}/WriteError.html>`__ type.
5142

52-
The `BulkWriteError <{+core-api+}/bulk/BulkWriteError.html>`__ type extends
53-
``WriteError`` and inherits the above methods. This type is used for bulk
54-
operations and includes an ``index`` field that identifies the item in the bulk
55-
operation that caused the error.
43+
The ``WriteError`` type contains the following fields:
5644

57-
The `WriteConcernError <{+core-api+}/bulk/WriteConcernError.html>`__ details
58-
errors related to the :manual:`write concern </reference/write-concern/>`. It
59-
does not extend the ``WriteError`` type, but has the same methods, except for
60-
`getCategory`. In addition, ``WriteConcernError`` has the ``getCodeName``
61-
method, which returns the name associated with the error code.
45+
- ``code``: the code associated with the error
46+
- ``message``: a message explaining the error
47+
- ``details``: an optional field containing details associated with the error
6248

63-
.. _java_error_write_errors:
49+
.. _java_error_write_exceptions:
6450

65-
Write Error
66-
~~~~~~~~~~~
51+
Write Exception
52+
---------------
6753

68-
The driver raises a ``WriteError`` error for any errors that it
69-
encounters when performing single write operations that are not related to
70-
satisfying the write concern.
54+
The driver raises a `MongoWriteException <{+core-api+}/MongoWriteException.html>` for
55+
any errors that it encounters when performing single write operations that are
56+
not related to satisfying the write concern. The ``WriteException`` type
57+
contains the ``WriteError`` object and a ``ServerAddress`` object.
7158

72-
For example, the driver raises a ``WriteError`` error if you attempt to
59+
For example, the driver raises a ``MongoWriteException`` if you attempt to
7360
insert a document into a collection that violates the collection's
7461
schema validation rules. Suppose the collection has a rule where the
7562
value of the ``quantity`` field must be an ``int`` type. If you
@@ -78,44 +65,46 @@ attempt to insert a document where the value of ``quantity`` is
7865

7966
.. code-block:: none
8067
:copyable: false
81-
82-
WriteError{ code=121, message='Document failed validation',
83-
details=Document{{operator=$jsonSchema, schema={type=object,
84-
required=["name", "age"], properties={name={type=string}, age={type=int,
85-
minimum=18}}}} }
86-
}
87-
88-
In the preceding error message, the ``message`` field describes the reason for
89-
the error, and the ``details`` field provides specific details about the failing
90-
operation, in this case it provides the schema rules. To address this error, you
91-
must either revise the document to adhere to the schema validation rules or
92-
bypass validation.
93-
94-
Bulk Write Error
95-
----------------
96-
97-
If the same example were run as a bulk write operation, the ``index`` field
98-
containing the index of the erring item would be added to the message. For
99-
example, if the ``index`` of the item that violated the schema is ``2``, the driver
100-
would print the following error message:
68+
:emphasize-lines: 1, 4-6
69+
70+
Exception in thread "main" com.mongodb.MongoWriteException: Document failed validation at
71+
com.mongodb.internal.connection.ProtocolHelper.getWriteException(ProtocolHelper.java:228)
72+
...
73+
Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
74+
message='Document failed validation', details={ operator: "$jsonSchema",
75+
schemaRules: { bsonType: "int", description: "must be an integer" },
76+
offendingDocument: {"name":"Apple","quantity":"three"} } } at
77+
com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
78+
79+
In the preceding error message, the ``MongoWriteException`` object describes the
80+
reason for the error, and the ``Caused by`` field provides details from the
81+
``WriteError`` object about the failing operation. To address this error, you must
82+
either revise the document to adhere to the schema validation rules or bypass
83+
validation.
84+
85+
Bulk Write Exception
86+
--------------------
87+
88+
If the same example were run as a bulk write operation, a
89+
`MongoBulkWriteException <{+core-api+}/MongoBulkWriteException.html>`__ would be
90+
thrown. A ``MongoBulkWriteException`` can contain a list of errors associated
91+
with the same bulk write operation. For example, if you attempt to insert two
92+
documents that violate the collection's schema, the error message would appear
93+
as follows:
10194

10295
.. code-block:: none
10396
:copyable: false
104-
:emphasize-lines: 1
105-
106-
BulkWriteError{ index=2, code=121, message='Document failed validation',
107-
details=Document{{operator=$jsonSchema, schema={type=object,
108-
required=["name", "age"], properties={name={type=string}, age={type=int,
109-
minimum=18}}}} }
110-
111-
Write Concern Error
112-
~~~~~~~~~~~~~~~~~~~
113-
114-
The driver raises a ``WriteConcernError`` error when you perform a write
115-
operation and the driver cannot satisfy the specified write concern. For
116-
example, if you specify a write concern of ``majority`` for
117-
operations on a replica set with three nodes, the driver returns
118-
this error if the write operation propagates only to one node.
119-
120-
To learn more about write concerns, see :manual:`Write Concern
121-
</reference/write-concern/>` in the Server manual.
97+
:emphasize-lines: 1, 5, 9
98+
99+
Exception in thread "main" com.mongodb.MongoBulkWriteException: Bulk write operation result had errors
100+
at com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:258)
101+
...
102+
at BulkWriteMultipleValidationErrorsExample.main(BulkWriteMultipleValidationErrorsExample.java:30)
103+
Caused by: com.mongodb.MongoWriteException: WriteError{code=121, message='Document failed validation', details={ operator: "$jsonSchema", schemaRules: { bsonType: "int", description: "must be an integer" }, offendingDocument: {"name":"Apple","quantity":"three"} }}
104+
at com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
105+
at com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
106+
... 19 more
107+
Caused by: com.mongodb.MongoWriteException: WriteError{code=121, message='Document failed validation', details={ operator: "$jsonSchema", schemaRules: { bsonType: "int", description: "must be an integer" }, offendingDocument: {"name":"Banana","quantity":"ten"} }}
108+
at com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
109+
at com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
110+
... 19 more

0 commit comments

Comments
 (0)