-
Notifications
You must be signed in to change notification settings - Fork 67
SWIFT-878: Define BSON Specific Error Types #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
341f664
SWIFT-878: Create BSON Specific Error Types
nbbeeken f23fd7a
fix: re-export new errors
nbbeeken 3a5b4d7
fix: tests
nbbeeken cdb0c7a
fix: docs
nbbeeken 21c3652
fix: docs
nbbeeken 7a26ccd
wip
nbbeeken 6827c0f
wip
nbbeeken 50c4500
wip
nbbeeken 94d6116
wip
nbbeeken 0ce1e93
wip
nbbeeken 1a38c8f
wip
nbbeeken 390d05d
formatting
nbbeeken 3a7f9fe
wip
nbbeeken caced54
wip
nbbeeken 1725e73
wip
nbbeeken 277a1d1
wip
nbbeeken 702f30b
formatting
nbbeeken 30fe386
wip
nbbeeken b90767c
wip
nbbeeken cd45de9
wip
nbbeeken 156ad65
wip
nbbeeken 4b960ba
wip
nbbeeken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,7 @@ | |
| * [See Also](#see-also) | ||
|
|
||
| ## Error Types | ||
| 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. | ||
|
|
||
| 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 categories 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. | ||
|
|
||
| ### Server Errors | ||
| 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. | ||
|
|
@@ -77,6 +76,14 @@ As part of the driver, `BSONEncoder` and `BSONDecoder` are implemented according | |
|
|
||
| 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. | ||
|
|
||
| ### BSON Errors | ||
|
|
||
| The BSON library has its own subset of errors that communicate issues when constructing or using BSON. | ||
| BSON Errors can be found in [Sources/MongoSwift/BSON/BSONError.swift](Sources/MongoSwift/BSON/BSONError.swift) and are as follows: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I didn't know you could link like this on GitHub, very cool. |
||
|
|
||
| - `BSONError.InvalidArgumentError` - This error is thrown when a BSON type is being incorrectly constructed. | ||
patrickfreed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - `BSONError.InternalError` - This error is thrown when there is an issue that is a result of system failure (e.g, allocation issue). | ||
| - `BSONError.LogicError` - This error is thrown when there is an unexpected usage of the the API. | ||
|
|
||
| ## Examples | ||
| ### Handling any error thrown by the driver | ||
|
|
@@ -154,6 +161,24 @@ Result: | |
| nInserted: 1 | ||
| InsertedIds: [0: 2] | ||
| ``` | ||
|
|
||
| ### Handling a BSONError | ||
|
|
||
| ```swift | ||
| var string = "+1..23e8" | ||
| do { | ||
| return try BSONDecimal128(string) | ||
| } catch let bsonError as BSONError.InvalidArgumentError { | ||
| print(bsonError.message) | ||
| } | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ```plain | ||
| Invalid Decimal128 string +1..23e8 | ||
| ``` | ||
|
|
||
| ## See Also | ||
| - [Error handling in Swift](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) | ||
| - [List of server error codes](https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.err) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hard to pick out but this was just:
caregories->categories