Skip to content

Commit e0742b3

Browse files
committed
Make WordPressOrgXMLRPCApiError conform to CustomNSError
This ensures that the error domain is the same between SPM and Framework/CocoaPods builds.
1 parent b38257e commit e0742b3

7 files changed

+74
-17
lines changed

Sources/CoreAPI/WordPressOrgXMLRPCApi.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ private extension WordPressAPIError where EndpointError == WordPressOrgXMLRPCApi
391391
/// Convert to NSError for backwards compatiblity.
392392
///
393393
/// Some Objective-C code in the WordPress app checks domain of the errors returned by `WordPressOrgXMLRPCApi`,
394-
/// which can be WordPressOrgXMLRPCApiError or WPXMLRPCFaultErrorDomain.
394+
/// which can be `WordPressOrgXMLRPCApiError` or `WPXMLRPCFaultErrorDomain`.
395395
///
396396
/// Swift code should avoid dealing with NSError instances. Instead, they should use the strongly typed
397397
/// `WordPressAPIError<WordPressOrgXMLRPCApiFault>`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// See `extension WordPressComRestApiEndpointError: CustomNSError` for documentation and rationale.
2+
extension WordPressOrgXMLRPCApiError: CustomNSError {
3+
4+
public static let errorDomain = WordPressOrgXMLRPCApiErrorDomain
5+
6+
public var errorCode: Int { self.rawValue }
7+
8+
public var errorUserInfo: [String: Any] { [:] }
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// Error domain of `NSError` instances that are converted from `WordPressOrgXMLRPCApiError`
2+
/// and `WordPressAPIError<WordPressOrgXMLRPCApiError>` instances.
3+
///
4+
/// See `extension WordPressOrgXMLRPCApiError: CustomNSError` for context.
5+
let WordPressOrgXMLRPCApiErrorDomain = "WordPressKit.WordPressOrgXMLRPCApiError"
6+
7+
// WordPressOrgXMLRPCApiErrorDomain is accessible only in Swift and, since it's a global, cannot be made @objc.
8+
// As a workaround, here is a builder to init NSError with that domain and a method to check the domain.
9+
@objc
10+
public extension NSError {
11+
12+
@objc
13+
static func wordPressOrgXMLRPCApiError(
14+
code: Int,
15+
userInfo: [String: Any]?
16+
) -> NSError {
17+
NSError(
18+
domain: WordPressOrgXMLRPCApiErrorDomain,
19+
code: code,
20+
userInfo: userInfo
21+
)
22+
}
23+
24+
@objc
25+
func hasWordPressOrgXMLRPCApiErrorDomain() -> Bool {
26+
domain == WordPressOrgXMLRPCApiErrorDomain
27+
}
28+
}

Tests/CoreAPITests/WordPressComRestApiTests+Error.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ class WordPressComRestApiErrorTests: XCTestCase {
2020
func testErrorDomain() {
2121
XCTAssertEqual(WordPressComRestApiErrorDomain, WordPressComRestApiEndpointError.errorDomain)
2222
}
23-
2423
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#if SWIFT_PACKAGE
2+
@testable import CoreAPI
3+
#else
4+
@testable import WordPressKit
5+
#endif
6+
import XCTest
7+
8+
class WordPressOrgXMLRPCApiErrorTests: XCTestCase {
9+
10+
func testNSErrorBridging() throws {
11+
for error in WordPressOrgXMLRPCApiError.allCases {
12+
let xmlRPCError = try XCTUnwrap(WordPressOrgXMLRPCApiError(rawValue: error.rawValue))
13+
let apiError = WordPressAPIError.endpointError(xmlRPCError)
14+
let newNSError = apiError as NSError
15+
16+
XCTAssertEqual(newNSError.domain, "WordPressKit.WordPressOrgXMLRPCApiError")
17+
XCTAssertEqual(newNSError.code, error.rawValue)
18+
}
19+
}
20+
21+
func testErrorDomain() {
22+
XCTAssertEqual(WordPressOrgXMLRPCApiErrorDomain, WordPressOrgXMLRPCApiError.errorDomain)
23+
}
24+
}

Tests/CoreAPITests/WordPressOrgXMLRPCApiTests.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ class WordPressOrgXMLRPCApiTests: XCTestCase {
6363
},
6464
failure: { (error, _) in
6565
expect.fulfill()
66-
// When building for SPM, the compiler doesn't generate the domain constant.
67-
#if SWIFT_PACKAGE
68-
XCTAssertEqual(error.domain, "CoreAPI.WordPressOrgXMLRPCApiError")
69-
#else
7066
XCTAssertEqual(error.domain, WordPressOrgXMLRPCApiErrorDomain)
71-
#endif
7267
XCTAssertEqual(error.code, WordPressOrgXMLRPCApiError.httpErrorStatusCode.rawValue)
7368
XCTAssertEqual(error.localizedFailureReason, "An HTTP error code 404 was returned.")
7469
XCTAssertNotNil(error.userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyData as String])
@@ -96,12 +91,7 @@ class WordPressOrgXMLRPCApiTests: XCTestCase {
9691
expect.fulfill()
9792

9893
XCTAssertFalse(error is WordPressOrgXMLRPCApiError)
99-
// When building for SPM, the compiler doesn't generate the domain constant.
100-
#if SWIFT_PACKAGE
101-
XCTAssertEqual(error.domain, "CoreAPI.WordPressOrgXMLRPCApiError")
102-
#else
10394
XCTAssertEqual(error.domain, WordPressOrgXMLRPCApiErrorDomain)
104-
#endif
10595
XCTAssertEqual(error.code, 403)
10696
XCTAssertEqual(error.localizedFailureReason, "An HTTP error code 403 was returned.")
10797
XCTAssertNotNil(error.userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyData as String])
@@ -129,12 +119,7 @@ class WordPressOrgXMLRPCApiTests: XCTestCase {
129119
expect.fulfill()
130120

131121
XCTAssertTrue(error is WordPressOrgXMLRPCApiError)
132-
// When building for SPM, the compiler doesn't generate the domain constant.
133-
#if SWIFT_PACKAGE
134-
XCTAssertEqual(error.domain, "CoreAPI.WordPressOrgXMLRPCApiError")
135-
#else
136122
XCTAssertEqual(error.domain, WordPressOrgXMLRPCApiErrorDomain)
137-
#endif
138123
XCTAssertEqual(error.code, WordPressOrgXMLRPCApiError.unknown.rawValue)
139124
XCTAssertEqual(error.localizedFailureReason, WordPressOrgXMLRPCApiError.unknown.failureReason)
140125
XCTAssertNotNil(error.userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyData as String])

WordPressKit.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
32FC20CE255DCC6100CD0A7B /* JetpackScanThreat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32FC20CD255DCC6100CD0A7B /* JetpackScanThreat.swift */; };
6565
3F3195AD266FF94B00397EE7 /* ZendeskMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F3195AC266FF94B00397EE7 /* ZendeskMetadata.swift */; };
6666
3F3C9E9C289A3E31009A1357 /* TestCollector+Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F3C9E9B289A3E31009A1357 /* TestCollector+Constants.swift */; };
67+
3F6128112BCB310F0063810D /* WordPressOrgXMLRPCApiErrorDomain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F6128102BCB310F0063810D /* WordPressOrgXMLRPCApiErrorDomain.swift */; };
68+
3F6128132BCB31660063810D /* WordPressOrgXMLRPCApiError+NSErrorBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F6128122BCB31660063810D /* WordPressOrgXMLRPCApiError+NSErrorBridge.swift */; };
69+
3F6128162BCB320B0063810D /* WordPressOrgXMLRPCApiErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F6128142BCB31DB0063810D /* WordPressOrgXMLRPCApiErrorTests.swift */; };
6770
3F758FD324F6C68200BBA2FC /* AnnouncementServiceRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F758FD224F6C68200BBA2FC /* AnnouncementServiceRemote.swift */; };
6871
3F8308A729EE683500354497 /* ActivityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F8308A629EE683500354497 /* ActivityTests.swift */; };
6972
3FB8642C2888089F003A86BE /* BuildkiteTestCollector in Frameworks */ = {isa = PBXBuildFile; productRef = 3FB8642B2888089F003A86BE /* BuildkiteTestCollector */; };
@@ -812,6 +815,9 @@
812815
32FC20CD255DCC6100CD0A7B /* JetpackScanThreat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackScanThreat.swift; sourceTree = "<group>"; };
813816
3F3195AC266FF94B00397EE7 /* ZendeskMetadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZendeskMetadata.swift; sourceTree = "<group>"; };
814817
3F3C9E9B289A3E31009A1357 /* TestCollector+Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TestCollector+Constants.swift"; sourceTree = "<group>"; };
818+
3F6128102BCB310F0063810D /* WordPressOrgXMLRPCApiErrorDomain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordPressOrgXMLRPCApiErrorDomain.swift; sourceTree = "<group>"; };
819+
3F6128122BCB31660063810D /* WordPressOrgXMLRPCApiError+NSErrorBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WordPressOrgXMLRPCApiError+NSErrorBridge.swift"; sourceTree = "<group>"; };
820+
3F6128142BCB31DB0063810D /* WordPressOrgXMLRPCApiErrorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordPressOrgXMLRPCApiErrorTests.swift; sourceTree = "<group>"; };
815821
3F758FD224F6C68200BBA2FC /* AnnouncementServiceRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnnouncementServiceRemote.swift; sourceTree = "<group>"; };
816822
3F8308A629EE683500354497 /* ActivityTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityTests.swift; sourceTree = "<group>"; };
817823
3FB8642D288813E9003A86BE /* UnitTests.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = UnitTests.xctestplan; sourceTree = "<group>"; };
@@ -1580,6 +1586,7 @@
15801586
4A05E79B2B2FDC6100C25E3B /* WordPressOrgAPITests.swift */,
15811587
FFA4D4A82423B10A00BF5180 /* WordPressOrgRestApiTests.swift */,
15821588
74B335DB1F06F4180053A184 /* WordPressOrgXMLRPCApiTests.swift */,
1589+
3F6128142BCB31DB0063810D /* WordPressOrgXMLRPCApiErrorTests.swift */,
15831590
46ABD0DF262EED3D00C7FF24 /* WordPressOrgXMLRPCValidatorTests.swift */,
15841591
);
15851592
path = CoreAPITests;
@@ -2135,6 +2142,8 @@
21352142
3FD634FE2BC3B3D400CEDF5E /* WordPressComRestApiErrorDomain.swift */,
21362143
4A05E7992B2FDC3200C25E3B /* WordPressOrgRestApi.swift */,
21372144
93BD27791EE73944002BB00B /* WordPressOrgXMLRPCApi.swift */,
2145+
3F6128102BCB310F0063810D /* WordPressOrgXMLRPCApiErrorDomain.swift */,
2146+
3F6128122BCB31660063810D /* WordPressOrgXMLRPCApiError+NSErrorBridge.swift */,
21382147
3FD634E32BC3A55F00CEDF5E /* WordPressOrgXMLRPCValidator.swift */,
21392148
93BD277B1EE73944002BB00B /* WordPressRSDParser.swift */,
21402149
);
@@ -3380,6 +3389,7 @@
33803389
74BA04F61F06DC0A00ED5CD8 /* CommentServiceRemoteXMLRPC.m in Sources */,
33813390
40819778221F00E600A298E4 /* StatsSummaryTimeIntervalData.swift in Sources */,
33823391
7430C9A81F1927180051B8E6 /* ReaderTopicServiceRemote.m in Sources */,
3392+
3F6128132BCB31660063810D /* WordPressOrgXMLRPCApiError+NSErrorBridge.swift in Sources */,
33833393
17CE77F120C6EB41001DEA5A /* ReaderFeed.swift in Sources */,
33843394
93C674F21EE8351E00BFAF05 /* NSMutableDictionary+Helpers.m in Sources */,
33853395
4624222D2548BA0F002B8A12 /* RemoteSiteDesign.swift in Sources */,
@@ -3388,6 +3398,7 @@
33883398
740B23C31F17EE8000067A2A /* RemotePostCategory.m in Sources */,
33893399
8B2F4BF124ACE3C30056C08A /* RemoteReaderInterest.swift in Sources */,
33903400
74B5F0E71EF8699C00B411E7 /* RemotePostType.m in Sources */,
3401+
3F6128112BCB310F0063810D /* WordPressOrgXMLRPCApiErrorDomain.swift in Sources */,
33913402
93188D1F1F2262BF0028ED4D /* RemotePostTag.m in Sources */,
33923403
3FD634F72BC3AD6200CEDF5E /* WebauthChallengeInfo.swift in Sources */,
33933404
74D67F081F15BEB70010C5ED /* RemotePerson.swift in Sources */,
@@ -3516,6 +3527,7 @@
35163527
1DC837C229B9F04F009DCD4B /* RemoteVideoPressVideoTests.swift in Sources */,
35173528
3FFCC04D2BABA6980051D229 /* NSDate+WordPressComTests.swift in Sources */,
35183529
FAD1345125909DEA00A8FEB1 /* JetpackBackupServiceRemoteTests.swift in Sources */,
3530+
3F6128162BCB320B0063810D /* WordPressOrgXMLRPCApiErrorTests.swift in Sources */,
35193531
8B2F4BE924ABC9DC0056C08A /* ReaderPostServiceRemote+CardsTests.swift in Sources */,
35203532
FEF87FEF2BB7343700A1D2C1 /* ReaderTopicServiceRemoteTests.swift in Sources */,
35213533
40F9880C221ACEEE00B7B369 /* StatsRemoteV2Tests.swift in Sources */,

0 commit comments

Comments
 (0)