Skip to content

Commit

Permalink
Updated static cantMap(value:toType:)->NSError method on NSError to r…
Browse files Browse the repository at this point in the history
…eturn an error that is more debugger friendly (prints a helpful, human-readable description), and that conforms to standard practices with regard to the 'domain' property.
  • Loading branch information
jakeHawkenGeocaching committed Oct 18, 2017
1 parent 83dc2e2 commit 0d40adb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ConcurrencyTests/PromiseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class PromiseTests: QuickSpec {
expect(mappedFuture.isComplete).toEventually(beTrue())
expect(mappedFuture.succeeded).toEventually(beFalse())
expect(mappedFuture.failed).toEventually(beTrue())
expect(item(mappedFuture.error, isA: NSError.self, and: {$0.domain == "Could not map value (3) to type String."})).toEventually(beTrue())
expect(item(mappedFuture.error, isA: NSError.self, and:{ $0.description == "Concurrency: Could not map value (3) to type String." })).toEventually(beTrue())
expect(mappedFuture.value).toEventually(beNil())
}
}
Expand Down
34 changes: 32 additions & 2 deletions Source/ConcurrencyHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,38 @@ import Foundation

extension NSError {
static func cantMap<T,Q>(value:T, toType: Q.Type) -> NSError {
return CantMapError(value: value, toType: toType)
}
}

fileprivate class CantMapError<Q,T>: NSError {

private var descriptionString: String

init(value: T, toType: Q.Type) {
let typeString = String(describing: type(of: Q.self)).replacingOccurrences(of: ".Type", with: "")
let description = "Could not map value (\(value)) to type \(typeString)."
return NSError(domain: description, code: 0, userInfo: nil)
let description = "Concurrency: Could not map value (\(value)) to type \(typeString)."
self.descriptionString = description

super.init(domain: "com.concurrency.map", code: 0, userInfo: ["description" : description])
}

required init?(coder aDecoder: NSCoder) {
self.descriptionString = "Mapping Error"

super.init(coder: aDecoder)

if let unarchivedDescription = self.userInfo["description"] as? String {
self.descriptionString = unarchivedDescription
}
}

override var description: String {
return descriptionString
}

public static func ==(lhs: CantMapError, rhs: CantMapError) -> Bool {
return lhs.description == rhs.description
}

}

0 comments on commit 0d40adb

Please sign in to comment.