Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid casting to NSError in favor of WordPressApiError #779

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions Sources/WordPressKit/Services/ActivityServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My brain can't process these two guard statements. But, are their logic changed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed an unit test failed, I think this guard change here might be the cause?


let status = RewindStatus(state: .unavailable)
success(status)
failure(error)

return
})
}
Expand Down Expand Up @@ -222,3 +219,24 @@ private extension ActivityServiceRemote {
}

}

private extension Error {

func castedToEndpointErrorWitCode(
_ code: WordPressComRestApiErrorCode
) -> WordPressComRestApiEndpointError? {
guard let apiError = self as? WordPressAPIError<WordPressComRestApiEndpointError> else {
return .none
}

guard case .endpointError(let endpointError) = apiError else {
return .none
}

guard endpointError.code == code else {
return .none
}

return endpointError
}
}
16 changes: 11 additions & 5 deletions Sources/WordPressKit/Services/JetpackServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<WordPressComRestApiEndpointError>,
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)
}
}
Expand Down