This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Avoid casting to NSError in favor of WordPressApiError
#779
Open
mokagio
wants to merge
4
commits into
trunk
Choose a base branch
from
mokagio/better-error-casting-followup-777
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
80ac1e9
Avoid casting to `NSError` in favor of `WordPressApiError`
mokagio 568d67f
Rename `castToEndpointErrorWitCode` to `castedToEndpointErrorWitCode`
mokagio 9d5aa85
Restore returning `error` instead of cast `endpointError`
mokagio 794f03f
Use better API to set error key code in `JetpackServiceRemote`
mokagio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original code returns |
||
|
|
||
| return | ||
| }) | ||
| } | ||
|
|
@@ -222,3 +219,24 @@ private extension ActivityServiceRemote { | |
| } | ||
|
|
||
| } | ||
|
|
||
| private extension Error { | ||
|
|
||
| func castToEndpointErrorWitCode( | ||
| _ 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 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.errorUserInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you can do |
||
| ) | ||
| completion(false, jetpackError) | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?