diff --git a/Sources/WordPressKit/Services/ActivityServiceRemote.swift b/Sources/WordPressKit/Services/ActivityServiceRemote.swift index d81ab327..d3cf6c8d 100644 --- a/Sources/WordPressKit/Services/ActivityServiceRemote.swift +++ b/Sources/WordPressKit/Services/ActivityServiceRemote.swift @@ -152,18 +152,15 @@ open class ActivityServiceRemote: ServiceRemoteWordPressComREST { failure(ResponseError.decodingFailure) } }, failure: { error, _ in - // FIXME: A hack to support free WPCom sites and Rewind. Should be obsolote as soon as the backend - // stops returning 412's for those sites. - let nsError = error as NSError - - guard nsError.domain == WordPressComRestApiEndpointError.errorDomain, - nsError.code == WordPressComRestApiErrorCode.preconditionFailure.rawValue else { - failure(error) + // FIXME: A hack to support free WPCom sites and Rewind. + // Should be obsolete as soon as the backend stops returning 412's for those sites. + guard error.castedToEndpointErrorWitCode(.preconditionFailure) != nil else { + success(RewindStatus(state: .unavailable)) return } - let status = RewindStatus(state: .unavailable) - success(status) + failure(error) + return }) } @@ -222,3 +219,24 @@ private extension ActivityServiceRemote { } } + +private extension Error { + + func castedToEndpointErrorWitCode( + _ code: WordPressComRestApiErrorCode + ) -> WordPressComRestApiEndpointError? { + guard let apiError = self as? WordPressAPIError else { + return .none + } + + guard case .endpointError(let endpointError) = apiError else { + return .none + } + + guard endpointError.code == code else { + return .none + } + + return endpointError + } +} diff --git a/Sources/WordPressKit/Services/JetpackServiceRemote.swift b/Sources/WordPressKit/Services/JetpackServiceRemote.swift index b84d62f7..ef7a4f52 100644 --- a/Sources/WordPressKit/Services/JetpackServiceRemote.swift +++ b/Sources/WordPressKit/Services/JetpackServiceRemote.swift @@ -84,11 +84,17 @@ public class JetpackServiceRemote: ServiceRemoteWordPressComREST { completion(false, JetpackInstallError(type: .installResponseError)) } }) { error, _ in - let error = error as NSError - let key = error.userInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String - let jetpackError = JetpackInstallError(title: error.localizedDescription, - code: error.code, - key: key) + guard let apiError = error as? WordPressAPIError, + case .endpointError(let endpointError) = apiError else { + completion(false, nil) + return + } + + let jetpackError = JetpackInstallError( + title: endpointError.localizedDescription, + code: endpointError.errorCode, + key: endpointError.apiErrorCode + ) completion(false, jetpackError) } }