Skip to content

Conversation

@patrickfreed
Copy link
Contributor

@patrickfreed patrickfreed commented May 28, 2020

SWIFT-851

This PR moves all the error type definitions under a new empty enum MongoError.
It also renames MongoError to MongoErrorProtocol and prefixes the other error protocols with Mongo.

/// A MongoDB server error code.
/// - SeeAlso: https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.yml
public typealias ServerErrorCode = Int
public protocol MongoErrorProtocol: LocalizedError {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to disambiguate from the namespace, I added the Protocol suffix that I've seen in a few places in the stdlib (e.g. LazySequenceProtocol)


/// Protocol conformed to by errors that may contain error labels.
public protocol LabeledError: MongoError {
public protocol MongoLabeledError: MongoErrorProtocol {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are limitations to this enum-as-namespace approach, namely protocols can't be defined that way. To avoid collisions, I added the Mongo prefix to all these protocols.

/// Labels that may describe the context in which this error was thrown.
public let errorLabels: [String]?
/// Thrown when commands experience errors on the server that prevent execution.
public struct CommandError: MongoServerError {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could even do sub-namespaces here for each of the error categories (e.g. MongoError.ServerError.CommandError), but that felt like overkill.

public var errorDescription: String? { self.message }
}
/// A struct to represent a single write error not resulting from an executed write operation.
public struct WriteFailure: Codable {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these -Failure types aren't errors themselves, but they are contained within different error types. I wasn't sure where to put them so I just left them under MongoError.

@patrickfreed patrickfreed marked this pull request as ready for review May 28, 2020 20:22
@patrickfreed patrickfreed requested review from kmahar and nbbeeken May 28, 2020 20:22
@codecov-commenter
Copy link

codecov-commenter commented May 28, 2020

Codecov Report

Merging #491 into master will decrease coverage by 0.01%.
The diff coverage is 50.17%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #491      +/-   ##
==========================================
- Coverage   76.99%   76.98%   -0.02%     
==========================================
  Files         125      125              
  Lines       13030    13050      +20     
==========================================
+ Hits        10033    10046      +13     
- Misses       2997     3004       +7     
Impacted Files Coverage Δ
Sources/MongoSwift/APM.swift 83.54% <ø> (ø)
Sources/MongoSwift/BSON/BSONDecoder.swift 65.50% <0.00%> (-0.68%) ⬇️
Sources/MongoSwift/BSON/BSONDocumentIterator.swift 89.53% <0.00%> (ø)
Sources/MongoSwift/BSON/Overwritable.swift 100.00% <ø> (ø)
Sources/MongoSwift/ChangeStream.swift 30.64% <0.00%> (ø)
Sources/MongoSwift/ClientSession.swift 39.30% <0.00%> (ø)
Sources/MongoSwift/ConnectionString.swift 69.67% <0.00%> (ø)
Sources/MongoSwift/CursorCommon.swift 95.12% <ø> (ø)
Sources/MongoSwift/MongoClient.swift 87.09% <0.00%> (ø)
...ces/MongoSwift/MongoCollection+ChangeStreams.swift 43.75% <ø> (ø)
... and 61 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 ba1825d...beaac75. 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.

what you've got here lgtm mod maybe handling the BSON stuff here, but I think probably all of our docstrings listing error types need to be updated now?

@patrickfreed patrickfreed force-pushed the SWIFT-851/namespace-errors branch from 4908f6c to beaac75 Compare May 29, 2020 18:04
@patrickfreed patrickfreed merged commit 24de514 into mongodb:master May 29, 2020
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.

4 participants