Skip to content

Commit a372888

Browse files
committed
Move WordPressComRestApiErrorDomain to APIInterface
This DRYs the definition, makes the constant available to both Objective-C and Swift, and removes the need for custom builder and checker. See @crazytonyli's suggestion from wordpress-mobile/WordPressKit-iOS#782 (comment)
1 parent f7d75c0 commit a372888

File tree

5 files changed

+26
-42
lines changed

5 files changed

+26
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#import <Foundation/Foundation.h>
2+
3+
/// Error domain of `NSError` instances that are converted from `WordPressComRestApiEndpointError`
4+
/// and `WordPressAPIError<WordPressComRestApiEndpointError>` instances.
5+
///
6+
/// This matches the compiler generated value and is used to ensure consistent error domain across error types and SPM or Framework build modes.
7+
///
8+
/// See `extension WordPressComRestApiEndpointError: CustomNSError` in CoreAPI package for context.
9+
static NSString *const _Nonnull WordPressComRestApiErrorDomain = @"WordPressKit.WordPressComRestApiError";

WooCommerce/WordPressAuthenticator/WordPressKit/CoreAPI/WordPressAPIError+NSErrorBridge.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import Foundation
2+
#if SWIFT_PACKAGE
3+
import APIInterface
4+
#endif
25

36
/// Custom `NSError` bridge implementation.
47
///
@@ -86,8 +89,7 @@ extension WordPressAPIError: CustomNSError {
8689
/// And in cases where additional user info was provided, they need to be carried over to the `NSError` instances.
8790
extension WordPressComRestApiEndpointError: CustomNSError {
8891

89-
// This value is the same as the `WordPressComRestApiErrorDomain` constant generated by Swift compiler.
90-
public static let errorDomain = "WordPressKit.WordPressComRestApiError"
92+
public static let errorDomain = WordPressComRestApiErrorDomain
9193

9294
public var errorCode: Int {
9395
code.rawValue

WooCommerce/WordPressAuthenticator/WordPressKit/CoreAPI/WordPressComRestApiErrorDomain.swift

-30
This file was deleted.

WooCommerce/WordPressAuthenticator/WordPressKit/Services/MediaServiceRemoteREST.m

+9-6
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ - (NSError *)processMediaUploadErrors:(NSArray *)errorList {
244244
errorMessage = errorInfo[@"message"];
245245
}
246246
NSDictionary *errorDictionary = @{NSLocalizedDescriptionKey: errorMessage};
247-
error = [NSError wordPressComRestApiErrorWithCode:WordPressComRestApiErrorCodeUploadFailed
248-
userInfo:errorDictionary];
247+
error = [[NSError alloc] initWithDomain:WordPressComRestApiErrorDomain
248+
code:WordPressComRestApiErrorCodeUploadFailed
249+
userInfo:errorDictionary];
249250
}
250251
return error;
251252
}
@@ -297,8 +298,9 @@ - (void)deleteMedia:(RemoteMedia *)media
297298
}
298299
} else {
299300
if (failure) {
300-
NSError *error = [NSError wordPressComRestApiErrorWithCode:WordPressComRestApiErrorCodeUnknown
301-
userInfo:nil];
301+
NSError *error = [[NSError alloc] initWithDomain:WordPressComRestApiErrorDomain
302+
code:WordPressComRestApiErrorCodeUnknown
303+
userInfo:nil];
302304
failure(error);
303305
}
304306
}
@@ -369,8 +371,9 @@ -(void)getVideoPressToken:(NSString *)videoPressID
369371
}
370372
} else {
371373
if (failure) {
372-
NSError *error = [NSError wordPressComRestApiErrorWithCode:WordPressComRestApiErrorCodeUnknown
373-
userInfo:nil];
374+
NSError *error = [[NSError alloc] initWithDomain:WordPressComRestApiErrorDomain
375+
code:WordPressComRestApiErrorCodeUnknown
376+
userInfo:nil];
374377
failure(error);
375378
}
376379
}

WooCommerce/WordPressAuthenticator/WordPressKit/Services/WordPressComServiceRemote.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ - (void)createWPComBlogWithUrl:(NSString *)blogUrl
179179
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
180180
userInfo[WordPressComRestApi.ErrorKeyErrorMessage] = localizedErrorMessage;
181181
userInfo[NSLocalizedDescriptionKey] = localizedErrorMessage;
182-
NSError *errorWithLocalizedMessage = [NSError wordPressComRestApiErrorWithCode:WordPressComRestApiErrorCodeUnknown
183-
userInfo:userInfo];
184-
182+
NSError *errorWithLocalizedMessage = [[NSError alloc] initWithDomain:WordPressComRestApiErrorDomain
183+
code:WordPressComRestApiErrorCodeUnknown
184+
userInfo:userInfo];
185185
failure(errorWithLocalizedMessage);
186186
} else {
187187
success(responseObject);
@@ -228,7 +228,7 @@ - (void)createWPComBlogWithUrl:(NSString *)blogUrl
228228

229229
- (NSError *)errorWithLocalizedMessage:(NSError *)error {
230230
NSError *errorWithLocalizedMessage = error;
231-
if ([error hasWordPressComRestApiErrorDomain] &&
231+
if ([error.domain isEqual:WordPressComRestApiErrorDomain] &&
232232
[error.userInfo objectForKey:WordPressComRestApi.ErrorKeyErrorCode] != nil) {
233233

234234
NSString *localizedErrorMessage = [self errorMessageForError:error];

0 commit comments

Comments
 (0)