Skip to content

Commit

Permalink
refactor wrongIterTypeError, throw typeMismatch instead of dataCorrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickfreed committed Jan 25, 2019
1 parent 6f7f596 commit 7bfd8e7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
12 changes: 9 additions & 3 deletions Sources/MongoSwift/BSON/AnyBSONValue.swift
Expand Up @@ -92,9 +92,15 @@ public struct AnyBSONValue: Codable, Equatable, Hashable {
if let bsonDecoder = decoder as? _BSONDecoder {
if bsonDecoder.storage.topContainer is Date {
guard case .bsonDateTime = bsonDecoder.options.dateDecodingStrategy else {
throw DecodingError.dataCorruptedError(in: bsonDecoder, debugDescription: "Got a BSON datetime " +
"but was expecting another format. To decode from BSON datetimes, use the default" +
" .bsonDateTime DateDecodingStrategy.")
throw DecodingError.typeMismatch(
AnyBSONValue.self,
DecodingError.Context(
codingPath: bsonDecoder.codingPath,
debugDescription: "Got a BSON datetime but was expecting another format. To " +
"decode from BSON datetimes, use the default .bsonDateTime " +
"DateDecodingStrategy."
)
)
}
}
self.value = bsonDecoder.storage.topContainer
Expand Down
36 changes: 18 additions & 18 deletions Sources/MongoSwift/BSON/BSONValue.swift
Expand Up @@ -132,7 +132,7 @@ public struct BSONNull: BSONValue, Codable, Equatable {

public static func from(iterator iter: DocumentIterator) throws -> BSONNull {
guard iter.currentType == .null else {
throw wrongIterTypeError(iter: iter, expected: BSONNull.self)
throw wrongIterTypeError(iter, expected: BSONNull.self)
}
return BSONNull()
}
Expand Down Expand Up @@ -247,7 +247,7 @@ public struct Binary: BSONValue, Equatable, Codable {
}

guard iter.currentType == .binary else {
throw wrongIterTypeError(iter: iter, expected: Binary.self)
throw wrongIterTypeError(iter, expected: Binary.self)
}

bson_iter_binary(&iter.iter, &subtype, &length, dataPointer)
Expand Down Expand Up @@ -277,7 +277,7 @@ extension Bool: BSONValue {

public static func from(iterator iter: DocumentIterator) throws -> Bool {
guard iter.currentType == .boolean else {
throw wrongIterTypeError(iter: iter, expected: Bool.self)
throw wrongIterTypeError(iter, expected: Bool.self)
}

return self.init(bson_iter_bool(&iter.iter))
Expand Down Expand Up @@ -305,7 +305,7 @@ extension Date: BSONValue {

public static func from(iterator iter: DocumentIterator) throws -> Date {
guard iter.currentType == .dateTime else {
throw wrongIterTypeError(iter: iter, expected: Date.self)
throw wrongIterTypeError(iter, expected: Date.self)
}

return self.init(msSinceEpoch: bson_iter_date_time(&iter.iter))
Expand Down Expand Up @@ -344,7 +344,7 @@ internal struct DBPointer: BSONValue {
bson_iter_dbpointer(&iter.iter, &length, collectionPP, oidPP)

guard let oidP = oidPP.pointee, let collectionP = collectionPP.pointee else {
throw wrongIterTypeError(iter: iter, expected: DBPointer.self)
throw wrongIterTypeError(iter, expected: DBPointer.self)
}

return [
Expand Down Expand Up @@ -404,7 +404,7 @@ public struct Decimal128: BSONValue, Equatable, Codable {
public static func from(iterator iter: DocumentIterator) throws -> Decimal128 {
var value = bson_decimal128_t()
guard bson_iter_decimal128(&iter.iter, &value) else {
throw wrongIterTypeError(iter: iter, expected: Decimal128.self)
throw wrongIterTypeError(iter, expected: Decimal128.self)
}

var str = Data(count: Int(BSON_DECIMAL128_STRING))
Expand All @@ -427,7 +427,7 @@ extension Double: BSONValue {

public static func from(iterator iter: DocumentIterator) throws -> Double {
guard iter.currentType == .double else {
throw wrongIterTypeError(iter: iter, expected: Double.self)
throw wrongIterTypeError(iter, expected: Double.self)
}

return self.init(bson_iter_double(&iter.iter))
Expand Down Expand Up @@ -459,7 +459,7 @@ extension Int: BSONValue {
case .int32, .int64:
return self.init(Int(bson_iter_int32(&iter.iter)))
default:
throw wrongIterTypeError(iter: iter, expected: Int.self)
throw wrongIterTypeError(iter, expected: Int.self)
}
}
}
Expand All @@ -476,7 +476,7 @@ extension Int32: BSONValue {

public static func from(iterator iter: DocumentIterator) throws -> Int32 {
guard iter.currentType == .int32 else {
throw wrongIterTypeError(iter: iter, expected: Int32.self)
throw wrongIterTypeError(iter, expected: Int32.self)
}
return self.init(bson_iter_int32(&iter.iter))
}
Expand All @@ -494,7 +494,7 @@ extension Int64: BSONValue {

public static func from(iterator iter: DocumentIterator) throws -> Int64 {
guard iter.currentType == .int64 else {
throw wrongIterTypeError(iter: iter, expected: Int64.self)
throw wrongIterTypeError(iter, expected: Int64.self)
}
return self.init(bson_iter_int64(&iter.iter))
}
Expand Down Expand Up @@ -539,7 +539,7 @@ public struct CodeWithScope: BSONValue, Equatable, Codable {
}

guard iter.currentType == .javascriptWithScope else {
throw wrongIterTypeError(iter: iter, expected: CodeWithScope.self)
throw wrongIterTypeError(iter, expected: CodeWithScope.self)
}

var scopeLength: UInt32 = 0
Expand Down Expand Up @@ -580,7 +580,7 @@ public struct MaxKey: BSONValue, Equatable, Codable {

public static func from(iterator iter: DocumentIterator) throws -> MaxKey {
guard iter.currentType == .maxKey else {
throw wrongIterTypeError(iter: iter, expected: MaxKey.self)
throw wrongIterTypeError(iter, expected: MaxKey.self)
}
return self.init()
}
Expand All @@ -605,7 +605,7 @@ public struct MinKey: BSONValue, Equatable, Codable {

public static func from(iterator iter: DocumentIterator) throws -> MinKey {
guard iter.currentType == .minKey else {
throw wrongIterTypeError(iter: iter, expected: MinKey.self)
throw wrongIterTypeError(iter, expected: MinKey.self)
}
return self.init()
}
Expand Down Expand Up @@ -682,7 +682,7 @@ public struct ObjectId: BSONValue, Equatable, CustomStringConvertible, Codable {

public static func from(iterator iter: DocumentIterator) throws -> ObjectId {
guard let oid = bson_iter_oid(&iter.iter) else {
throw wrongIterTypeError(iter: iter, expected: ObjectId.self)
throw wrongIterTypeError(iter, expected: ObjectId.self)
}
return self.init(fromPointer: oid)
}
Expand Down Expand Up @@ -790,7 +790,7 @@ public struct RegularExpression: BSONValue, Equatable, Codable {
}

guard let pattern = bson_iter_regex(&iter.iter, options) else {
throw wrongIterTypeError(iter: iter, expected: RegularExpression.self)
throw wrongIterTypeError(iter, expected: RegularExpression.self)
}
let patternString = String(cString: pattern)

Expand Down Expand Up @@ -834,7 +834,7 @@ extension String: BSONValue {
public static func from(iterator iter: DocumentIterator) throws -> String {
var length: UInt32 = 0
guard let strValue = bson_iter_utf8(&iter.iter, &length) else {
throw wrongIterTypeError(iter: iter, expected: String.self)
throw wrongIterTypeError(iter, expected: String.self)
}
return self.init(cString: strValue)
}
Expand All @@ -858,7 +858,7 @@ internal struct Symbol: BSONValue {
internal static func asString(from iter: DocumentIterator) throws -> String {
var length: UInt32 = 0
guard let strValue = bson_iter_symbol(&iter.iter, &length) else {
throw wrongIterTypeError(iter: iter, expected: Symbol.self)
throw wrongIterTypeError(iter, expected: Symbol.self)
}
return String(cString: strValue)
}
Expand Down Expand Up @@ -897,7 +897,7 @@ public struct Timestamp: BSONValue, Equatable, Codable {
var i: UInt32 = 0

guard iter.currentType == .timestamp else {
throw wrongIterTypeError(iter: iter, expected: Timestamp.self)
throw wrongIterTypeError(iter, expected: Timestamp.self)
}

bson_iter_timestamp(&iter.iter, &t, &i)
Expand Down
2 changes: 1 addition & 1 deletion Sources/MongoSwift/MongoError.swift
Expand Up @@ -239,7 +239,7 @@ internal func bsonTooLargeError(value: BSONValue, forKey: String) -> MongoSwiftE
"Failed to set value for key \(forKey) to \(value) with BSON type \(value.bsonType): document too large")
}

internal func wrongIterTypeError(iter: DocumentIterator, expected type: BSONValue.Type) -> MongoSwiftError {
internal func wrongIterTypeError(_ iter: DocumentIterator, expected type: BSONValue.Type) -> MongoSwiftError {
return UserError.logicError(message: "Tried to retreive a \(type) from an iterator whose next type " +
"is \(iter.currentType) for key \(iter.currentKey)")
}

0 comments on commit 7bfd8e7

Please sign in to comment.