Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 0b17ca5

Browse files
authored
Make WordPressComOAuthError conforms to Swift.Error (#650)
2 parents 8f8a8c6 + 5b2c433 commit 0b17ca5

File tree

5 files changed

+246
-287
lines changed

5 files changed

+246
-287
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ _None._
3434

3535
### Breaking Changes
3636

37-
_None._
37+
- `WordPressComOAuthError` now conforms to `Swift.Error` [#650]
3838

3939
### New Features
4040

WordPressKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
46ABD0E0262EED3D00C7FF24 /* WordPressOrgXMLRPCValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46ABD0DF262EED3D00C7FF24 /* WordPressOrgXMLRPCValidatorTests.swift */; };
140140
46ABD0E6262EEDAB00C7FF24 /* FakeInfoDictionaryObjectProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46ABD0E5262EEDAB00C7FF24 /* FakeInfoDictionaryObjectProvider.swift */; };
141141
46ABD0EA262EEE0400C7FF24 /* AppTransportSecuritySettingsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46ABD0E9262EEE0400C7FF24 /* AppTransportSecuritySettingsTests.swift */; };
142+
4A11239A2B19269A004690CF /* WordPressAPIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A1123992B19269A004690CF /* WordPressAPIError.swift */; };
142143
4A1DEF44293051BC00322608 /* LoggingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A1DEF43293051BC00322608 /* LoggingTests.swift */; };
143144
4A1DEF46293051C600322608 /* LoggingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A1DEF45293051C600322608 /* LoggingTests.m */; };
144145
4A68E3CD29404181004AC3DC /* RemoteBlog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A68E3CC29404181004AC3DC /* RemoteBlog.swift */; };
@@ -830,6 +831,7 @@
830831
46ABD0DF262EED3D00C7FF24 /* WordPressOrgXMLRPCValidatorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WordPressOrgXMLRPCValidatorTests.swift; sourceTree = "<group>"; };
831832
46ABD0E5262EEDAB00C7FF24 /* FakeInfoDictionaryObjectProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeInfoDictionaryObjectProvider.swift; sourceTree = "<group>"; };
832833
46ABD0E9262EEE0400C7FF24 /* AppTransportSecuritySettingsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppTransportSecuritySettingsTests.swift; sourceTree = "<group>"; };
834+
4A1123992B19269A004690CF /* WordPressAPIError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WordPressAPIError.swift; sourceTree = "<group>"; };
833835
4A1DEF43293051BC00322608 /* LoggingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingTests.swift; sourceTree = "<group>"; };
834836
4A1DEF45293051C600322608 /* LoggingTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LoggingTests.m; sourceTree = "<group>"; };
835837
4A68E3CC29404181004AC3DC /* RemoteBlog.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteBlog.swift; sourceTree = "<group>"; };
@@ -2418,6 +2420,7 @@
24182420
93BD27791EE73944002BB00B /* WordPressOrgXMLRPCApi.swift */,
24192421
93BD277A1EE73944002BB00B /* WordPressOrgXMLRPCValidator.swift */,
24202422
93BD277B1EE73944002BB00B /* WordPressRSDParser.swift */,
2423+
4A1123992B19269A004690CF /* WordPressAPIError.swift */,
24212424
);
24222425
name = WordPressAPI;
24232426
sourceTree = "<group>";
@@ -3317,6 +3320,7 @@
33173320
82FFBF561F460DD400F4573F /* BlogJetpackSettingsServiceRemote.swift in Sources */,
33183321
3297E15625645C7D00287D21 /* JetpackCredentials.swift in Sources */,
33193322
F1B7F4FC272376A8004215CD /* NSCharacterSet+URLEncode.swift in Sources */,
3323+
4A11239A2B19269A004690CF /* WordPressAPIError.swift in Sources */,
33203324
404057C9221B789B0060250C /* StatsTopAuthorsTimeIntervalData.swift in Sources */,
33213325
FEFFD99326C141A800F34231 /* RemoteShareAppContent.swift in Sources */,
33223326
74BA04F61F06DC0A00ED5CD8 /* CommentServiceRemoteXMLRPC.m in Sources */,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Foundation
2+
3+
public enum WordPressAPIError<EndpointError>: Error where EndpointError: LocalizedError {
4+
static var unknownErrorMessage: String {
5+
NSLocalizedString(
6+
"wordpress-api.error.unknown",
7+
value: "Something went wrong, please try again later.",
8+
comment: "Error message that describes an unknown error had occured"
9+
)
10+
}
11+
12+
/// Can't encode the request arguments into a valid HTTP request. This is a programming error.
13+
case requestEncodingFailure
14+
/// Error occured in the HTTP connection.
15+
case connection(URLError)
16+
/// The API call returned an error result. For example, an OAuth endpoint may return an 'incorrect username or password' error, an upload media endpoint may return an 'unsupported media type' error.
17+
case endpointError(EndpointError)
18+
/// The API call returned an HTTP response that WordPressKit can't parse. Receiving this error could be an indicator that there is an error response that's not handled properly by WordPressKit.
19+
case unparsableResponse(response: HTTPURLResponse?, body: Data?)
20+
/// Other error occured.
21+
case unknown(underlyingError: Error)
22+
}
23+
24+
extension WordPressComOAuthError: LocalizedError {
25+
26+
public var errorDescription: String? {
27+
switch self {
28+
case .requestEncodingFailure, .unparsableResponse:
29+
// These are usually programming errors.
30+
return Self.unknownErrorMessage
31+
case let .endpointError(error):
32+
return error.errorDescription
33+
case let .connection(error):
34+
return error.localizedDescription
35+
case let .unknown(underlyingError):
36+
if let msg = (underlyingError as? LocalizedError)?.errorDescription {
37+
return msg
38+
}
39+
return Self.unknownErrorMessage
40+
}
41+
}
42+
43+
}

0 commit comments

Comments
 (0)