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

Commit 80ac1e9

Browse files
committed
Avoid casting to NSError in favor of WordPressApiError
See discussion at #777 (comment) with @crazytonyli
1 parent 9930ab2 commit 80ac1e9

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

Sources/WordPressKit/Services/ActivityServiceRemote.swift

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,15 @@ open class ActivityServiceRemote: ServiceRemoteWordPressComREST {
152152
failure(ResponseError.decodingFailure)
153153
}
154154
}, failure: { error, _ in
155-
// FIXME: A hack to support free WPCom sites and Rewind. Should be obsolote as soon as the backend
156-
// stops returning 412's for those sites.
157-
let nsError = error as NSError
158-
159-
guard nsError.domain == WordPressComRestApiEndpointError.errorDomain,
160-
nsError.code == WordPressComRestApiErrorCode.preconditionFailure.rawValue else {
161-
failure(error)
155+
// FIXME: A hack to support free WPCom sites and Rewind.
156+
// Should be obsolete as soon as the backend stops returning 412's for those sites.
157+
guard let endpointError = error.castToEndpointErrorWitCode(.preconditionFailure) else {
158+
success(RewindStatus(state: .unavailable))
162159
return
163160
}
164161

165-
let status = RewindStatus(state: .unavailable)
166-
success(status)
162+
failure(endpointError)
163+
167164
return
168165
})
169166
}
@@ -222,3 +219,24 @@ private extension ActivityServiceRemote {
222219
}
223220

224221
}
222+
223+
private extension Error {
224+
225+
func castToEndpointErrorWitCode(
226+
_ code: WordPressComRestApiErrorCode
227+
) -> WordPressComRestApiEndpointError? {
228+
guard let apiError = self as? WordPressAPIError<WordPressComRestApiEndpointError> else {
229+
return .none
230+
}
231+
232+
guard case .endpointError(let endpointError) = apiError else {
233+
return .none
234+
}
235+
236+
guard endpointError.code == code else {
237+
return .none
238+
}
239+
240+
return endpointError
241+
}
242+
}

Sources/WordPressKit/Services/JetpackServiceRemote.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,17 @@ public class JetpackServiceRemote: ServiceRemoteWordPressComREST {
8484
completion(false, JetpackInstallError(type: .installResponseError))
8585
}
8686
}) { error, _ in
87-
let error = error as NSError
88-
let key = error.userInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String
89-
let jetpackError = JetpackInstallError(title: error.localizedDescription,
90-
code: error.code,
91-
key: key)
87+
guard let apiError = error as? WordPressAPIError<WordPressComRestApiEndpointError>,
88+
case .endpointError(let endpointError) = apiError else {
89+
completion(false, nil)
90+
return
91+
}
92+
93+
let jetpackError = JetpackInstallError(
94+
title: endpointError.localizedDescription,
95+
code: endpointError.errorCode,
96+
key: endpointError.errorUserInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String
97+
)
9298
completion(false, jetpackError)
9399
}
94100
}

0 commit comments

Comments
 (0)