-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from AppCron/improved-nserror-handling
Improved NSError handling
- Loading branch information
Showing
6 changed files
with
256 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,45 @@ | ||
import Foundation | ||
|
||
open class InteractorError: Error { | ||
public typealias InteractorError = NSError | ||
|
||
public extension InteractorError { | ||
|
||
private static let dictKey = "InteractorErrorDictKey" | ||
|
||
// MARK: - Init | ||
|
||
public convenience init(message: String, code: Int, dict: [String: Any]) { | ||
var infoDict = [String : Any]() | ||
infoDict[NSLocalizedDescriptionKey] = message | ||
infoDict[InteractorError.dictKey] = dict | ||
self.init(domain: "com.appcron.acinteractor", code: code, userInfo: infoDict) | ||
} | ||
|
||
open var message: String | ||
open var errorCode = 0 | ||
open var nsError: NSError? | ||
public convenience init(message: String, code: Int) { | ||
self.init(message: message, code: code, dict: [String: Any]()) | ||
} | ||
|
||
public init(message:String) { | ||
self.message = message | ||
public convenience init(message: String) { | ||
self.init(message: message, code: 0) | ||
} | ||
|
||
public init(error:NSError) { | ||
self.message = error.localizedDescription | ||
self.errorCode = error.code | ||
self.nsError = error | ||
// MARK: - Message | ||
|
||
public var message: String { | ||
get { | ||
return userInfo[NSLocalizedDescriptionKey] as? String ?? "" | ||
} | ||
} | ||
|
||
// MARK: - Error Dict | ||
|
||
public var dict: [String: Any] { | ||
get { | ||
guard let dict = userInfo[InteractorError.dictKey] as? [String: Any] else { | ||
return [String: Any]() | ||
} | ||
return dict | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Foundation | ||
|
||
public class MutableInteractorError: InteractorError { | ||
|
||
private var mutableMessage: String | ||
private var mutableCode: Int | ||
private var mutableDict: [String : Any] | ||
|
||
// MARK: - Init | ||
|
||
public init(message: String, code: Int, dict: [String: Any]) { | ||
mutableMessage = message | ||
mutableCode = code | ||
mutableDict = dict | ||
super.init(domain: "com.appcron.acinteractor", code: code, userInfo: nil) | ||
} | ||
|
||
public convenience init(message: String, code: Int) { | ||
self.init(message: message, code: code, dict: [String: Any]()) | ||
} | ||
|
||
public convenience init(message: String) { | ||
self.init(message: message, code: 0) | ||
} | ||
|
||
public required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
public init(error: NSError) { | ||
mutableMessage = error.localizedDescription | ||
mutableCode = error.code | ||
mutableDict = error.dict | ||
super.init(domain: error.domain, code: error.code, userInfo: error.userInfo) | ||
} | ||
|
||
// MARK: - Getter & Setter | ||
|
||
public override var message: String { | ||
get { | ||
return mutableMessage | ||
} | ||
set { | ||
mutableMessage = newValue | ||
} | ||
} | ||
|
||
public override var code: Int { | ||
get { | ||
return mutableCode | ||
} | ||
set { | ||
mutableCode = newValue | ||
} | ||
} | ||
|
||
public override var dict: [String : Any] { | ||
get { | ||
return mutableDict | ||
} | ||
set { | ||
mutableDict = newValue | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
Tests/ACInteractorTests/MutableInteractorErrorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import XCTest | ||
@testable import ACInteractor | ||
|
||
class MutableInteractorErrorTests: XCTestCase { | ||
|
||
var testError: MutableInteractorError? | ||
var testErrorDict = [String: Any]() | ||
|
||
override func setUp() { | ||
super.setUp() | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
|
||
testError = MutableInteractorError(message: "") | ||
|
||
testErrorDict["key1"] = "value1" | ||
testErrorDict["key2"] = 2 | ||
} | ||
|
||
// MARK: - Init | ||
|
||
func testInit_withMessage_setsMessage() { | ||
// Act | ||
let error = MutableInteractorError(message: "testMessage") | ||
|
||
// Assert | ||
XCTAssertEqual(error.message, "testMessage") | ||
} | ||
|
||
func testInit_withCode_setsCode() { | ||
// Act | ||
let error = MutableInteractorError(message: "", code: 42) | ||
|
||
// Assert | ||
XCTAssertEqual(error.code, 42) | ||
} | ||
|
||
func testInit_withDict_setsDict() { | ||
// Act | ||
let error = MutableInteractorError(message: "", code: 0, dict: testErrorDict) | ||
|
||
// Assert | ||
XCTAssertEqual(error.dict["key1"] as? String, "value1") | ||
XCTAssertEqual(error.dict["key2"] as? Int, 2) | ||
} | ||
|
||
// MARK: - Init with Error | ||
|
||
func testInit_withNsError_copiesValuesFromNsError() { | ||
// Arrange | ||
let userInfo: [String: Any] = [NSLocalizedDescriptionKey: "localizedTestDescription", "key2": 2] | ||
let nsError = NSError(domain: "nserror.domain", code: 13, userInfo: userInfo) | ||
|
||
// Act | ||
let error = MutableInteractorError(error: nsError) | ||
|
||
// Assert | ||
XCTAssertEqual(error.message, nsError.message) | ||
XCTAssertEqual(error.localizedDescription, nsError.localizedDescription) | ||
|
||
XCTAssertEqual(error.code, nsError.code) | ||
XCTAssertEqual(error.domain, nsError.domain) | ||
|
||
XCTAssertEqual(error.userInfo[NSLocalizedDescriptionKey] as? String, "localizedTestDescription") | ||
XCTAssertEqual(error.userInfo["key2"] as? Int, 2) | ||
} | ||
|
||
func testInit_withInteractorError_copiesValuesFromInteractorError() { | ||
// Arrange | ||
let interactorError = InteractorError(message: "testMessage", code: 42, dict: testErrorDict) | ||
|
||
// Act | ||
let error = MutableInteractorError(error: interactorError) | ||
|
||
// Assert | ||
XCTAssertEqual(error.message, interactorError.message) | ||
XCTAssertEqual(error.localizedDescription, interactorError.localizedDescription) | ||
|
||
XCTAssertEqual(error.code, interactorError.code) | ||
XCTAssertEqual(error.domain, interactorError.domain) | ||
|
||
XCTAssertEqual(error.dict["key1"] as? String, "value1") | ||
XCTAssertEqual(error.dict["key2"] as? Int, 2) | ||
} | ||
|
||
// MARK: - Setters | ||
|
||
func testSetMessage_setsMessage() { | ||
// Act | ||
testError?.message = "newMessage" | ||
|
||
// Assert | ||
XCTAssertEqual(testError?.message, "newMessage") | ||
} | ||
|
||
func testSetCode_setCode() { | ||
// Act | ||
testError?.code = 42 | ||
|
||
// Assert | ||
XCTAssertEqual(testError?.code, 42) | ||
} | ||
|
||
func testSetDict_setsDict() { | ||
// Act | ||
testError?.dict = testErrorDict | ||
|
||
// Assert | ||
XCTAssertEqual(testError?.dict["key1"] as? String, "value1") | ||
XCTAssertEqual(testError?.dict["key2"] as? Int, 2) | ||
} | ||
|
||
func testSetDictValue_addsDictValue() { | ||
// Arrange | ||
testError?.dict = testErrorDict | ||
|
||
// Act | ||
testError?.dict["newKey"] = "newValue" | ||
|
||
// Assert | ||
XCTAssertEqual(testError?.dict["newKey"] as? String, "newValue") | ||
XCTAssertEqual(testError?.dict.count, testErrorDict.count + 1) | ||
} | ||
|
||
} |