|
5 | 5 | * [Server Errors](#server-errors) |
6 | 6 | * [User Errors](#user-errors) |
7 | 7 | * [Runtime Errors](#runtime-errors) |
| 8 | + * [Error Labels](#error-labels) |
8 | 9 | * [Encoding/Decoding Errors](#encoding-decoding-errors) |
9 | 10 | * [Examples](#the-code) |
10 | 11 | * [All Errors](#handling-any-error-thrown-by-the-driver) |
|
16 | 17 | ## Error Types |
17 | 18 | The driver uses errors to communicate that an operation failed, an assumption wasn't met, or that the user did something incorrectly. Applications that use the driver can in turn catch these errors and respond appropriately without crashing or resulting in an otherwise inconsistent state. To correctly model the different sources of errors, the driver defines three separate caregories of errors (`ServerError`, `UserError`, `RuntimeError`), each of which are protocols that inherit from the `MongoError` protocol. These protocols are defined in `MongoError.swift`, and the structs that conform to them are outlined here. The documentation for every public function that throws lists some of the errors that could possibly be thrown and the reasons they might be. The errors listed there are not comprehensive but will generally cover the most common cases. |
18 | 19 |
|
19 | | -**Error Labels:** Some types of errors may contain more specific information describing the context in which they occured. Such errors conform to the `LabeledError` protocol, and the extra information is conveyed through the `errorLabels` property. Specifically, any server error or connection related error may contain labels. |
20 | 20 |
|
21 | 21 | ### Server Errors |
22 | 22 | Server errors correspond to failures that occur in the database itself and are returned to the driver via some response to a command. Each error that conforms to `ServerError` contains at least one error code representing what went wrong on the server. |
@@ -62,6 +62,16 @@ The `RuntimeError` cases are as follows: |
62 | 62 | - See [the official MongoDB documentation](https://docs.mongodb.com/manual/core/read-preference-mechanics/) for more information. |
63 | 63 |
|
64 | 64 |
|
| 65 | +### Error Labels |
| 66 | +Some types of errors may contain more specific information describing the context in which they occured. Such errors conform to the `LabeledError` protocol, and the extra information is conveyed through the `errorLabels` property. Specifically, any server error or connection related error may contain labels. |
| 67 | + |
| 68 | +The following error labels are currently defined. Future versions of MongoDB may introduce new labels: |
| 69 | +- `TransientTransactionError`: |
| 70 | + - Within a multi-document transaction, certain errors can leave the transaction in an unknown or aborted state. These include write conflicts, primary stepdowns, and network errors. In response, the application should abort the transaction and try the same sequence of operations again in a new transaction. |
| 71 | +- `UnknownTransactionCommitResult`: |
| 72 | + - When `commitTransaction()` encounters a network error or certain server errors, it is not known whether the transaction was committed. Applications should attempt to commit the transaction again until (i) the commit succeeds, (ii) the commit fails with an error *not* labeled `UnknownTransactionCommitResult`, or (iii) the application chooses to give up. |
| 73 | + |
| 74 | + |
65 | 75 | ### Encoding/Decoding Errors |
66 | 76 | As part of the driver, `BSONEncoder` and `BSONDecoder` are implemented according to the `Encoder` and `Decoder` protocols [defined in Apple's Foundation](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types). User applications can use them to seamlessly convert between their Swift data structures and the BSON documents stored in the database. While this functionality is part of the public API, the driver itself also makes heavy use of it internally. During any encoding or decoding operations, errors can occur that prevent the data from being written to or read from BSON. In these cases, the driver throws an `EncodingError` or `DecodingError` as appropriate. These error types are not unique to MongoSwift and are commonly used by other encoder implementations, such as Foundation's `JSONEncoder`, so they do not conform to the `MongoError` protocol or any of the other error protocols defined in the driver. |
67 | 77 |
|
|
0 commit comments