|
| 1 | +.. _java-operation-errors: |
| 2 | + |
| 3 | +============================== |
| 4 | +Write Operation Error Handling |
| 5 | +============================== |
| 6 | + |
| 7 | +.. meta:: |
| 8 | + :description: Understand how to handle operation errors in the MongoDB Java Sync Driver, including error types like WriteErrror and ConnectionPoolCleared. |
| 9 | + |
| 10 | +.. contents:: |
| 11 | + :local: |
| 12 | + :backlinks: none |
| 13 | + :depth: 2 |
| 14 | + :class: singlecol |
| 15 | + |
| 16 | +Overview |
| 17 | +-------- |
| 18 | + |
| 19 | +This page describes write errors you might encounter when |
| 20 | +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 |
| 22 | +appropriate actions to either handle them or correct the error-causing code. |
| 23 | + |
| 24 | +.. note:: |
| 25 | + |
| 26 | + This page addresses only write operation error handling. If you encounter |
| 27 | + any other issues with MongoDB or the driver, visit the following |
| 28 | + resources: |
| 29 | + |
| 30 | + - :ref:`java-connection-troubleshooting` for potential solutions to |
| 31 | + issues you might encounter when connecting to a MongoDB deployment |
| 32 | + - :ref:`java-issues-and-help` page for information about reporting bugs, |
| 33 | + contributing to the driver, and finding more resources |
| 34 | + - :community-forum:`MongoDB Community Forums </tag/java>` for questions, |
| 35 | + discussions, or general technical support |
| 36 | + |
| 37 | +Write Error Types |
| 38 | +----------------- |
| 39 | + |
| 40 | +If the driver encounters an error while performing an 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. The |
| 51 | + enum ``ErrorCategory`` can be one of the following options: |
| 52 | + |
| 53 | + - ``DUPLICATE_KEY``: you provided duplicate key to a method |
| 54 | + - ``EXECUTION_TIMEOUT``: the driver timed out |
| 55 | + - ``UNCATEGORIZED``: the error has no category |
| 56 | + |
| 57 | +The `BulkWriteError<{+core-api+}/bulk/BulkWriteError.html>`__ type extends |
| 58 | +``WriteError`` and inherits the above methods. The `WriteConcernError |
| 59 | +<{+core-api+}/bulk/WriteConcernError.html>`__ is not related to the ``WriteError`` type. |
| 60 | + |
| 61 | +Write Error |
| 62 | +~~~~~~~~~~~ |
| 63 | + |
| 64 | +The driver raises a ``WriteError`` error for any errors that it |
| 65 | +encounters when performing a write operation that are not related to |
| 66 | +satisfying the write concern. Because there are many causes for this |
| 67 | +error, the ``WriteError`` type contains fields that describe the type of |
| 68 | +write error and reason for the error. |
| 69 | + |
| 70 | +For example, the driver raises a ``WriteError`` error if you attempt to |
| 71 | +insert a document into a collection that violates the collection's |
| 72 | +schema validation rules. Suppose the collection has a rule where the |
| 73 | +value of the ``quantity`` field must be an ``int`` type. If you |
| 74 | +attempt to insert a document where the value of ``quantity`` is |
| 75 | +``"three"``, the driver prints the following error message: |
| 76 | + |
| 77 | +.. code-block:: none |
| 78 | + :copyable: false |
| 79 | + :emphasize-lines: 2-3 |
| 80 | + |
| 81 | + Error: Error { kind: Write(WriteError(WriteError { code: 121, code_name: |
| 82 | + None, message: "Document failed validation", details: |
| 83 | + Some(Document({"failingDocumentId": Int32(1), "details": |
| 84 | + Document({"operatorName": String("$jsonSchema"), "title": |
| 85 | + String("Numerical Validation"), "schemaRulesNotSatisfied": |
| 86 | + Array(...)})})) })), labels: {}, |
| 87 | + wire_version: None, source: None } |
| 88 | + |
| 89 | +In the preceding error message, the ``message`` field describes the |
| 90 | +reason for the error, and the ``details`` field provides specific |
| 91 | +details about the failing operation. To address this error, you must |
| 92 | +either revise the document to adhere to the schema validation rules or |
| 93 | +bypass validation. |
| 94 | + |
| 95 | +Write Concern Error |
| 96 | +~~~~~~~~~~~~~~~~~~~ |
| 97 | + |
| 98 | +The driver raises a ``WriteConcernError`` error when you perform a write |
| 99 | +operation and the driver cannot satisfy the specified write concern. For |
| 100 | +example, if you specify a write concern of ``majority`` for |
| 101 | +operations on a replica set with three nodes, the driver returns |
| 102 | +this error if the write operation propagates only to one node. |
| 103 | + |
| 104 | +To learn more about write concerns, see :manual:`Write Concern |
| 105 | +</reference/write-concern/>` in the Server manual. |
0 commit comments