-
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 9 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import Foundation | ||
|
|
||
| /// An empty protocol for encapsulating all errors that BSON package can throw. | ||
| public protocol BSONErrorProtocol: LocalizedError {} | ||
|
|
||
| /// A protocol describing errors caused by improper usage of the BSON library by the user. | ||
| public protocol BSONUserError: BSONErrorProtocol {} | ||
patrickfreed marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// The possible errors that can occur unexpectedly BSON library-side. | ||
| public protocol BSONRuntimeError: BSONErrorProtocol {} | ||
|
|
||
| /// Namespace containing all the error types introduced by this BSON library and their dependent types. | ||
| public enum BSONError { | ||
| /// An error thrown when the user passes in invalid arguments to a BSON method. | ||
| public struct InvalidArgumentError: BSONUserError { | ||
| internal let message: String | ||
|
|
||
| public var errorDescription: String? { self.message } | ||
| } | ||
|
|
||
| /// An error thrown when the BSON library encounters a internal error not caused by the user. | ||
| /// This is usually indicative of a bug in the BSON library or system related failure. | ||
| public struct InternalError: BSONRuntimeError { | ||
| internal let message: String | ||
|
|
||
| public var errorDescription: String? { self.message } | ||
| } | ||
|
|
||
| /// An error thrown when the BSON library is incorrectly used. | ||
| public struct LogicError: BSONUserError { | ||
| internal let message: String | ||
|
|
||
| public var errorDescription: String? { self.message } | ||
| } | ||
| } | ||
|
|
||
| internal func bsonTooLargeError(value: BSONValue, forKey: String) -> BSONErrorProtocol { | ||
patrickfreed marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| BSONError.InternalError( | ||
| message: | ||
| "Failed to set value for key \(forKey) to \(value) with BSON type \(value.bsonType): document too large" | ||
| ) | ||
| } | ||
|
|
||
| internal func wrongIterTypeError(_ iter: BSONDocumentIterator, expected type: BSONValue.Type) -> BSONErrorProtocol { | ||
| BSONError.LogicError( | ||
| message: "Tried to retreive a \(type) from an iterator whose next type " + | ||
| "is \(iter.currentType) for key \(iter.currentKey)" | ||
| ) | ||
| } | ||
|
|
||
| /// Error thrown when a BSONValue type introduced by the driver (e.g. BSONObjectID) is encoded not using BSONEncoder | ||
| internal func bsonEncodingUnsupportedError<T: BSONValue>(value: T, at codingPath: [CodingKey]) -> EncodingError { | ||
| let description = "Encoding \(T.self) BSONValue type with a non-BSONEncoder is currently unsupported" | ||
|
|
||
| return EncodingError.invalidValue( | ||
| value, | ||
| EncodingError.Context(codingPath: codingPath, debugDescription: description) | ||
| ) | ||
| } | ||
|
|
||
| /// Error thrown when a BSONValue type introduced by the driver (e.g. BSONObjectID) is decoded not using BSONDecoder | ||
| internal func bsonDecodingUnsupportedError<T: BSONValue>(type _: T.Type, at codingPath: [CodingKey]) -> DecodingError { | ||
| let description = "Initializing a \(T.self) BSONValue type with a non-BSONDecoder is currently unsupported" | ||
|
|
||
| return DecodingError.typeMismatch( | ||
| T.self, | ||
| DecodingError.Context(codingPath: codingPath, debugDescription: description) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Error thrown when a `BSONValue` type introduced by the driver (e.g. BSONObjectID) is decoded directly via the | ||
| * top-level `BSONDecoder`. | ||
| */ | ||
| internal func bsonDecodingDirectlyError<T: BSONValue>(type _: T.Type, at codingPath: [CodingKey]) -> DecodingError { | ||
| let description = "Cannot initialize BSONValue type \(T.self) directly from BSONDecoder. It must be decoded as " + | ||
| "a member of a struct or a class." | ||
|
|
||
| return DecodingError.typeMismatch( | ||
| T.self, | ||
| DecodingError.Context(codingPath: codingPath, debugDescription: description) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * This function determines which error to throw when a driver-introduced BSON type is decoded via its init(decoder). | ||
| * The types that use this function are all BSON primitives, so they should be decoded directly in `_BSONDecoder`. If | ||
| * execution reaches their decoding initializer, it means something went wrong. This function determines an appropriate | ||
| * error to throw for each possible case. | ||
| * | ||
| * Some example cases: | ||
| * - Decoding directly from the BSONDecoder top-level (e.g. BSONDecoder().decode(BSONObjectID.self, from: ...)) | ||
| * - Encountering the wrong type of BSONValue (e.g. expected "_id" to be an `BSONObjectID`, got a `BSONDocument` | ||
| * instead) | ||
| * - Attempting to decode a driver-introduced BSONValue with a non-BSONDecoder | ||
| */ | ||
| internal func getDecodingError<T: BSONValue>(type _: T.Type, decoder: Decoder) -> DecodingError { | ||
| if let bsonDecoder = decoder as? _BSONDecoder { | ||
| // Cannot decode driver-introduced BSONValues directly | ||
| if decoder.codingPath.isEmpty { | ||
| return bsonDecodingDirectlyError(type: T.self, at: decoder.codingPath) | ||
| } | ||
|
|
||
| // Got the wrong BSONValue type | ||
| return DecodingError._typeMismatch( | ||
| at: decoder.codingPath, | ||
| expectation: T.self, | ||
| reality: bsonDecoder.storage.topContainer.bsonValue | ||
| ) | ||
| } | ||
|
|
||
| // Non-BSONDecoders are currently unsupported | ||
| return bsonDecodingUnsupportedError(type: T.self, at: decoder.codingPath) | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * BSONUtil contains helpers to wrap the underlying BSON library to assist in providing a consistent API | ||
| */ | ||
|
|
||
| /// We don't want driver users to handle any BSONErrors | ||
| /// this will convert BSONError.* thrown from `fn` to MongoError.* and rethrow | ||
| internal func convertingBSONErrors<T>(_ fn: () throws -> T) rethrows -> T { | ||
kmahar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| do { | ||
| return try fn() | ||
| } catch let error as BSONError.InvalidArgumentError { | ||
kmahar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw MongoError.InvalidArgumentError(message: error.message) | ||
| } catch let error as BSONError.InternalError { | ||
| throw MongoError.InternalError(message: error.message) | ||
| } catch let error as BSONError.LogicError { | ||
| throw MongoError.LogicError(message: error.message) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.