Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit b1596f5

Browse files
committed
Merge develop
2 parents acdaabe + 8e34c17 commit b1596f5

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

WordPressKit/JetpackServiceRemote.swift

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
import Foundation
22

3-
public enum JetpackInstallError: String, Error {
4-
case invalidCredentials = "INVALID_CREDENTIALS"
5-
case forbidden = "FORBIDDEN"
6-
case installFailure = "INSTALL_FAILURE"
7-
case installResponseError = "INSTALL_RESPONSE_ERROR"
8-
case loginFailure = "LOGIN_FAILURE"
9-
case siteIsJetpack = "SITE_IS_JETPACK"
10-
case activationOnInstallFailure = "ACTIVATION_ON_INSTALL_FAILURE"
11-
case activationResponseError = "ACTIVATION_RESPONSE_ERROR"
12-
case activationFailure = "ACTIVATION_FAILURE"
13-
case unknown
14-
15-
init(error key: String) {
16-
self = JetpackInstallError(rawValue: key) ?? .unknown
3+
public struct JetpackInstallError: LocalizedError, Equatable {
4+
public enum ErrorType: String {
5+
case invalidCredentials = "INVALID_CREDENTIALS"
6+
case forbidden = "FORBIDDEN"
7+
case installFailure = "INSTALL_FAILURE"
8+
case installResponseError = "INSTALL_RESPONSE_ERROR"
9+
case loginFailure = "LOGIN_FAILURE"
10+
case siteIsJetpack = "SITE_IS_JETPACK"
11+
case activationOnInstallFailure = "ACTIVATION_ON_INSTALL_FAILURE"
12+
case activationResponseError = "ACTIVATION_RESPONSE_ERROR"
13+
case activationFailure = "ACTIVATION_FAILURE"
14+
case unknown
15+
16+
init(error key: String) {
17+
self = ErrorType(rawValue: key) ?? .unknown
18+
}
19+
}
20+
21+
public var title: String?
22+
public var code: Int
23+
public var type: ErrorType
24+
25+
public static var unknown: JetpackInstallError {
26+
return JetpackInstallError(type: .unknown)
27+
}
28+
29+
public init(title: String? = nil, code: Int = 0, key: String? = nil) {
30+
self.init(title: title, code: code, type: ErrorType(error: key ?? ""))
31+
}
32+
33+
public init(title: String? = nil, code: Int = 0, type: ErrorType = .unknown) {
34+
self.title = title
35+
self.code = code
36+
self.type = type
1737
}
1838
}
1939

@@ -61,14 +81,14 @@ public class JetpackServiceRemote: ServiceRemoteWordPressComREST {
6181
let success = response[Constants.status] {
6282
completion(success, nil)
6383
} else {
64-
completion(false, .installResponseError)
84+
completion(false, JetpackInstallError(type: .installResponseError))
6585
}
6686
}) { (error: NSError, httpResponse: HTTPURLResponse?) in
67-
if let key = error.userInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String {
68-
completion(false, JetpackInstallError(error: key))
69-
} else {
70-
completion(false, .unknown)
71-
}
87+
let key = error.userInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String
88+
let jetpackError = JetpackInstallError(title: error.localizedDescription,
89+
code: error.code,
90+
key: key)
91+
completion(false, jetpackError)
7292
}
7393
}
7494

WordPressKitTests/JetpackServiceRemoteTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
134134
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorInvalidCredentialsMockFilename, contentType: .ApplicationJSON, status: 400)
135135
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
136136
XCTAssertFalse(success, "Success should be false")
137-
XCTAssertEqual(error, .invalidCredentials)
137+
XCTAssertEqual(error?.type, .invalidCredentials)
138138
expect.fulfill()
139139
}
140140

@@ -147,7 +147,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
147147
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorUnknownMockFilename, contentType: .ApplicationJSON, status: 400)
148148
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
149149
XCTAssertFalse(success, "Success should be false")
150-
XCTAssertEqual(error, .unknown)
150+
XCTAssertEqual(error?.type, .unknown)
151151
expect.fulfill()
152152
}
153153

@@ -160,7 +160,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
160160
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorForbiddenMockFilename, contentType: .ApplicationJSON, status: 400)
161161
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
162162
XCTAssertFalse(success, "Success should be false")
163-
XCTAssertEqual(error, .forbidden)
163+
XCTAssertEqual(error?.type, .forbidden)
164164
expect.fulfill()
165165
}
166166

@@ -173,7 +173,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
173173
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorInstallFailureMockFilename, contentType: .ApplicationJSON, status: 400)
174174
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
175175
XCTAssertFalse(success, "Success should be false")
176-
XCTAssertEqual(error, .installFailure)
176+
XCTAssertEqual(error?.type, .installFailure)
177177
expect.fulfill()
178178
}
179179

@@ -186,7 +186,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
186186
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorInstallResponseMockFilename, contentType: .ApplicationJSON, status: 400)
187187
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
188188
XCTAssertFalse(success, "Success should be false")
189-
XCTAssertEqual(error, .installResponseError)
189+
XCTAssertEqual(error?.type, .installResponseError)
190190
expect.fulfill()
191191
}
192192

@@ -199,7 +199,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
199199
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorLoginFailureMockFilename, contentType: .ApplicationJSON, status: 400)
200200
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
201201
XCTAssertFalse(success, "Success should be false")
202-
XCTAssertEqual(error, .loginFailure)
202+
XCTAssertEqual(error?.type, .loginFailure)
203203
expect.fulfill()
204204
}
205205

@@ -212,7 +212,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
212212
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorSiteIsJetpackMockFilename, contentType: .ApplicationJSON, status: 400)
213213
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
214214
XCTAssertFalse(success, "Success should be false")
215-
XCTAssertEqual(error, .siteIsJetpack)
215+
XCTAssertEqual(error?.type, .siteIsJetpack)
216216
expect.fulfill()
217217
}
218218

@@ -225,7 +225,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
225225
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorActivationInstallMockFilename, contentType: .ApplicationJSON, status: 400)
226226
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
227227
XCTAssertFalse(success, "Success should be false")
228-
XCTAssertEqual(error, .activationOnInstallFailure)
228+
XCTAssertEqual(error?.type, .activationOnInstallFailure)
229229
expect.fulfill()
230230
}
231231

@@ -238,7 +238,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
238238
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorActivationResponseMockFilename, contentType: .ApplicationJSON, status: 400)
239239
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
240240
XCTAssertFalse(success, "Success should be false")
241-
XCTAssertEqual(error, .activationResponseError)
241+
XCTAssertEqual(error?.type, .activationResponseError)
242242
expect.fulfill()
243243
}
244244

@@ -251,7 +251,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
251251
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorActivationFailureMockFilename, contentType: .ApplicationJSON, status: 400)
252252
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
253253
XCTAssertFalse(success, "Success should be false")
254-
XCTAssertEqual(error, .activationFailure)
254+
XCTAssertEqual(error?.type, .activationFailure)
255255
expect.fulfill()
256256
}
257257

0 commit comments

Comments
 (0)