From 80ac1e989cfb8a5ea2df6cd97af886081acff392 Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Fri, 5 Apr 2024 11:11:33 +1100 Subject: [PATCH 1/4] Avoid casting to `NSError` in favor of `WordPressApiError` See discussion at https://github.com/wordpress-mobile/WordPressKit-iOS/pull/777#discussion_r1552498217 with @crazytonyli --- .../Services/ActivityServiceRemote.swift | 36 ++++++++++++++----- .../Services/JetpackServiceRemote.swift | 16 ++++++--- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Sources/WordPressKit/Services/ActivityServiceRemote.swift b/Sources/WordPressKit/Services/ActivityServiceRemote.swift index d81ab327..a62bf2a4 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 let endpointError = error.castToEndpointErrorWitCode(.preconditionFailure) else { + success(RewindStatus(state: .unavailable)) return } - let status = RewindStatus(state: .unavailable) - success(status) + failure(endpointError) + return }) } @@ -222,3 +219,24 @@ private extension ActivityServiceRemote { } } + +private extension Error { + + func castToEndpointErrorWitCode( + _ 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..a8aae7c8 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.errorUserInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String + ) completion(false, jetpackError) } } From 568d67fd016b04e5213878c40593317f9a0468d5 Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Mon, 15 Apr 2024 16:40:58 +1000 Subject: [PATCH 2/4] Rename `castToEndpointErrorWitCode` to `castedToEndpointErrorWitCode` --- Sources/WordPressKit/Services/ActivityServiceRemote.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/WordPressKit/Services/ActivityServiceRemote.swift b/Sources/WordPressKit/Services/ActivityServiceRemote.swift index a62bf2a4..75b39486 100644 --- a/Sources/WordPressKit/Services/ActivityServiceRemote.swift +++ b/Sources/WordPressKit/Services/ActivityServiceRemote.swift @@ -154,7 +154,7 @@ open class ActivityServiceRemote: ServiceRemoteWordPressComREST { }, failure: { error, _ in // 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 let endpointError = error.castToEndpointErrorWitCode(.preconditionFailure) else { + guard error.castedToEndpointErrorWitCode(.preconditionFailure) != nil else { success(RewindStatus(state: .unavailable)) return } @@ -222,7 +222,7 @@ private extension ActivityServiceRemote { private extension Error { - func castToEndpointErrorWitCode( + func castedToEndpointErrorWitCode( _ code: WordPressComRestApiErrorCode ) -> WordPressComRestApiEndpointError? { guard let apiError = self as? WordPressAPIError else { From 9d5aa85e52e11eb86c45b0980b2ea2019626248e Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Mon, 15 Apr 2024 16:41:14 +1000 Subject: [PATCH 3/4] Restore returning `error` instead of cast `endpointError` Thanks for the hint @crazytonyli. See https://github.com/wordpress-mobile/WordPressKit-iOS/pull/779/files#r1552634468 --- Sources/WordPressKit/Services/ActivityServiceRemote.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WordPressKit/Services/ActivityServiceRemote.swift b/Sources/WordPressKit/Services/ActivityServiceRemote.swift index 75b39486..d3cf6c8d 100644 --- a/Sources/WordPressKit/Services/ActivityServiceRemote.swift +++ b/Sources/WordPressKit/Services/ActivityServiceRemote.swift @@ -159,7 +159,7 @@ open class ActivityServiceRemote: ServiceRemoteWordPressComREST { return } - failure(endpointError) + failure(error) return }) From 794f03f08456c8d764cea9ac29b3cc525a9fda78 Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Mon, 15 Apr 2024 16:46:13 +1000 Subject: [PATCH 4/4] Use better API to set error key code in `JetpackServiceRemote` --- Sources/WordPressKit/Services/JetpackServiceRemote.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WordPressKit/Services/JetpackServiceRemote.swift b/Sources/WordPressKit/Services/JetpackServiceRemote.swift index a8aae7c8..ef7a4f52 100644 --- a/Sources/WordPressKit/Services/JetpackServiceRemote.swift +++ b/Sources/WordPressKit/Services/JetpackServiceRemote.swift @@ -93,7 +93,7 @@ public class JetpackServiceRemote: ServiceRemoteWordPressComREST { let jetpackError = JetpackInstallError( title: endpointError.localizedDescription, code: endpointError.errorCode, - key: endpointError.errorUserInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String + key: endpointError.apiErrorCode ) completion(false, jetpackError) }