You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Guides/Error-Handling.md
+23-23Lines changed: 23 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,55 +15,55 @@
15
15
*[See Also](#see-also)
16
16
17
17
## Error Types
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
+
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 (`MongoServerError`, `MongoUserError`, `MongoRuntimeError`), each of which are protocols that inherit from the `MongoErrorProtocol` 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.
19
19
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.
23
23
24
24
For an enumeration of the possible server error codes, [see this list](https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.yml).
25
25
26
-
The possible errors that conform to `ServerError` are as follows:
27
-
-`CommandError`:
26
+
The possible errors that conform to `MongoServerError` are as follows:
27
+
-`MongoError.CommandError`:
28
28
- Thrown when commands experience errors server side that prevent execution.
29
29
- Example command failures include failure to parse, operation aborted by the user, and unexpected errors during execution.
30
-
-`WriteError`
30
+
-`MongoError.WriteError`
31
31
- Thrown when a single write command fails on the server (e.g. insertOne, updateOne, updateMany).
32
-
-`BulkWriteError`
32
+
-`MongoError.BulkWriteError`
33
33
- Thrown when the server returns errors as part of an executed bulk write.
34
34
- If WriteConcernFailure is populated, writeErrors may not be.
35
-
-**Note:**`InsertMany` throws a `BulkWriteError`, _not_ a `WriteError`.
35
+
-**Note:**`InsertMany` throws a `MongoError.BulkWriteError`, _not_ a `MongoError.WriteError`.
36
36
37
37
38
38
### User Errors
39
39
User applications can sometimes cause errors by using the driver incorrectly (e.g. by passing invalid argument combinations). This category of error covers those cases.
40
40
41
-
The possible errors that conform to `UserError` are as follows:
42
-
-`LogicError`
41
+
The possible errors that conform to `MongoUserError` are as follows:
42
+
-`MongoError.LogicError`
43
43
- Thrown when the user uses the driver incorrectly (e.g. advancing a dead cursor).
44
-
-`InvalidArgumentError`
44
+
-`MongoError.InvalidArgumentError`
45
45
- Thrown when user passes invalid arguments to some driver function.
46
46
47
47
48
48
### Runtime Errors
49
49
The driver may experience errors that happen at runtime unexpectedly. These errors don't fit neatly into the categories of occurring only server-side or only as part of the user's fault, so they are represented by their own set of cases.
50
50
51
-
The `RuntimeError` cases are as follows:
52
-
-`InternalError`
51
+
The `MongoRuntimeError` cases are as follows:
52
+
-`MongoError.InternalError`
53
53
- Thrown when something is null when it shouldn't be, the driver has an internal failure, or MongoSwift cannot understand a server response.
54
54
- This is generally indicative of a bug somewhere in the driver stack or a system related failure (e.g. a memory allocation failure). If you experience an error that you think is the result of a bug, please file a bug report on GitHub or our Jira project.
55
-
-`ConnectionError`
55
+
-`MongoError.ConnectionError`
56
56
- Thrown during any connection establishment / socket related errors.
57
57
- This error also conforms to `LabeledError`.
58
-
-`AuthenticationError`
58
+
-`MongoError.AuthenticationError`
59
59
- Thrown when the driver is not authorized to perform a requested command (e.g. due to invalid credentials)
60
-
-`ServerSelectionError`
60
+
-`MongoError.ServerSelectionError`
61
61
- Thrown when the driver was unable to select a server for an operation (e.g. due to a timeout or unsatisfiable read preference)
62
62
- See [the official MongoDB documentation](https://docs.mongodb.com/manual/core/read-preference-mechanics/) for more information.
63
63
64
64
65
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.
66
+
Some types of errors may contain more specific information describing the context in which they occured. Such errors conform to the `MongoLabeledError` protocol, and the extra information is conveyed through the `errorLabels` property. Specifically, any server error or connection related error may contain labels.
67
67
68
68
The following error labels are currently defined. Future versions of MongoDB may introduce new labels:
69
69
-`TransientTransactionError`:
@@ -73,7 +73,7 @@ The following error labels are currently defined. Future versions of MongoDB may
73
73
74
74
75
75
### Encoding/Decoding Errors
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.
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 `MongoErrorProtocol` protocol or any of the other error protocols defined in the driver.
77
77
78
78
See the official documentation for both [`EncodingErrors`](https://developer.apple.com/documentation/swift/encodingerror) and [`DecodingErrors`](https://developer.apple.com/documentation/swift/decodingerror) for more information.
79
79
@@ -83,14 +83,14 @@ See the official documentation for both [`EncodingErrors`](https://developer.app
0 commit comments