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

Refactor WordPressComRestApiError #696

Merged
merged 13 commits into from
Jan 19, 2024
Merged
Changes from 1 commit
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
18 changes: 9 additions & 9 deletions WordPressKit/WordPressComRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ extension WordPressComRestApi {
let responseDictionary = responseObject as? [String: AnyObject] else {

if let error = checkForThrottleErrorIn(data: data) {
return error
return error as NSError
}
return WordPressComRestApiError.unknown as NSError
}
Expand Down Expand Up @@ -546,20 +546,20 @@ extension WordPressComRestApi {
return resultError
}

func checkForThrottleErrorIn(data: Data) -> NSError? {
func checkForThrottleErrorIn(data: Data) -> WordPressComRestApiEndpointError? {
// This endpoint is throttled, so check if we've sent too many requests and fill that error in as
// when too many requests occur the API just spits out an html page.
guard let responseString = String(data: data, encoding: .utf8),
responseString.contains("Limit reached") else {
return nil
}
var userInfo = [String: Any]()
userInfo[WordPressComRestApi.ErrorKeyErrorCode] = "too_many_requests"
userInfo[WordPressComRestApi.ErrorKeyErrorMessage] = NSLocalizedString("Limit reached. You can try again in 1 minute. Trying again before that will only increase the time you have to wait before the ban is lifted. If you think this is in error, contact support.", comment: "Message to show when a request for a WP.com API endpoint is throttled")
userInfo[NSLocalizedDescriptionKey] = userInfo[WordPressComRestApi.ErrorKeyErrorMessage]
let nsError = WordPressComRestApiError.tooManyRequests as NSError
let errorWithLocalizedMessage = NSError(domain: nsError.domain, code: nsError.code, userInfo: userInfo)
return errorWithLocalizedMessage

let message = NSLocalizedString("Limit reached. You can try again in 1 minute. Trying again before that will only increase the time you have to wait before the ban is lifted. If you think this is in error, contact support.", comment: "Message to show when a request for a WP.com API endpoint is throttled")
Copy link
Contributor

Choose a reason for hiding this comment

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

Good occasion to add key as well? I mean, I understand this is a refactor so behavior should not change, but I think we can squeeze this in:

Suggested change
let message = NSLocalizedString("Limit reached. You can try again in 1 minute. Trying again before that will only increase the time you have to wait before the ban is lifted. If you think this is in error, contact support.", comment: "Message to show when a request for a WP.com API endpoint is throttled")
let message = NSLocalizedString(
"wordpresskit.api.message.endpoint_throttled",
value: "Limit reached. You can try again in 1 minute. Trying again before that will only increase the time you have to wait before the ban is lifted. If you think this is in error, contact support.",
comment: "Message to show when a request for a WP.com API endpoint is throttled"
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code squeezed. 3cc7a80

return .init(
code: .tooManyRequests,
apiErrorCode: "too_many_requests",
apiErrorMessage: message
)
}
}
// MARK: - Anonymous API support
Expand Down