From 0d40adb80b103a7af91e22cc67a9b7761dca7196 Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 18 Oct 2017 16:47:08 -0700 Subject: [PATCH] Updated static cantMap(value:toType:)->NSError method on NSError to return 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. --- ConcurrencyTests/PromiseTests.swift | 2 +- Source/ConcurrencyHelpers.swift | 34 +++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ConcurrencyTests/PromiseTests.swift b/ConcurrencyTests/PromiseTests.swift index aa97ec6..07a8ad0 100644 --- a/ConcurrencyTests/PromiseTests.swift +++ b/ConcurrencyTests/PromiseTests.swift @@ -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()) } } diff --git a/Source/ConcurrencyHelpers.swift b/Source/ConcurrencyHelpers.swift index 858f2ca..08c0c71 100644 --- a/Source/ConcurrencyHelpers.swift +++ b/Source/ConcurrencyHelpers.swift @@ -8,8 +8,38 @@ import Foundation extension NSError { static func cantMap(value:T, toType: Q.Type) -> NSError { + return CantMapError(value: value, toType: toType) + } +} + +fileprivate class CantMapError: 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 } + }