From 09b2b47c37579f4dc9a9e357218604431f4f712e Mon Sep 17 00:00:00 2001 From: "Tanner W. Stokes" Date: Tue, 7 Dec 2021 16:26:27 -0500 Subject: [PATCH 1/6] Add XML-RPC error descriptions for better readability. --- WordPressKit/WordPressOrgXMLRPCApi.swift | 31 +++++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/WordPressKit/WordPressOrgXMLRPCApi.swift b/WordPressKit/WordPressOrgXMLRPCApi.swift index be4b7a8b..a1f9027e 100644 --- a/WordPressKit/WordPressOrgXMLRPCApi.swift +++ b/WordPressKit/WordPressOrgXMLRPCApi.swift @@ -306,6 +306,14 @@ open class WordPressOrgXMLRPCApi: NSObject { userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyData] = data userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyDataString] = NSString(data: data, encoding: String.Encoding.utf8.rawValue) userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyStatusCode] = statusCode + + if let statusCode = statusCode, (400..<600).contains(statusCode) { + let formatString = NSLocalizedString("An HTTP error code %i was returned from the site.", comment: "An error status code returned from a HTTP server") + userInfo[NSLocalizedDescriptionKey] = String(format: formatString, statusCode) + } else { + userInfo[NSLocalizedDescriptionKey] = error.localizedDescription + } + return NSError(domain: error.domain, code: responseCode, userInfo: userInfo as? [String: Any]) } return error @@ -376,13 +384,28 @@ extension WordPressOrgXMLRPCApi { } /// Error constants for the WordPress XMLRPC API -/// - RequestSerializationFailed: The serialization of the request failed -/// - ResponseSerializationFailed: The serialization of the response failed -/// - Unknown: Unknow error happen -/// @objc public enum WordPressOrgXMLRPCApiError: Int, Error { + /// An error HTTP status code was returned. case httpErrorStatusCode + /// The serialization of the request failed. case requestSerializationFailed + /// The serialization of the response failed. case responseSerializationFailed + /// An unknown error occurred. case unknown } + +extension WordPressOrgXMLRPCApiError: LocalizedError { + public var errorDescription: String? { + switch self { + case .httpErrorStatusCode: + return NSLocalizedString("An HTTP error code was returned from the site.", comment: "Description of an error where an error HTTP status code was returned from a site") + case .requestSerializationFailed: + return NSLocalizedString("The serialization of the request failed.", comment: "Description of an error where a request couldn't be serialized") + case .responseSerializationFailed: + return NSLocalizedString("The serialization of the response failed.", comment: "Description of an error where a response couldn't be serialized") + case .unknown: + return NSLocalizedString("An unknown error occurred.", comment: "Description of an unknown error") + } + } +} From 06e31151bc6ea52296bac29bf96beb5e894e3c82 Mon Sep 17 00:00:00 2001 From: "Tanner W. Stokes" Date: Tue, 7 Dec 2021 16:27:20 -0500 Subject: [PATCH 2/6] Attempt to decode faults even when a non-200 is returned. --- WordPressKit/WordPressOrgXMLRPCApi.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/WordPressKit/WordPressOrgXMLRPCApi.swift b/WordPressKit/WordPressOrgXMLRPCApi.swift index a1f9027e..b3f682a2 100644 --- a/WordPressKit/WordPressOrgXMLRPCApi.swift +++ b/WordPressKit/WordPressOrgXMLRPCApi.swift @@ -274,7 +274,12 @@ open class WordPressOrgXMLRPCApi: NSObject { } if (400..<600).contains(httpResponse.statusCode) { - throw convertError(WordPressOrgXMLRPCApiError.httpErrorStatusCode as NSError, data: originalData, statusCode: httpResponse.statusCode) + if let decoder = WPXMLRPCDecoder(data: data), decoder.isFault(), let decoderError = decoder.error() { + // sometimes a decodable fault is provided for non-200 HTTP responses, against the XML-RPC spec + throw decoderError + } else { + throw convertError(WordPressOrgXMLRPCApiError.httpErrorStatusCode as NSError, data: originalData, statusCode: httpResponse.statusCode) + } } if ["application/xml", "text/xml"].filter({ (type) -> Bool in return contentType.hasPrefix(type)}).count == 0 { @@ -308,7 +313,7 @@ open class WordPressOrgXMLRPCApi: NSObject { userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyStatusCode] = statusCode if let statusCode = statusCode, (400..<600).contains(statusCode) { - let formatString = NSLocalizedString("An HTTP error code %i was returned from the site.", comment: "An error status code returned from a HTTP server") + let formatString = NSLocalizedString("An HTTP error code %i was returned from the site.", comment: "An error status code returned from an HTTP server") userInfo[NSLocalizedDescriptionKey] = String(format: formatString, statusCode) } else { userInfo[NSLocalizedDescriptionKey] = error.localizedDescription @@ -383,7 +388,7 @@ extension WordPressOrgXMLRPCApi { } } -/// Error constants for the WordPress XMLRPC API +/// Error constants for the WordPress XML-RPC API @objc public enum WordPressOrgXMLRPCApiError: Int, Error { /// An error HTTP status code was returned. case httpErrorStatusCode From aed87c38c65d2a4af7c7ca24632df0a223fa2eaa Mon Sep 17 00:00:00 2001 From: "Tanner W. Stokes" Date: Thu, 6 Jan 2022 17:01:04 -0500 Subject: [PATCH 3/6] Utilize NSLocalizedFailure keys. --- WordPressKit/WordPressOrgXMLRPCApi.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/WordPressKit/WordPressOrgXMLRPCApi.swift b/WordPressKit/WordPressOrgXMLRPCApi.swift index b3f682a2..335d9372 100644 --- a/WordPressKit/WordPressOrgXMLRPCApi.swift +++ b/WordPressKit/WordPressOrgXMLRPCApi.swift @@ -311,12 +311,13 @@ open class WordPressOrgXMLRPCApi: NSObject { userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyData] = data userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyDataString] = NSString(data: data, encoding: String.Encoding.utf8.rawValue) userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyStatusCode] = statusCode + userInfo[NSLocalizedFailureErrorKey] = error.localizedDescription if let statusCode = statusCode, (400..<600).contains(statusCode) { - let formatString = NSLocalizedString("An HTTP error code %i was returned from the site.", comment: "An error status code returned from an HTTP server") - userInfo[NSLocalizedDescriptionKey] = String(format: formatString, statusCode) + let formatString = NSLocalizedString("An HTTP error code %i was returned.", comment: "Description of a specific error status code returned from an HTTP server") + userInfo[NSLocalizedFailureReasonErrorKey] = String(format: formatString, statusCode) } else { - userInfo[NSLocalizedDescriptionKey] = error.localizedDescription + userInfo[NSLocalizedFailureReasonErrorKey] = error.localizedFailureReason } return NSError(domain: error.domain, code: responseCode, userInfo: userInfo as? [String: Any]) @@ -402,9 +403,13 @@ extension WordPressOrgXMLRPCApi { extension WordPressOrgXMLRPCApiError: LocalizedError { public var errorDescription: String? { + return "There was a problem communicating with the site." + } + + public var failureReason: String? { switch self { case .httpErrorStatusCode: - return NSLocalizedString("An HTTP error code was returned from the site.", comment: "Description of an error where an error HTTP status code was returned from a site") + return NSLocalizedString("An HTTP error code was returned.", comment: "Description of an error where an error HTTP status code was returned from a site") case .requestSerializationFailed: return NSLocalizedString("The serialization of the request failed.", comment: "Description of an error where a request couldn't be serialized") case .responseSerializationFailed: From 881ab088d6df3ef931fa1e640a333a57cf050d0e Mon Sep 17 00:00:00 2001 From: "Tanner W. Stokes" Date: Thu, 6 Jan 2022 17:03:59 -0500 Subject: [PATCH 4/6] Update comment. --- WordPressKit/WordPressOrgXMLRPCApi.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPressKit/WordPressOrgXMLRPCApi.swift b/WordPressKit/WordPressOrgXMLRPCApi.swift index 335d9372..7631a9c0 100644 --- a/WordPressKit/WordPressOrgXMLRPCApi.swift +++ b/WordPressKit/WordPressOrgXMLRPCApi.swift @@ -275,7 +275,8 @@ open class WordPressOrgXMLRPCApi: NSObject { if (400..<600).contains(httpResponse.statusCode) { if let decoder = WPXMLRPCDecoder(data: data), decoder.isFault(), let decoderError = decoder.error() { - // sometimes a decodable fault is provided for non-200 HTTP responses, against the XML-RPC spec + // when XML-RPC is disabled for authenticated calls (e.g. xmlrpc_enabled is false on WP.org), + // it will return a valid fault payload with a non-200 throw decoderError } else { throw convertError(WordPressOrgXMLRPCApiError.httpErrorStatusCode as NSError, data: originalData, statusCode: httpResponse.statusCode) From 0bd4dc8d4905b4f7130b380e82dbc7d175a70020 Mon Sep 17 00:00:00 2001 From: "Tanner W. Stokes" Date: Thu, 13 Jan 2022 00:28:05 -0500 Subject: [PATCH 5/6] Touch up localization. --- WordPressKit/WordPressOrgXMLRPCApi.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/WordPressKit/WordPressOrgXMLRPCApi.swift b/WordPressKit/WordPressOrgXMLRPCApi.swift index 7631a9c0..0d8282a7 100644 --- a/WordPressKit/WordPressOrgXMLRPCApi.swift +++ b/WordPressKit/WordPressOrgXMLRPCApi.swift @@ -315,7 +315,7 @@ open class WordPressOrgXMLRPCApi: NSObject { userInfo[NSLocalizedFailureErrorKey] = error.localizedDescription if let statusCode = statusCode, (400..<600).contains(statusCode) { - let formatString = NSLocalizedString("An HTTP error code %i was returned.", comment: "Description of a specific error status code returned from an HTTP server") + let formatString = NSLocalizedString("An HTTP error code %i was returned.", comment: "A failure reason for when an error HTTP status code was returned from the site, with the specific error code.") userInfo[NSLocalizedFailureReasonErrorKey] = String(format: formatString, statusCode) } else { userInfo[NSLocalizedFailureReasonErrorKey] = error.localizedFailureReason @@ -404,19 +404,19 @@ extension WordPressOrgXMLRPCApi { extension WordPressOrgXMLRPCApiError: LocalizedError { public var errorDescription: String? { - return "There was a problem communicating with the site." + return NSLocalizedString("There was a problem communicating with the site.", comment: "A general error message shown to the user when there was an API communication failure.") } public var failureReason: String? { switch self { case .httpErrorStatusCode: - return NSLocalizedString("An HTTP error code was returned.", comment: "Description of an error where an error HTTP status code was returned from a site") + return NSLocalizedString("An HTTP error code was returned.", comment: "A failure reason for when an error HTTP status code was returned from the site.") case .requestSerializationFailed: - return NSLocalizedString("The serialization of the request failed.", comment: "Description of an error where a request couldn't be serialized") + return NSLocalizedString("The serialization of the request failed.", comment: "A failure reason for when the request couldn't be serialized.") case .responseSerializationFailed: - return NSLocalizedString("The serialization of the response failed.", comment: "Description of an error where a response couldn't be serialized") + return NSLocalizedString("The serialization of the response failed.", comment: "A failure reason for when the response couldn't be serialized.") case .unknown: - return NSLocalizedString("An unknown error occurred.", comment: "Description of an unknown error") + return NSLocalizedString("An unknown error occurred.", comment: "A failure reason for when the error that occured wasn't able to be determined.") } } } From 36c22a8dc86beb347f7ba6f4b5eaf294982cae1a Mon Sep 17 00:00:00 2001 From: "Tanner W. Stokes" Date: Mon, 17 Jan 2022 21:08:51 -0500 Subject: [PATCH 6/6] Update Podspec to beta version. --- WordPressKit.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPressKit.podspec b/WordPressKit.podspec index eaf97e58..c110b07c 100644 --- a/WordPressKit.podspec +++ b/WordPressKit.podspec @@ -2,7 +2,7 @@ Pod::Spec.new do |s| s.name = 'WordPressKit' - s.version = '4.45.0' + s.version = '4.46.0-beta.1' s.summary = 'WordPressKit offers a clean and simple WordPress.com and WordPress.org API.' s.description = <<-DESC