Skip to content

Commit

Permalink
Merge pull request JohnSundell#69 from JohnSundell/invalid-value
Browse files Browse the repository at this point in the history
Document new UnboxError case + rename to InvalidValueErrors
  • Loading branch information
JohnSundell committed May 23, 2016
2 parents fad190b + e0921d8 commit c135b12
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions Sources/Unbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public func Unbox<T: UnboxableWithContext>(data: NSData, context: T.ContextType,

// MARK: - Error type

/// Enum describing unboxing errors that were caused by invalid or missing values
public enum UnboxValueError: ErrorType, CustomStringConvertible {
public var description: String {
switch self {
Expand All @@ -95,20 +96,20 @@ public enum UnboxValueError: ErrorType, CustomStringConvertible {
}
}

/// Thrown when a required key was missing in an unboxed dictionary. Contains the missing key.
/// Thrown when a required key/value was missing in an unboxed dictionary. Contains the missing key.
case MissingValueForKey(String)
/// Thrown when a required key contained an invalid value in an unboxed dictionary. Contains the invalid
/// key and a description of the invalid data.
case InvalidValue(String, String)
}

/// Enum describing errors that can occur during unboxing. Use the throwing functions to receive any errors.
/// Enum describing errors that can occur during unboxing
public enum UnboxError: ErrorType, CustomStringConvertible {
public var description: String {
let baseDescription = "[Unbox error] "

switch self {
case .InvalidInput(let errors):
case .InvalidValues(let errors):
return baseDescription + errors.map{"\($0)"}.joinWithSeparator(", ")
case .InvalidData:
return baseDescription + "Invalid NSData"
Expand All @@ -117,7 +118,8 @@ public enum UnboxError: ErrorType, CustomStringConvertible {
}
}

case InvalidInput([UnboxValueError])
/// Thrown when one or many invalid values were encountered. Contains errors for each value. See UnboxValueError for more info.
case InvalidValues([UnboxValueError])
/// Thrown when a piece of data (NSData) could not be unboxed because it was considered invalid
case InvalidData
/// Thrown when a custom unboxing closure returned nil
Expand Down Expand Up @@ -736,7 +738,7 @@ private extension Unboxer {
}
}

throw UnboxError.InvalidInput(inputErrors)
throw UnboxError.InvalidValues(inputErrors)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/UnboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class UnboxTests: XCTestCase {
do {
try Unbox(invalidDictionary) as UnboxTestMock
XCTFail("Unbox should have thrown for a missing value")
} catch UnboxError.InvalidInput(let errors) where !errors.isEmpty {
} catch UnboxError.InvalidValues(let errors) where !errors.isEmpty {
guard case .MissingValueForKey(_) = errors.first! else {
XCTFail("Unbox did not return the correct error type")
return
Expand All @@ -510,7 +510,7 @@ class UnboxTests: XCTestCase {
do {
try Unbox(invalidDictionary) as UnboxTestMock
XCTFail("Unbox should have thrown for an invalid value")
} catch UnboxError.InvalidInput(let errors) where !errors.isEmpty {
} catch UnboxError.InvalidValues(let errors) where !errors.isEmpty {
guard case .InvalidValue(_, _) = errors.first! else {
XCTFail("Unbox did not return the correct error type")
return
Expand Down

0 comments on commit c135b12

Please sign in to comment.