diff --git a/CHANGELOG.md b/CHANGELOG.md index 3abe34af..d0509667 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,7 @@ _None._ ### Breaking Changes -_None._ +- `WordPressComRestApiError` is renamed to `WordPressRestApiErrorCode`, and no longer conforms to `Swift.Error`. [#696] ### New Features diff --git a/WordPressKit.xcodeproj/project.pbxproj b/WordPressKit.xcodeproj/project.pbxproj index f3bff504..8ccaa05e 100644 --- a/WordPressKit.xcodeproj/project.pbxproj +++ b/WordPressKit.xcodeproj/project.pbxproj @@ -155,6 +155,10 @@ 4A1DEF44293051BC00322608 /* LoggingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A1DEF43293051BC00322608 /* LoggingTests.swift */; }; 4A1DEF46293051C600322608 /* LoggingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A1DEF45293051C600322608 /* LoggingTests.m */; }; 4A40F6552B2A5A1A0015DA77 /* WordPressAPIErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A40F6542B2A5A1A0015DA77 /* WordPressAPIErrorTests.swift */; }; + 4A57A6812B549144008D0660 /* WordPressComRestApiTests+Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A57A6802B549144008D0660 /* WordPressComRestApiTests+Error.swift */; }; + 4A57A6832B54A326008D0660 /* WordPressAPIError+NSErrorBrdige.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A57A6822B54A326008D0660 /* WordPressAPIError+NSErrorBrdige.swift */; }; + 4A57A6872B54C68C008D0660 /* Constants.h in Headers */ = {isa = PBXBuildFile; fileRef = 4A57A6852B54C68C008D0660 /* Constants.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 4A57A6882B54C68C008D0660 /* Constants.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A57A6862B54C68C008D0660 /* Constants.m */; }; 4A68E3CD29404181004AC3DC /* RemoteBlog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A68E3CC29404181004AC3DC /* RemoteBlog.swift */; }; 4A68E3CF29404289004AC3DC /* RemoteBlogOptionsHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A68E3CE29404289004AC3DC /* RemoteBlogOptionsHelper.swift */; }; 4A68E3D329406AA0004AC3DC /* RemoteMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A68E3D029406AA0004AC3DC /* RemoteMenu.swift */; }; @@ -865,6 +869,10 @@ 4A1DEF43293051BC00322608 /* LoggingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingTests.swift; sourceTree = ""; }; 4A1DEF45293051C600322608 /* LoggingTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LoggingTests.m; sourceTree = ""; }; 4A40F6542B2A5A1A0015DA77 /* WordPressAPIErrorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordPressAPIErrorTests.swift; sourceTree = ""; }; + 4A57A6802B549144008D0660 /* WordPressComRestApiTests+Error.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WordPressComRestApiTests+Error.swift"; sourceTree = ""; }; + 4A57A6822B54A326008D0660 /* WordPressAPIError+NSErrorBrdige.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WordPressAPIError+NSErrorBrdige.swift"; sourceTree = ""; }; + 4A57A6852B54C68C008D0660 /* Constants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Constants.h; sourceTree = ""; }; + 4A57A6862B54C68C008D0660 /* Constants.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Constants.m; sourceTree = ""; }; 4A68E3CC29404181004AC3DC /* RemoteBlog.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteBlog.swift; sourceTree = ""; }; 4A68E3CE29404289004AC3DC /* RemoteBlogOptionsHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteBlogOptionsHelper.swift; sourceTree = ""; }; 4A68E3D029406AA0004AC3DC /* RemoteMenu.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteMenu.swift; sourceTree = ""; }; @@ -1846,6 +1854,7 @@ FFE247A620C891D1002DF3A2 /* WordPressComOAuthClientTests.swift */, 74B335D91F06F3D60053A184 /* WordPressComRestApiTests.swift */, 73B3DAD521FBB20D00B2CF18 /* WordPressComRestApiTests+Locale.swift */, + 4A57A6802B549144008D0660 /* WordPressComRestApiTests+Error.swift */, FFA4D4A82423B10A00BF5180 /* WordPressOrgRestApiTests.swift */, 74B335DB1F06F4180053A184 /* WordPressOrgXMLRPCApiTests.swift */, 740B23D51F17F7C100067A2A /* XMLRPCTestable.swift */, @@ -2465,6 +2474,7 @@ 4A11239D2B1926D1004690CF /* HTTPClient.swift */, 4A11239B2B1926B7004690CF /* HTTPRequestBuilder.swift */, 4A1123992B19269A004690CF /* WordPressAPIError.swift */, + 4A57A6822B54A326008D0660 /* WordPressAPIError+NSErrorBrdige.swift */, ); name = WordPressAPI; sourceTree = ""; @@ -2484,6 +2494,8 @@ 4A11239F2B196821004690CF /* MultipartForm.swift */, 4AE278432B2FAF6200E4D9B1 /* HTTPProtocolHelpers.swift */, 3F391E192B50F3EB007975C4 /* Result+Callback.swift */, + 4A57A6852B54C68C008D0660 /* Constants.h */, + 4A57A6862B54C68C008D0660 /* Constants.m */, ); name = Utility; sourceTree = ""; @@ -2767,6 +2779,7 @@ 740B23C41F17EE8000067A2A /* RemotePost.h in Headers */, 740B23C21F17EE8000067A2A /* RemotePostCategory.h in Headers */, B5A4822F20AC6C1A009D95F6 /* WPKitLogging.h in Headers */, + 4A57A6872B54C68C008D0660 /* Constants.h in Headers */, 9309995B1F16616A00F006A1 /* RemoteTheme.h in Headers */, 1A4F98672279A87D00D86E8E /* WPKit-Swift.h in Headers */, 93F50A371F226B9300B5BEBA /* WordPressComServiceRemote.h in Headers */, @@ -3334,6 +3347,7 @@ 93BD277E1EE73944002BB00B /* NSDate+WordPressJSON.m in Sources */, 8B2F4BED24ABCAEF0056C08A /* Decodable+Dictionary.swift in Sources */, E194CB731FBDEF6500B0A8B8 /* PluginState.swift in Sources */, + 4A57A6882B54C68C008D0660 /* Constants.m in Sources */, 404057D6221C92660060250C /* StatsTopClicksTimeIntervalData.swift in Sources */, 9AF4F2FC218331DC00570E4B /* PostServiceRemoteREST+Revisions.swift in Sources */, F4B0F4732ACAF498003ABC61 /* DomainsServiceRemote+AllDomains.swift in Sources */, @@ -3431,6 +3445,7 @@ 436D563C2118E18D00CEAA33 /* WPState.swift in Sources */, 439A44DA2107C93000795ED7 /* RemotePlan_ApiVersion1_3.swift in Sources */, 93BD27811EE73944002BB00B /* WordPressOrgXMLRPCApi.swift in Sources */, + 4A57A6832B54A326008D0660 /* WordPressAPIError+NSErrorBrdige.swift in Sources */, 439A44D62107C66A00795ED7 /* JSONDecoderExtension.swift in Sources */, B5A4822B20AC6C0B009D95F6 /* WPKitLogging.swift in Sources */, B5A4822E20AC6C1A009D95F6 /* WPKitLogging.m in Sources */, @@ -3557,6 +3572,7 @@ FFE247A720C891D1002DF3A2 /* WordPressComOAuthClientTests.swift in Sources */, 93AB06041EE8838400EF8764 /* RemoteTestCase.swift in Sources */, 4A40F6552B2A5A1A0015DA77 /* WordPressAPIErrorTests.swift in Sources */, + 4A57A6812B549144008D0660 /* WordPressComRestApiTests+Error.swift in Sources */, BA2A78FA24A486D300BB6F53 /* SitePluginTests.swift in Sources */, 465F88A7263B371D00F4C950 /* BlockEditorSettingsServiceRemoteTests.swift in Sources */, 93BD27411EE73311002BB00B /* AccountServiceRemoteRESTTests.swift in Sources */, diff --git a/WordPressKit/ActivityServiceRemote.swift b/WordPressKit/ActivityServiceRemote.swift index a17260cc..c8c09ecd 100644 --- a/WordPressKit/ActivityServiceRemote.swift +++ b/WordPressKit/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 obsolote as soon as the backend // stops returning 412's for those sites. - if let error = error as? WordPressComRestApiError, error == WordPressComRestApiError.preconditionFailure { + if error.domain == WordPressComRestApiEndpointError.errorDomain, error.code == WordPressComRestApiErrorCode.preconditionFailure.rawValue { let status = RewindStatus(state: .unavailable) success(status) return diff --git a/WordPressKit/Constants.h b/WordPressKit/Constants.h new file mode 100644 index 00000000..92d10365 --- /dev/null +++ b/WordPressKit/Constants.h @@ -0,0 +1,7 @@ +#import + +NS_ASSUME_NONNULL_BEGIN + +FOUNDATION_EXTERN NSString *WordPressComRestApiErrorDomain; + +NS_ASSUME_NONNULL_END diff --git a/WordPressKit/Constants.m b/WordPressKit/Constants.m new file mode 100644 index 00000000..87c8320f --- /dev/null +++ b/WordPressKit/Constants.m @@ -0,0 +1,7 @@ +#import "Constants.h" + +/// Error domain of `NSError` instances that are converted from `WordPressComRestApiEndpointError` +/// and `WordPressAPIError` instances. +/// +/// See `extension WordPressComRestApiEndpointError: CustomNSError` for context. +NSString *WordPressComRestApiErrorDomain = @"WordPressKit.WordPressComRestApiError"; diff --git a/WordPressKit/MediaServiceRemoteREST.m b/WordPressKit/MediaServiceRemoteREST.m index a5475dc9..c8509e79 100644 --- a/WordPressKit/MediaServiceRemoteREST.m +++ b/WordPressKit/MediaServiceRemoteREST.m @@ -244,7 +244,7 @@ - (NSError *)processMediaUploadErrors:(NSArray *)errorList { errorMessage = errorInfo[@"message"]; } NSDictionary *errorDictionary = @{NSLocalizedDescriptionKey: errorMessage}; - error = [NSError errorWithDomain:WordPressComRestApiErrorDomain code:WordPressComRestApiErrorUploadFailed userInfo:errorDictionary]; + error = [NSError errorWithDomain:WordPressComRestApiErrorDomain code:WordPressComRestApiErrorCodeUploadFailed userInfo:errorDictionary]; } return error; } @@ -297,7 +297,7 @@ - (void)deleteMedia:(RemoteMedia *)media } else { if (failure) { NSError *error = [NSError errorWithDomain:WordPressComRestApiErrorDomain - code:WordPressComRestApiErrorUnknown + code:WordPressComRestApiErrorCodeUnknown userInfo:nil]; failure(error); } @@ -370,7 +370,7 @@ -(void)getVideoPressToken:(NSString *)videoPressID } else { if (failure) { NSError *error = [NSError errorWithDomain:WordPressComRestApiErrorDomain - code:WordPressComRestApiErrorUnknown + code:WordPressComRestApiErrorCodeUnknown userInfo:nil]; failure(error); } diff --git a/WordPressKit/WordPressAPIError+NSErrorBrdige.swift b/WordPressKit/WordPressAPIError+NSErrorBrdige.swift new file mode 100644 index 00000000..8c412429 --- /dev/null +++ b/WordPressKit/WordPressAPIError+NSErrorBrdige.swift @@ -0,0 +1,114 @@ +import Foundation + +/// Custom `NSError` bridge implementation. +/// +/// The implementation ensures `NSError` instances that are casted from `WordPressAPIError.endpointError` +/// are the same as those instances that are casted directly from the underlying `EndpointError` instances. +/// +/// In theory, we should not need to implement this bridging, because we should never cast errors to `NSError` +/// instances in any error handling code. But since there are still Objective-C callers, providing this custom bridging +/// implementation comes in handy for those cases. See the `WordPressComRestApiEndpointError` extension below. +extension WordPressAPIError: CustomNSError { + + public static var errorDomain: String { + (EndpointError.self as? CustomNSError.Type)?.errorDomain + ?? String(describing: Self.self) + } + + public var errorCode: Int { + switch self { + case let .endpointError(endpointError): + return (endpointError as NSError).code + // Use negative values for other cases to reduce chances of collision with `EndpointError`. + case .requestEncodingFailure: + return -100000 + case .connection: + return -100001 + case .unacceptableStatusCode: + return -100002 + case .unparsableResponse: + return -100003 + case .unknown: + return -100004 + } + } + + public var errorUserInfo: [String: Any] { + switch self { + case let .endpointError(endpointError): + return (endpointError as NSError).userInfo + case .requestEncodingFailure, .connection, .unacceptableStatusCode, .unparsableResponse, + .unknown: + return [:] + } + + } + +} + +// MARK: - Bridge WordPressComRestApiEndpointError to NSError + +/// A custom NSError bridge implementation to ensure `NSError` instances converted from `WordPressComRestApiEndpointError` +/// are the same as the ones converted from their underlying error (the `code: WordPressComRestApiError` property in +/// `WordPressComRestApiEndpointError`). +/// +/// Along with `WordPressAPIError`'s conformance to `CustomNSError`, the three `NSError` instances below have the +/// same domain and code. +/// +/// ``` +/// let error: WordPressComRestApiError = // ... +/// let newError: WordPressComRestApiEndpointError = .init(code: error) +/// let apiError: WordPressAPIError = .endpointError(newError) +/// +/// // Following `NSError` instance have the same domain and code. +/// let errorNSError = error as NSError +/// let newErrorNSError = newError as NSError +/// let apiErrorNSError = apiError as NSError +/// ``` +/// +/// ## Why implementing this custom NSError brdige? +/// +/// `WordPressComRestApi` returns `NSError` instances to their callers. Since `WordPressComRestApi` is used in many +/// Objective-C file, we can't change the API to return an `Error` type that's Swift-only (i.e. `WordPressAPIError`). +/// If the day where there are no Objective-C callers finally comes, we definitely should stop returning `NSError` and +/// start using a concrete error type instead. But for now, we have to provide backwards compatiblity to those +/// Objective-C code while using `WordPressAPIError` internally in `WordPressComRestApi`. +/// +/// The `NSError` instances returned by `WordPressComRestApi` is one of the following: +/// - `WordPressComRestApiError` enum cases that are directly converted to `NSError` +/// - `NSError` instances that have domain and code from `WordPressComRestApiError`, with additional `userInfo` (error +/// code, message, etc). +/// - Error instances returned by Alamofire 4: `AFError`, or maybe other errors. +/// +/// Alamofire will be removed from this library, there is no point (also not possible) in providing backwards +/// compatiblity to `AFError`. That means, we need to make sure the `NSError` instances that are converted from +/// `WordPressAPIError` have the same error domain and code as the underlying `WordPressComRestApiError` enum. +/// And in cases where additional user info was provided, they need to be carried over to the `NSError` instances. +extension WordPressComRestApiEndpointError: CustomNSError { + + // This value is the same as the `WordPressComRestApiErrorDomain` constant generated by Swift compiler. + public static let errorDomain = "WordPressKit.WordPressComRestApiError" + + public var errorCode: Int { + code.rawValue + } + + public var errorUserInfo: [String: Any] { + var userInfo = additionalUserInfo ?? [:] + + if let code = apiErrorCode { + userInfo[WordPressComRestApi.ErrorKeyErrorCode] = code + } + if let message = apiErrorMessage { + userInfo[WordPressComRestApi.ErrorKeyErrorMessage] = message + userInfo[NSLocalizedDescriptionKey] = message + } + if let data = apiErrorData { + userInfo[WordPressComRestApi.ErrorKeyErrorData] = data + } + + return userInfo + + } + +} diff --git a/WordPressKit/WordPressComRestApi.swift b/WordPressKit/WordPressComRestApi.swift index a4fd6dda..a6dc9312 100644 --- a/WordPressKit/WordPressComRestApi.swift +++ b/WordPressKit/WordPressComRestApi.swift @@ -14,7 +14,7 @@ import Alamofire - RequestSerializationFailed: The serialization of the request failed - Unknown: Unknow error happen */ -@objc public enum WordPressComRestApiError: Int, Error { +@objc public enum WordPressComRestApiErrorCode: Int, CaseIterable { case invalidInput case invalidToken case authorizationRequired @@ -28,6 +28,22 @@ import Alamofire case invalidQuery } +public struct WordPressComRestApiEndpointError: Error { + public var code: WordPressComRestApiErrorCode + + public var apiErrorCode: String? + public var apiErrorMessage: String? + public var apiErrorData: AnyObject? + + var additionalUserInfo: [String: Any]? +} + +extension WordPressComRestApiEndpointError: LocalizedError { + public var errorDescription: String? { + apiErrorMessage + } +} + public enum ResponseType { case json case data @@ -205,8 +221,8 @@ open class WordPressComRestApi: NSObject { progress?.completedUnitCount = progress?.totalUnitCount ?? 0 success(responseObject as AnyObject, response.response) case .failure(let error): - let nserror = self.processError(response: response, originalError: error) - failure(nserror, response.response) + let processedError = self.processError(response: response, originalError: error).flatMap { $0 as NSError } + failure(processedError ?? (error as NSError), response.response) } }).downloadProgress(closure: progressUpdater) progress.sessionTask = dataRequest.task @@ -341,8 +357,8 @@ open class WordPressComRestApi: NSObject { progress.completedUnitCount = progress.totalUnitCount success(responseObject as AnyObject, response.response) case .failure(let error): - let nserror = self.processError(response: response, originalError: error) - failure(nserror, response.response) + let processedError = self.processError(response: response, originalError: error).flatMap { $0 as NSError } + failure(processedError ?? (error as NSError), response.response) } }).uploadProgress(closure: progressUpdater) @@ -463,31 +479,27 @@ public final class FilePart: NSObject { extension WordPressComRestApi { /// A custom error processor to handle error responses when status codes are betwen 400 and 500 - func processError(response: DataResponse, originalError: Error) -> NSError { - - let originalNSError = originalError as NSError + func processError(response: DataResponse, originalError: Error) -> WordPressComRestApiEndpointError? { guard let afError = originalError as? AFError, case AFError.responseValidationFailed(_) = afError, let httpResponse = response.response, (400...500).contains(httpResponse.statusCode), let data = response.data else { if let afError = originalError as? AFError, case AFError.responseSerializationFailed(_) = afError { - return WordPressComRestApiError.responseSerializationFailed as NSError + return .init(code: .responseSerializationFailed) } - return originalNSError + return nil } - var userInfo: [String: Any] = originalNSError.userInfo - guard let responseObject = try? JSONSerialization.jsonObject(with: data, options: .allowFragments), let responseDictionary = responseObject as? [String: AnyObject] else { if let error = checkForThrottleErrorIn(data: data) { return error } - return WordPressComRestApiError.unknown as NSError + return .init(code: .unknown) } // 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. if httpResponse.statusCode == 412, let code = responseDictionary["code"] as? String, code == "no_connected_jetpack" { - return WordPressComRestApiError.preconditionFailure as NSError + return .init(code: .preconditionFailure) } var errorDictionary: AnyObject? = responseDictionary as AnyObject? @@ -498,52 +510,53 @@ extension WordPressComRestApi { let errorCode = errorEntry["error"] as? String, let errorDescription = errorEntry["message"] as? String else { - return WordPressComRestApiError.unknown as NSError + return .init(code: .unknown) } - let errorsMap = [ - "invalid_input": WordPressComRestApiError.invalidInput, - "invalid_token": WordPressComRestApiError.invalidToken, - "authorization_required": WordPressComRestApiError.authorizationRequired, - "upload_error": WordPressComRestApiError.uploadFailed, - "unauthorized": WordPressComRestApiError.authorizationRequired, - "invalid_query": WordPressComRestApiError.invalidQuery + let errorsMap: [String: WordPressComRestApiErrorCode] = [ + "invalid_input": .invalidInput, + "invalid_token": .invalidToken, + "authorization_required": .authorizationRequired, + "upload_error": .uploadFailed, + "unauthorized": .authorizationRequired, + "invalid_query": .invalidQuery ] - let mappedError = errorsMap[errorCode] ?? WordPressComRestApiError.unknown + let mappedError = errorsMap[errorCode] ?? .unknown if mappedError == .invalidToken { invalidTokenHandler?() } - userInfo[WordPressComRestApi.ErrorKeyErrorCode] = errorCode - userInfo[WordPressComRestApi.ErrorKeyErrorMessage] = errorDescription - userInfo[NSLocalizedDescriptionKey] = errorDescription - if let errorData = errorEntry["data"] { - userInfo[WordPressComRestApi.ErrorKeyErrorData] = errorData - } + var originalErrorUserInfo = (originalError as NSError).userInfo + originalErrorUserInfo.removeValue(forKey: NSLocalizedDescriptionKey) - let nserror = mappedError as NSError - let resultError = NSError(domain: nserror.domain, - code: nserror.code, - userInfo: userInfo - ) - return resultError + return .init( + code: mappedError, + apiErrorCode: errorCode, + apiErrorMessage: errorDescription, + apiErrorData: errorEntry["data"], + additionalUserInfo: originalErrorUserInfo + ) } - 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( + "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" + ) + return .init( + code: .tooManyRequests, + apiErrorCode: "too_many_requests", + apiErrorMessage: message + ) } } // MARK: - Anonymous API support @@ -586,8 +599,8 @@ extension WordPressComRestApi: WordPressRestApi { private extension WordPressComRestApi { enum Constants { - static let buildRequestError = NSError(domain: String(describing: WordPressComRestApiError.self), - code: WordPressComRestApiError.requestSerializationFailed.rawValue, + static let buildRequestError = NSError(domain: WordPressComRestApiEndpointError.errorDomain, + code: WordPressComRestApiErrorCode.requestSerializationFailed.rawValue, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Failed to serialize request to the REST API.", comment: "Error message to show when wrong URL format is used to access the REST API")]) } diff --git a/WordPressKit/WordPressComServiceRemote.m b/WordPressKit/WordPressComServiceRemote.m index 5d77127f..3013c921 100644 --- a/WordPressKit/WordPressComServiceRemote.m +++ b/WordPressKit/WordPressComServiceRemote.m @@ -180,7 +180,7 @@ - (void)createWPComBlogWithUrl:(NSString *)blogUrl userInfo[WordPressComRestApi.ErrorKeyErrorMessage] = localizedErrorMessage; userInfo[NSLocalizedDescriptionKey] = localizedErrorMessage; NSError *errorWithLocalizedMessage = [[NSError alloc] initWithDomain:WordPressComRestApiErrorDomain - code:WordPressComRestApiErrorUnknown + code:WordPressComRestApiErrorCodeUnknown userInfo:userInfo]; failure(errorWithLocalizedMessage); diff --git a/WordPressKit/WordPressKit.h b/WordPressKit/WordPressKit.h index 2fe628dd..bec245f2 100644 --- a/WordPressKit/WordPressKit.h +++ b/WordPressKit/WordPressKit.h @@ -51,3 +51,4 @@ FOUNDATION_EXPORT const unsigned char WordPressKitVersionString[]; #import #import +#import diff --git a/WordPressKitTests/AccountServiceRemoteRESTTests.swift b/WordPressKitTests/AccountServiceRemoteRESTTests.swift index d14dcd36..67a70717 100644 --- a/WordPressKitTests/AccountServiceRemoteRESTTests.swift +++ b/WordPressKitTests/AccountServiceRemoteRESTTests.swift @@ -91,7 +91,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -112,7 +112,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -209,7 +209,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -230,7 +230,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -251,7 +251,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -272,7 +272,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -353,7 +353,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -374,7 +374,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -427,7 +427,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorizationRequired") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorizationRequired") expect.fulfill() }) @@ -449,7 +449,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -517,7 +517,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -555,7 +555,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -607,7 +607,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -644,7 +644,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -665,7 +665,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -686,7 +686,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -708,7 +708,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -745,7 +745,7 @@ class AccountServiceRemoteRESTTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) diff --git a/WordPressKitTests/AccountSettingsRemoteTests.swift b/WordPressKitTests/AccountSettingsRemoteTests.swift index f08d0bdd..3d2c79e1 100644 --- a/WordPressKitTests/AccountSettingsRemoteTests.swift +++ b/WordPressKitTests/AccountSettingsRemoteTests.swift @@ -90,7 +90,7 @@ class AccountSettingsRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -107,7 +107,7 @@ class AccountSettingsRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -260,7 +260,7 @@ class AccountSettingsRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -293,7 +293,7 @@ class AccountSettingsRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.invalidInput.rawValue, "The error code should be 0 - invalid input") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.invalidInput.rawValue, "The error code should be 0 - invalid input") expect.fulfill() }) diff --git a/WordPressKitTests/ActivityServiceRemoteTests.swift b/WordPressKitTests/ActivityServiceRemoteTests.swift index dd0358c6..e405138c 100644 --- a/WordPressKitTests/ActivityServiceRemoteTests.swift +++ b/WordPressKitTests/ActivityServiceRemoteTests.swift @@ -168,7 +168,7 @@ class ActivityServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) diff --git a/WordPressKitTests/DomainsServiceRemoteRESTTests.swift b/WordPressKitTests/DomainsServiceRemoteRESTTests.swift index 0265f5a6..5e5f0289 100644 --- a/WordPressKitTests/DomainsServiceRemoteRESTTests.swift +++ b/WordPressKitTests/DomainsServiceRemoteRESTTests.swift @@ -88,7 +88,7 @@ class DomainsServiceRemoteRESTTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -105,7 +105,7 @@ class DomainsServiceRemoteRESTTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -237,7 +237,7 @@ class DomainsServiceRemoteRESTTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.invalidQuery.rawValue, "The error code should be 10 - invalid_query") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.invalidQuery.rawValue, "The error code should be 10 - invalid_query") expect.fulfill() }) diff --git a/WordPressKitTests/NotificationSyncServiceRemoteTests.swift b/WordPressKitTests/NotificationSyncServiceRemoteTests.swift index b6acb7e1..57ada0ed 100644 --- a/WordPressKitTests/NotificationSyncServiceRemoteTests.swift +++ b/WordPressKitTests/NotificationSyncServiceRemoteTests.swift @@ -77,7 +77,7 @@ class NotificationSyncServiceRemoteTests: RemoteTestCase, RESTTestable { } XCTAssertNil(notes) XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() } waitForExpectations(timeout: timeout, handler: nil) diff --git a/WordPressKitTests/PeopleServiceRemoteTests.swift b/WordPressKitTests/PeopleServiceRemoteTests.swift index 89409efc..9ac254eb 100644 --- a/WordPressKitTests/PeopleServiceRemoteTests.swift +++ b/WordPressKitTests/PeopleServiceRemoteTests.swift @@ -104,7 +104,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -121,7 +121,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -138,7 +138,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.invalidInput.rawValue, "The error code should be 0 - invalid input") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.invalidInput.rawValue, "The error code should be 0 - invalid input") expect.fulfill() }) @@ -170,7 +170,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -204,7 +204,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -221,7 +221,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -253,7 +253,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -327,7 +327,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { // Then let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization required") expect.fulfill() }) @@ -347,7 +347,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { // Then let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -367,7 +367,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { // Then let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) waitForExpectations(timeout: timeout, handler: nil) @@ -386,7 +386,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { // Then let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) waitForExpectations(timeout: timeout, handler: nil) @@ -421,7 +421,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { // Then let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -486,7 +486,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -503,7 +503,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -553,7 +553,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -570,7 +570,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -648,7 +648,7 @@ class PeopleServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) diff --git a/WordPressKitTests/PlanServiceRemoteTests.swift b/WordPressKitTests/PlanServiceRemoteTests.swift index 845c96a2..62cfcd48 100644 --- a/WordPressKitTests/PlanServiceRemoteTests.swift +++ b/WordPressKitTests/PlanServiceRemoteTests.swift @@ -120,7 +120,7 @@ class PlanServiceRemoteTests: RemoteTestCase, RESTTestable { }, failure: { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -248,7 +248,7 @@ class PlanServiceRemoteTests: RemoteTestCase, RESTTestable { }) { error in let error = error as NSError XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() } diff --git a/WordPressKitTests/PluginServiceRemoteTests.swift b/WordPressKitTests/PluginServiceRemoteTests.swift index 149b94f3..cc1356c3 100644 --- a/WordPressKitTests/PluginServiceRemoteTests.swift +++ b/WordPressKitTests/PluginServiceRemoteTests.swift @@ -93,8 +93,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }, failure: { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() }) @@ -133,8 +133,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }) { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() } @@ -276,8 +276,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }) { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() } @@ -358,8 +358,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }) { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() } @@ -378,8 +378,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }) { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() } @@ -398,8 +398,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }) { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() } @@ -418,8 +418,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }) { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() } @@ -438,8 +438,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }) { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() } @@ -458,8 +458,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }) { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() } @@ -478,8 +478,8 @@ class PluginServiceRemoteTests: RemoteTestCase, RESTTestable { expect.fulfill() }) { (error) in let error = error as NSError - let expected = WordPressComRestApiError.responseSerializationFailed as NSError - XCTAssertEqual(error, expected) + XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.responseSerializationFailed.rawValue) expect.fulfill() } diff --git a/WordPressKitTests/SiteManagementServiceRemoteTests.swift b/WordPressKitTests/SiteManagementServiceRemoteTests.swift index 923c1423..a3700dc5 100644 --- a/WordPressKitTests/SiteManagementServiceRemoteTests.swift +++ b/WordPressKitTests/SiteManagementServiceRemoteTests.swift @@ -82,7 +82,7 @@ class SiteManagementServiceRemoteTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -103,7 +103,7 @@ class SiteManagementServiceRemoteTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -217,7 +217,7 @@ class SiteManagementServiceRemoteTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -238,7 +238,7 @@ class SiteManagementServiceRemoteTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) @@ -362,7 +362,7 @@ class SiteManagementServiceRemoteTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue, "The error code should be 7 - unknown") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue, "The error code should be 7 - unknown") expect.fulfill() }) @@ -383,7 +383,7 @@ class SiteManagementServiceRemoteTests: RemoteTestCase, RESTTestable { return } XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.authorizationRequired.rawValue, "The error code should be 2 - authorization_required") expect.fulfill() }) diff --git a/WordPressKitTests/WordPressComRestApiTests+Error.swift b/WordPressKitTests/WordPressComRestApiTests+Error.swift new file mode 100644 index 00000000..b8d3a58b --- /dev/null +++ b/WordPressKitTests/WordPressComRestApiTests+Error.swift @@ -0,0 +1,22 @@ +import Foundation +import XCTest + +@testable import WordPressKit + +class WordPressComRestApiErrorTests: XCTestCase { + + func testNSErrorBridging() { + for error in WordPressComRestApiErrorCode.allCases { + let apiError = WordPressAPIError.endpointError(WordPressComRestApiEndpointError(code: error)) + let newNSError = apiError as NSError + + XCTAssertEqual(newNSError.domain, "WordPressKit.WordPressComRestApiError") + XCTAssertEqual(newNSError.code, error.rawValue) + } + } + + func testErrorDomain() { + XCTAssertEqual(WordPressComRestApiErrorDomain, WordPressComRestApiEndpointError.errorDomain) + } + +} diff --git a/WordPressKitTests/WordPressComRestApiTests.swift b/WordPressKitTests/WordPressComRestApiTests.swift index b9321ca0..3202eeb7 100644 --- a/WordPressKitTests/WordPressComRestApiTests.swift +++ b/WordPressKitTests/WordPressComRestApiTests.swift @@ -103,7 +103,7 @@ class WordPressComRestApiTests: XCTestCase { }, failure: { (error, _) in expect.fulfill() XCTAssert(error.domain == "WordPressKit.WordPressComRestApiError", "The error should a WordPressComRestApiError") - XCTAssert(error.code == Int(WordPressComRestApiError.invalidToken.rawValue), "The error code should be invalid token") + XCTAssert(error.code == Int(WordPressComRestApiErrorCode.invalidToken.rawValue), "The error code should be invalid token") }) self.waitForExpectations(timeout: 2, handler: nil) } @@ -121,7 +121,7 @@ class WordPressComRestApiTests: XCTestCase { }, failure: { (error, _) in expect.fulfill() XCTAssert(error.domain == WordPressComRestApiErrorDomain, "The error domain should be WordPressComRestApiErrorDomain") - XCTAssert(error.code == Int(WordPressComRestApiError.responseSerializationFailed.rawValue), "The code should be invalid response serialization") + XCTAssert(error.code == Int(WordPressComRestApiErrorCode.responseSerializationFailed.rawValue), "The code should be invalid response serialization") }) self.waitForExpectations(timeout: 2, handler: nil) } @@ -139,7 +139,7 @@ class WordPressComRestApiTests: XCTestCase { }, failure: { (error, _) in expect.fulfill() XCTAssert(error.domain == "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssert(error.code == Int(WordPressComRestApiError.invalidInput.rawValue), "The error code should be invalid input") + XCTAssert(error.code == Int(WordPressComRestApiErrorCode.invalidInput.rawValue), "The error code should be invalid input") }) self.waitForExpectations(timeout: 2, handler: nil) } @@ -157,7 +157,7 @@ class WordPressComRestApiTests: XCTestCase { }, failure: { (error, _) in expect.fulfill() XCTAssert(error.domain == "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssert(error.code == Int(WordPressComRestApiError.authorizationRequired.rawValue), "The error code should be AuthorizationRequired") + XCTAssert(error.code == Int(WordPressComRestApiErrorCode.authorizationRequired.rawValue), "The error code should be AuthorizationRequired") }) self.waitForExpectations(timeout: 2, handler: nil) } @@ -175,7 +175,7 @@ class WordPressComRestApiTests: XCTestCase { }, failure: { (error, _) in expect.fulfill() XCTAssert(error.domain == "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssert(error.code == Int(WordPressComRestApiError.uploadFailed.rawValue), "The error code should be AuthorizationRequired") + XCTAssert(error.code == Int(WordPressComRestApiErrorCode.uploadFailed.rawValue), "The error code should be AuthorizationRequired") }) self.waitForExpectations(timeout: 2, handler: nil) } @@ -193,7 +193,7 @@ class WordPressComRestApiTests: XCTestCase { }, failure: { (error, _) in expect.fulfill() XCTAssert(error.domain == "WordPressKit.WordPressComRestApiError", "The error domain should be WordPressComRestApiError") - XCTAssert(error.code == Int(WordPressComRestApiError.uploadFailed.rawValue), "The error code should be AuthorizationRequired") + XCTAssert(error.code == Int(WordPressComRestApiErrorCode.uploadFailed.rawValue), "The error code should be AuthorizationRequired") }) self.waitForExpectations(timeout: 2, handler: nil) } @@ -321,7 +321,7 @@ class WordPressComRestApiTests: XCTestCase { case .failure(let err): let error = err as NSError XCTAssert(error.domain == WordPressComRestApiErrorDomain, "The error domain should be WordPressComRestApiErrorDomain") - XCTAssert(error.code == Int(WordPressComRestApiError.responseSerializationFailed.rawValue), "The code should be invalid response serialization") + XCTAssert(error.code == Int(WordPressComRestApiErrorCode.responseSerializationFailed.rawValue), "The code should be invalid response serialization") } } self.waitForExpectations(timeout: 2, handler: nil) @@ -344,7 +344,7 @@ class WordPressComRestApiTests: XCTestCase { failure: { error, _ in complete.fulfill() XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.unknown.rawValue) + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.unknown.rawValue) } ) @@ -392,7 +392,7 @@ class WordPressComRestApiTests: XCTestCase { failure: { error, _ in complete.fulfill() XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.tooManyRequests.rawValue) + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.tooManyRequests.rawValue) XCTAssertEqual(error.userInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String, "too_many_requests") XCTAssertTrue(error.localizedDescription.contains("You can try again in 1 minute")) } @@ -418,7 +418,7 @@ class WordPressComRestApiTests: XCTestCase { failure: { error, _ in complete.fulfill() XCTAssertEqual(error.domain, "WordPressKit.WordPressComRestApiError") - XCTAssertEqual(error.code, WordPressComRestApiError.preconditionFailure.rawValue) + XCTAssertEqual(error.code, WordPressComRestApiErrorCode.preconditionFailure.rawValue) } ) diff --git a/WordPressKitTests/WordPressComServiceRemoteRestTests.swift b/WordPressKitTests/WordPressComServiceRemoteRestTests.swift index 0fa66e5d..6c01f502 100644 --- a/WordPressKitTests/WordPressComServiceRemoteRestTests.swift +++ b/WordPressKitTests/WordPressComServiceRemoteRestTests.swift @@ -62,7 +62,7 @@ class WordPressComServiceRemoteRestTests: XCTestCase { expect.fulfill() let error = error! as NSError XCTAssert(error.domain == "WordPressKit.WordPressComRestApiError", "The error should a WordPressComRestApiError") - XCTAssert(error.code == Int(WordPressComRestApiError.tooManyRequests.rawValue), "The error code should be too many requests") + XCTAssert(error.code == Int(WordPressComRestApiErrorCode.tooManyRequests.rawValue), "The error code should be too many requests") }) self.waitForExpectations(timeout: 2, handler: nil) }