Skip to content

Conversation

@nbbeeken
Copy link
Contributor

https://jira.mongodb.org/browse/SWIFT-878

Pretty much mirrors MongoError but with BSON. In order to nicely 🎣 catch and release errors I pulled out wrapper functions into a BSONUtil.swift file.

@nbbeeken nbbeeken requested review from kmahar and patrickfreed May 29, 2020 19:14
@codecov-commenter
Copy link

codecov-commenter commented May 29, 2020

Codecov Report

Merging #492 into master will increase coverage by 0.09%.
The diff coverage is 48.69%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #492      +/-   ##
==========================================
+ Coverage   76.98%   77.07%   +0.09%     
==========================================
  Files         125      128       +3     
  Lines       13050    13513     +463     
==========================================
+ Hits        10046    10415     +369     
- Misses       3004     3098      +94     
Impacted Files Coverage Δ
Sources/MongoSwift/MongoError.swift 63.18% <ø> (+2.31%) ⬆️
Sources/MongoSwift/BSON/BSONValue.swift 78.50% <16.21%> (+2.60%) ⬆️
Sources/MongoSwift/BSON/BSONError.swift 24.44% <24.44%> (ø)
Sources/MongoSwift/BSON/BSONDecoder.swift 64.71% <30.00%> (-0.79%) ⬇️
Sources/MongoSwift/BSON/BSONUtil.swift 30.76% <30.76%> (ø)
Sources/MongoSwift/BSON/BSONDocument.swift 88.36% <33.33%> (ø)
Sources/MongoSwift/BSON/BSONDocumentIterator.swift 89.53% <66.66%> (ø)
Sources/MongoSwift/BSON/BSONEncoder.swift 64.49% <74.35%> (-0.13%) ⬇️
Sources/MongoSwift/BSON/Overwritable.swift 100.00% <100.00%> (ø)
Sources/MongoSwift/MongoCollection+BulkWrite.swift 86.36% <100.00%> (+0.73%) ⬆️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 24de514...4b960ba. Read the comment docs.

Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks pretty good! mainly have some comments based on actually reading through these docstrings and realizing a good number are outdated 😳
I also think we should add a section in the error handling guide about these errors.

Comment on lines 777 to 787
// This is unused
fileprivate var bson: BSON { .document(self.toDocument()) }
fileprivate var bson: BSON { .document((try? self.toDocument()) ?? [:]) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used in testing, not sure what to do here since prop getters can't throw, do y'all think this is fine? maybe I can add a comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think that comment is inaccurate, I would imagine we use this anytime we have a nested struct within another struct!
I don't think we should swallow the error though, maybe in toDocument you can special-case when self.values[i] is a MutableDictionary and in that case directly call toDocument rather than .bson?
we probably also need to special case like that in MutableArray since it calls .bson and the array could have a MutableDictionary stored in it

Comment on lines 777 to 787
// This is unused
fileprivate var bson: BSON { .document(self.toDocument()) }
fileprivate var bson: BSON { .document((try? self.toDocument()) ?? [:]) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think that comment is inaccurate, I would imagine we use this anytime we have a nested struct within another struct!
I don't think we should swallow the error though, maybe in toDocument you can special-case when self.values[i] is a MutableDictionary and in that case directly call toDocument rather than .bson?
we probably also need to special case like that in MutableArray since it calls .bson and the array could have a MutableDictionary stored in it

Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also still need to handle the symmetric error issue in the decoder I mentioned before.

Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 comment on existing code. otherwise we still need the decoder changes to catch the errors as well as updates to the error handling guide to talk about BSON errors.

Comment on lines 534 to 536
private func getValue(forKey key: Key) throws -> BSON {
guard let entry = try self.container.getValue(for: key.stringValue) else {
guard let entry = (try convertingBSONErrors { try self.container.getValue(for: key.stringValue) }) else {
throw DecodingError.keyNotFound(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the only place where we'd have an issue, but I'm suspicious 🤔 sanity check?

## 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.
Copy link
Contributor Author

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

@kmahar
Copy link
Contributor

kmahar commented Jun 2, 2020

whoops I don't know why that comment posted like that, but 🤷‍♀️

Copy link
Contributor

@patrickfreed patrickfreed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall looks good, I have a few minor API suggestions (cc @kmahar on both)

### 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:
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

@patrickfreed patrickfreed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good, just some docs suggestions

@nbbeeken nbbeeken merged commit 7e77a3d into master Jun 3, 2020
@nbbeeken nbbeeken deleted the SWIFT-878/bson-errors branch June 3, 2020 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants