Skip to content

Commit 9428573

Browse files
author
Florian Rieger
committed
Added init with error to MutableInteractorError.
1 parent b10093a commit 9428573

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Sources/MutableInteractorError.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public class MutableInteractorError: InteractorError {
2727
fatalError("init(coder:) has not been implemented")
2828
}
2929

30+
public init(error: NSError) {
31+
mutableMessage = error.localizedDescription
32+
mutableCode = error.code
33+
mutableDict = error.dict
34+
super.init(domain: error.domain, code: error.code, userInfo: error.userInfo)
35+
}
36+
3037
// MARK: - Getter & Setter
3138

3239
public override var message: String {

Tests/ACInteractorTests/MutableInteractorErrorTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,45 @@ class MutableInteractorErrorTests: XCTestCase {
4343
XCTAssertEqual(error.dict["key2"] as? Int, 2)
4444
}
4545

46+
// MARK: - Init with Error
47+
48+
func testInit_withNsError_copiesValuesFromNsError() {
49+
// Arrange
50+
let userInfo: [String: Any] = [NSLocalizedDescriptionKey: "localizedTestDescription", "key2": 2]
51+
let nsError = NSError(domain: "nserror.domain", code: 13, userInfo: userInfo)
52+
53+
// Act
54+
let error = MutableInteractorError(error: nsError)
55+
56+
// Assert
57+
XCTAssertEqual(error.message, nsError.message)
58+
XCTAssertEqual(error.localizedDescription, nsError.localizedDescription)
59+
60+
XCTAssertEqual(error.code, nsError.code)
61+
XCTAssertEqual(error.domain, nsError.domain)
62+
63+
XCTAssertEqual(error.userInfo[NSLocalizedDescriptionKey] as? String, "localizedTestDescription")
64+
XCTAssertEqual(error.userInfo["key2"] as? Int, 2)
65+
}
66+
67+
func testInit_withInteractorError_copiesValuesFromInteractorError() {
68+
// Arrange
69+
let interactorError = InteractorError(message: "testMessage", code: 42, dict: testErrorDict)
70+
71+
// Act
72+
let error = MutableInteractorError(error: interactorError)
73+
74+
// Assert
75+
XCTAssertEqual(error.message, interactorError.message)
76+
XCTAssertEqual(error.localizedDescription, interactorError.localizedDescription)
77+
78+
XCTAssertEqual(error.code, interactorError.code)
79+
XCTAssertEqual(error.domain, interactorError.domain)
80+
81+
XCTAssertEqual(error.dict["key1"] as? String, "value1")
82+
XCTAssertEqual(error.dict["key2"] as? Int, 2)
83+
}
84+
4685
// MARK: - Setters
4786

4887
func testSetMessage_setsMessage() {

0 commit comments

Comments
 (0)