This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Make WordPressOrgXMLRPCApiError
conform to CustomNSError
#790
Open
mokagio
wants to merge
7
commits into
trunk
Choose a base branch
from
mokagio/coreapi-customnserror-xmlrpc
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8fb7ca2
Make `WordPressComRestApiError` `CaseIterable`
mokagio 0d08c55
Make `WordPressOrgXMLRPCApiError` conform to `CustomNSError`
mokagio eeb26d8
Import Foundation in `WordPressOrgXMLRPCApiError*` files for SPM builds
mokagio 277f0ca
Move `WordPressOrgXMLRPCApiErrorDomain` definition to APIInterface
mokagio f6f0c1c
Add workaround to avoid `WordPressOrgXMLRPCApiErrorDomain` redefinition
mokagio ac15e9a
Wrap .org XML RPC error `enum` in an `Error`-conforming `struct`
mokagio e8ee86f
Make `WordPressOrgXMLRPCApiErrorCode` into a nested `enum`
mokagio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
Sources/APIInterface/include/WordPressOrgXMLRPCApiErrorDomain.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
Sources/CoreAPI/WordPressOrgXMLRPCApiError+NSErrorBridge.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] { [:] } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.