11.. _java-operation-errors:
22
3- ============================
4- Troubleshooting Write Errors
5- ============================
3+ ================================
4+ Troubleshooting Write Exceptions
5+ ================================
66
77.. meta::
88 :description: Understand how to handle write exceptions in the MongoDB Java Sync Driver, including error types like WriteError and BulkWriteError.
@@ -35,7 +35,7 @@ appropriate actions to either handle them or correct the error-causing code.
3535 discussions, or general technical support
3636
3737Write Error
38- -----------
38+ ~~~~~~~~~~~
3939
4040If the driver encounters an error while performing a write operation, it
4141creates an error of the `WriteError <{+core-api+}/WriteError.html>`__ type.
@@ -51,10 +51,10 @@ The ``WriteError`` type contains the following fields:
5151Write Exception
5252---------------
5353
54- The driver raises a `MongoWriteException <{+core-api+}/MongoWriteException.html>` for
54+ The driver raises a `MongoWriteException <{+core-api+}/MongoWriteException.html>`__ for
5555any 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 .
56+ not related to satisfying the write concern. The ``MongoWriteException `` type
57+ contains the ``WriteError`` object.
5858
5959For example, the driver raises a ``MongoWriteException`` if you attempt to
6060insert a document into a collection that violates the collection's
@@ -65,7 +65,7 @@ attempt to insert a document where the value of ``quantity`` is
6565
6666.. code-block:: none
6767 :copyable: false
68- :emphasize-lines: 1, 4-6
68+ :emphasize-lines: 1, 4-7
6969
7070 Exception in thread "main" com.mongodb.MongoWriteException: Document failed validation at
7171 com.mongodb.internal.connection.ProtocolHelper.getWriteException(ProtocolHelper.java:228)
@@ -94,17 +94,25 @@ as follows:
9494
9595.. code-block:: none
9696 :copyable: false
97- :emphasize-lines: 1, 5, 9
97+ :emphasize-lines: 1-2, 6-9, 13-16
9898
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)
99+ Exception in thread "main" com.mongodb.MongoBulkWriteException: Bulk write
100+ operation result had errors at
101+ com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:258)
102+ ... at
103+ BulkWriteMultipleValidationErrorsExample.main(BulkWriteMultipleValidationErrorsExample.java:30)
104+ Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
105+ message='Document failed validation', details={ operator: "$jsonSchema",
106+ schemaRules: { bsonType: "int", description: "must be an integer" },
107+ offendingDocument: {"name":"Apple","quantity":"three"} }} at
108+ com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
109+ at
110+ com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
111+ ... 19 more Caused by: com.mongodb.MongoWriteException: WriteError{code=121,
112+ message='Document failed validation', details={ operator: "$jsonSchema",
113+ schemaRules: { bsonType: "int", description: "must be an integer" },
114+ offendingDocument: {"name":"Banana","quantity":"ten"} }} at
115+ com.mongodb.internal.connection.WriteResultHelper.createWriteException(WriteResultHelper.java:50)
116+ at
117+ com.mongodb.internal.connection.ProtocolHelper.getBulkWriteException(ProtocolHelper.java:254)
110118 ... 19 more
0 commit comments