-
Notifications
You must be signed in to change notification settings - Fork 16
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
Make WordPressOrgXMLRPCApiError
conform to CustomNSError
#790
base: trunk
Are you sure you want to change the base?
Changes from all commits
8fb7ca2
0d08c55
eeb26d8
277f0ca
f6f0c1c
ac15e9a
e8ee86f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
/// Error domain of `NSError` instances that are converted from `WordPressOrgXMLRPCApiError` | ||
/// and `WordPressAPIError<WordPressOrgXMLRPCApiError>` instances. | ||
/// | ||
/// This matches the compiler generated value and is used to ensure consistent error domain across error types and SPM or Framework build modes. | ||
/// | ||
/// See `extension WordPressComRestApiEndpointError: CustomNSError` in CoreAPI package for context. | ||
static NSString *const _Nonnull WordPressOrgXMLRPCApiErrorDomain = @"WordPressKit.WordPressOrgXMLRPCApiError"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
#if SWIFT_PACKAGE | ||
import APIInterface | ||
#endif | ||
|
||
/// See `extension WordPressComRestApiEndpointError: CustomNSError` for documentation and rationale. | ||
extension WordPressOrgXMLRPCApiError: CustomNSError { | ||
|
||
public static let errorDomain = WordPressOrgXMLRPCApiErrorDomain | ||
|
||
public var errorCode: Int { code.rawValue } | ||
|
||
public var errorUserInfo: [String: Any] { [:] } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Foundation | ||
|
||
public struct WordPressOrgXMLRPCApiError: Error { | ||
|
||
/// Error constants for the WordPress XML-RPC API | ||
@objc public enum Code: Int, CaseIterable { | ||
/// 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 | ||
} | ||
|
||
let code: Code | ||
} | ||
|
||
extension WordPressOrgXMLRPCApiError: LocalizedError { | ||
public var errorDescription: String? { | ||
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 code { | ||
case .httpErrorStatusCode: | ||
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: "A failure reason for when the request couldn't be serialized.") | ||
case .responseSerializationFailed: | ||
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: "A failure reason for when the error that occured wasn't able to be determined.") | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#if SWIFT_PACKAGE | ||
import APIInterface | ||
@testable import CoreAPI | ||
#else | ||
@testable import WordPressKit | ||
#endif | ||
import XCTest | ||
|
||
class WordPressOrgXMLRPCApiErrorTests: XCTestCase { | ||
|
||
func testNSErrorBridging() throws { | ||
for error in WordPressOrgXMLRPCApiError.Code.allCases { | ||
let xmlRPCError = try XCTUnwrap(WordPressOrgXMLRPCApiError(code: error)) | ||
let apiError = WordPressAPIError.endpointError(xmlRPCError) | ||
let newNSError = apiError as NSError | ||
|
||
XCTAssertEqual(newNSError.domain, "WordPressKit.WordPressOrgXMLRPCApiError") | ||
XCTAssertEqual(newNSError.code, error.rawValue) | ||
} | ||
} | ||
|
||
func testErrorDomain() { | ||
XCTAssertEqual(WordPressOrgXMLRPCApiErrorDomain, WordPressOrgXMLRPCApiError.errorDomain) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import XCTest | |
import OHHTTPStubs | ||
import wpxmlrpc | ||
#if SWIFT_PACKAGE | ||
import APIInterface | ||
@testable import CoreAPI | ||
import OHHTTPStubsSwift | ||
#else | ||
|
@@ -63,13 +64,8 @@ class WordPressOrgXMLRPCApiTests: XCTestCase { | |
}, | ||
failure: { (error, _) in | ||
expect.fulfill() | ||
// When building for SPM, the compiler doesn't generate the domain constant. | ||
#if SWIFT_PACKAGE | ||
XCTAssertEqual(error.domain, "CoreAPI.WordPressOrgXMLRPCApiError") | ||
#else | ||
XCTAssertEqual(error.domain, WordPressOrgXMLRPCApiErrorDomain) | ||
#endif | ||
XCTAssertEqual(error.code, WordPressOrgXMLRPCApiError.httpErrorStatusCode.rawValue) | ||
XCTAssertEqual(error.code, WordPressOrgXMLRPCApiError.Code.httpErrorStatusCode.rawValue) | ||
XCTAssertEqual(error.localizedFailureReason, "An HTTP error code 404 was returned.") | ||
XCTAssertNotNil(error.userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyData as String]) | ||
XCTAssertNotNil(error.userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyStatusCode as String]) | ||
|
@@ -96,12 +92,7 @@ class WordPressOrgXMLRPCApiTests: XCTestCase { | |
expect.fulfill() | ||
|
||
XCTAssertFalse(error is WordPressOrgXMLRPCApiError) | ||
// When building for SPM, the compiler doesn't generate the domain constant. | ||
#if SWIFT_PACKAGE | ||
XCTAssertEqual(error.domain, "CoreAPI.WordPressOrgXMLRPCApiError") | ||
#else | ||
XCTAssertEqual(error.domain, WordPressOrgXMLRPCApiErrorDomain) | ||
#endif | ||
XCTAssertEqual(error.code, 403) | ||
XCTAssertEqual(error.localizedFailureReason, "An HTTP error code 403 was returned.") | ||
XCTAssertNotNil(error.userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyData as String]) | ||
|
@@ -128,15 +119,10 @@ class WordPressOrgXMLRPCApiTests: XCTestCase { | |
failure: { (error, _) in | ||
expect.fulfill() | ||
|
||
XCTAssertTrue(error is WordPressOrgXMLRPCApiError) | ||
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. This is the only change in behavior from the rewrite. The 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 might have missed something. Does that mean the |
||
// When building for SPM, the compiler doesn't generate the domain constant. | ||
#if SWIFT_PACKAGE | ||
XCTAssertEqual(error.domain, "CoreAPI.WordPressOrgXMLRPCApiError") | ||
#else | ||
XCTAssertEqual(error.domain, WordPressOrgXMLRPCApiErrorDomain) | ||
#endif | ||
XCTAssertEqual(error.code, WordPressOrgXMLRPCApiError.unknown.rawValue) | ||
XCTAssertEqual(error.localizedFailureReason, WordPressOrgXMLRPCApiError.unknown.failureReason) | ||
let errorUnknonw = WordPressOrgXMLRPCApiError(code: .unknown) | ||
XCTAssertEqual(error.code, errorUnknonw.code.rawValue) | ||
XCTAssertEqual(error.localizedFailureReason, errorUnknonw.failureReason) | ||
XCTAssertNotNil(error.userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyData as String]) | ||
XCTAssertNil(error.userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyStatusCode as String]) | ||
} | ||
|
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.
@crazytonyli I ended up having to wrap the error code enum into an
Error
type like you did for the REST API error.Otherwise, between the
@objc
visibility (needed because WordPress-Authenticator uses it 🙄 ) and theError
conformance, I couldn't find a way to stop the compiler from automatically generatingWordPressOrgXMLRPCApiErrorDomain
for@objc public enum WordPressOrgXMLRPCApiError: Int, Error
.I think this is an okay solution. Apart for the fact that's the only one I could get to work, it's, as far as I can see, the same setup as the other error, which makes the implementation consistent.