Skip to content

Commit

Permalink
Better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Oct 13, 2016
1 parent 16c7618 commit 8acc44e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Sources/Document+ParsingSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

public func fromBytes<T, S : Collection>(_ bytes: S) throws -> T where S.Iterator.Element == UInt8, S.IndexDistance == Int {
guard bytes.count >= MemoryLayout<T>.size else {
throw DeserializationError.InvalidElementSize
throw DeserializationError.invalidElementSize
}

return UnsafeRawPointer([UInt8](bytes)).assumingMemoryBound(to: T.self).pointee
Expand Down
26 changes: 15 additions & 11 deletions Sources/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@
// Copyright © 2016 Robbert Brandsma. All rights reserved.
//

import Foundation

/// All errors that can occur when (de)serializing BSON
public enum DeserializationError : Error {
/// The Document doesn't have a valid length
case InvalidDocumentLength
case invalidDocumentLength

/// The instantiating went wrong because the element has an invalid size
case InvalidElementSize
case invalidElementSize

/// The contents of the BSON binary data was invalid
case InvalidElementContents
case invalidElementContents

/// The BSON Element type was unknown
case UnknownElementType
case unknownElementType

/// The lsat element of the BSON Binary Array was invalid
case InvalidLastElement
case invalidLastElement

/// The given length for the ObjectId isn't 12-bytes or a 24-character hexstring
case InvalidObjectIdLength

/// String with given bytes couldn't be instantiated
case unableToInstantiateString(fromBytes: [UInt8])

/// Something went wrong with parsing (yeah.. very specific)
case ParseError
/// -
case missingNullTerminatorInString

/// This operation was invalid
case InvalidOperation
/// No CString found in given data
case noCStringFound
}
14 changes: 7 additions & 7 deletions Sources/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public extension String {
private static func _instant(bytes data: [UInt8]) throws -> (Int, String) {
// Check for null-termination and at least 5 bytes (length spec + terminator)
guard data.count >= 5 && data.last == 0x00 else {
throw DeserializationError.InvalidLastElement
throw DeserializationError.invalidLastElement
}

// Get the length
let length = try fromBytes(data[0...3]) as Int32

// Check if the data is at least the right size
guard data.count >= Int(length) + 4 else {
throw DeserializationError.ParseError
throw DeserializationError.invalidElementSize
}

// Empty string
Expand All @@ -56,13 +56,13 @@ public extension String {
}

guard length > 0 else {
throw DeserializationError.ParseError
throw DeserializationError.invalidElementSize
}

var stringData = Array(data[4..<Int(length + 3)])

guard let string = String(bytesNoCopy: &stringData, length: stringData.count, encoding: String.Encoding.utf8, freeWhenDone: false) else {
throw DeserializationError.ParseError
throw DeserializationError.unableToInstantiateString(fromBytes: stringData)
}

return (Int(length + 4), string)
Expand All @@ -84,15 +84,15 @@ public extension String {

private static func _cInstant(bytes data: [UInt8]) throws -> (Int, String) {
guard data.contains(0x00) else {
throw DeserializationError.ParseError
throw DeserializationError.missingNullTerminatorInString
}

guard let stringData = data.split(separator: 0x00, maxSplits: 1, omittingEmptySubsequences: false).first else {
throw DeserializationError.ParseError
throw DeserializationError.noCStringFound
}

guard let string = String(bytes: stringData, encoding: String.Encoding.utf8) else {
throw DeserializationError.ParseError
throw DeserializationError.unableToInstantiateString(fromBytes: Array(stringData))
}

return (stringData.count+1, string)
Expand Down
6 changes: 3 additions & 3 deletions Sources/ObjectId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public struct ObjectId {
/// Throws errors in case of an invalid string (e.g. wrong length)
public init(_ hexString: String) throws {
guard hexString.characters.count == 24 else {
throw DeserializationError.ParseError
throw DeserializationError.InvalidObjectIdLength
}

var data = [UInt8]()
Expand All @@ -86,7 +86,7 @@ public struct ObjectId {
}

guard data.count == 12 else {
throw DeserializationError.ParseError
throw DeserializationError.InvalidObjectIdLength
}

self._storage = data
Expand All @@ -102,7 +102,7 @@ public struct ObjectId {
/// Throws when there are not exactly 12 bytes provided
public init(bytes data: [UInt8]) throws {
guard data.count == 12 else {
throw DeserializationError.InvalidElementSize
throw DeserializationError.invalidElementSize
}

self._storage = data
Expand Down

0 comments on commit 8acc44e

Please sign in to comment.