Skip to content

Commit 2258c07

Browse files
authored
Swift Error Improvements (#9512)
* Firebase Messaging enum * Add tests for App Check errors * App Distribution Error * Installations error improvements * Remote Config errors * Firestore error handling * Formatting * Silly import error * Consistency for App Check tests. * Typo fix
1 parent 6472d7c commit 2258c07

File tree

12 files changed

+205
-138
lines changed

12 files changed

+205
-138
lines changed

FirebaseAppCheck/Tests/Unit/Swift/AppCheckAPITests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ final class AppCheckAPITests {
178178
Task {
179179
do {
180180
_ = try await deviceCheckProvider.getToken()
181+
} catch AppCheckErrorCode.unsupported {
182+
// ...
181183
} catch {
182184
// ...
183185
}

FirebaseAppDistribution/Sources/Public/FirebaseAppDistribution/FIRAppDistribution.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,25 @@ FOUNDATION_EXPORT NSString *const FIRAppDistributionErrorDomain
7272

7373
/// The key for finding error details in the `NSError`'s `userInfo`.
7474
FOUNDATION_EXPORT NSString *const FIRAppDistributionErrorDetailsKey
75-
NS_SWIFT_NAME(FunctionsErrorDetailsKey);
75+
NS_SWIFT_NAME(AppDistributionErrorDetailsKey);
7676
// clang-format on
7777

7878
/**
7979
* Error codes representing sign in or version check failure reasons.
8080
*/
81-
typedef NS_ENUM(NSUInteger, FIRAppDistributionError) {
82-
/// Returned when an unknown error occurred.
83-
FIRAppDistributionErrorUnknown = 0,
81+
typedef NS_ERROR_ENUM(FIRAppDistributionErrorDomain, FIRAppDistributionError){
82+
/// Returned when an unknown error occurred.
83+
FIRAppDistributionErrorUnknown = 0,
8484

85-
/// Returned when App Distribution failed to authenticate the user.
86-
FIRAppDistributionErrorAuthenticationFailure = 1,
85+
/// Returned when App Distribution failed to authenticate the user.
86+
FIRAppDistributionErrorAuthenticationFailure = 1,
8787

88-
/// Returned when sign-in was cancelled.
89-
FIRAppDistributionErrorAuthenticationCancelled = 2,
88+
/// Returned when sign-in was cancelled.
89+
FIRAppDistributionErrorAuthenticationCancelled = 2,
9090

91-
/// Returned when the network was unavailable to make requests or
92-
/// the request timed out.
93-
FIRAppDistributionErrorNetworkFailure = 3,
91+
/// Returned when the network was unavailable to make requests or
92+
/// the request timed out.
93+
FIRAppDistributionErrorNetworkFailure = 3,
9494

9595
} NS_SWIFT_NAME(AppDistributionError);
9696

FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FIRInstallationsErrors.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818

1919
extern NSString *const kFirebaseInstallationsErrorDomain NS_SWIFT_NAME(InstallationsErrorDomain);
2020

21-
typedef NS_ENUM(NSUInteger, FIRInstallationsErrorCode) {
22-
/** Unknown error. See `userInfo` for details. */
23-
FIRInstallationsErrorCodeUnknown = 0,
21+
typedef NS_ERROR_ENUM(kFirebaseInstallationsErrorDomain, FIRInstallationsErrorCode){
22+
/** Unknown error. See `userInfo` for details. */
23+
FIRInstallationsErrorCodeUnknown = 0,
2424

25-
/** Keychain error. See `userInfo` for details. */
26-
FIRInstallationsErrorCodeKeychain = 1,
25+
/** Keychain error. See `userInfo` for details. */
26+
FIRInstallationsErrorCodeKeychain = 1,
2727

28-
/** Server unreachable. A network error or server is unavailable. See `userInfo` for details. */
29-
FIRInstallationsErrorCodeServerUnreachable = 2,
28+
/** Server unreachable. A network error or server is unavailable. See `userInfo` for details. */
29+
FIRInstallationsErrorCodeServerUnreachable = 2,
3030

31-
/** FirebaseApp configuration issues e.g. invalid GMP-App-ID, etc. See `userInfo` for details. */
32-
FIRInstallationsErrorCodeInvalidConfiguration = 3,
31+
/** FirebaseApp configuration issues e.g. invalid GMP-App-ID, etc. See `userInfo` for details.
32+
*/
33+
FIRInstallationsErrorCodeInvalidConfiguration = 3,
3334

3435
} NS_SWIFT_NAME(InstallationsErrorCode);

FirebaseInstallations/Source/Tests/Unit/Swift/InstallationsAPITests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ final class InstallationsAPITests {
121121
Task {
122122
do {
123123
_ = try await Installations.installations().delete()
124+
} catch let error as NSError
125+
where error.domain == InstallationsErrorDomain && error.code == InstallationsErrorCode
126+
.unknown.rawValue {
127+
// Above is the old way to handle errors.
128+
} catch InstallationsErrorCode.unknown {
129+
// Above is the new way to handle errors.
124130
} catch {
125131
// ...
126132
}
@@ -141,6 +147,7 @@ final class InstallationsAPITests {
141147

142148
Installations.installations().authToken { _, error in
143149
if let error = error {
150+
// Old error handling.
144151
switch (error as NSError).code {
145152
case Int(InstallationsErrorCode.unknown.rawValue):
146153
break
@@ -153,6 +160,21 @@ final class InstallationsAPITests {
153160
default:
154161
break
155162
}
163+
164+
// New error handling.
165+
switch error {
166+
case InstallationsErrorCode.unknown:
167+
break
168+
case InstallationsErrorCode.keychain:
169+
break
170+
case InstallationsErrorCode.serverUnreachable:
171+
break
172+
case InstallationsErrorCode.invalidConfiguration:
173+
break
174+
175+
default:
176+
break
177+
}
156178
}
157179
}
158180
func globalStringSymbols() {

FirebaseMessaging/Sources/FIRMessaging.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
NSString *const kFIRMessagingPlistAutoInitEnabled =
6464
@"FirebaseMessagingAutoInitEnabled"; // Auto Init Enabled key stored in Info.plist
6565

66+
NSString *const FIRMessagingErrorDomain = @"com.google.fcm";
67+
6668
const BOOL FIRMessagingIsAPNSSyncMessage(NSDictionary *message) {
6769
if ([message[kFIRMessagingMessageViaAPNSRootKey] isKindOfClass:[NSDictionary class]]) {
6870
NSDictionary *aps = message[kFIRMessagingMessageViaAPNSRootKey];
@@ -208,7 +210,7 @@ - (void)configureMessagingWithOptions:(FIROptions *)options {
208210
if (!GCMSenderID.length) {
209211
FIRMessagingLoggerError(kFIRMessagingMessageCodeFIRApp000,
210212
@"Firebase not set up correctly, nil or empty senderID.");
211-
[NSException raise:kFIRMessagingDomain
213+
[NSException raise:FIRMessagingErrorDomain
212214
format:@"Could not configure Firebase Messaging. GCMSenderID must not be nil or "
213215
@"empty."];
214216
}

FirebaseMessaging/Sources/NSError+FIRMessaging.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
NS_ASSUME_NONNULL_BEGIN
2020

21-
FOUNDATION_EXPORT NSString *const kFIRMessagingDomain;
22-
2321
// FIRMessaging Internal Error Code
2422
typedef NS_ENUM(NSUInteger, FIRMessagingErrorCode) {
2523
kFIRMessagingErrorCodeUnknown = 0,

FirebaseMessaging/Sources/NSError+FIRMessaging.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
*/
1616

1717
#import "FirebaseMessaging/Sources/NSError+FIRMessaging.h"
18-
19-
NSString *const kFIRMessagingDomain = @"com.google.fcm";
18+
#import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
2019

2120
@implementation NSError (FIRMessaging)
2221

2322
+ (NSError *)messagingErrorWithCode:(FIRMessagingErrorCode)errorCode
2423
failureReason:(NSString *)failureReason {
2524
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
2625
userInfo[NSLocalizedFailureReasonErrorKey] = failureReason;
27-
return [NSError errorWithDomain:kFIRMessagingDomain code:errorCode userInfo:userInfo];
26+
return [NSError errorWithDomain:FIRMessagingErrorDomain code:errorCode userInfo:userInfo];
2827
}
2928

3029
@end

FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,37 @@ FOUNDATION_EXPORT const NSNotificationName FIRMessagingRegistrationTokenRefreshe
6868
NS_SWIFT_NAME(MessagingRegistrationTokenRefreshed);
6969
// clang-format on
7070

71+
/**
72+
* The domain used for all errors in Messaging.
73+
*/
74+
FOUNDATION_EXPORT NSString *const FIRMessagingErrorDomain NS_SWIFT_NAME(MessagingErrorDomain);
7175
/**
7276
* @enum FIRMessagingError
7377
*/
74-
typedef NS_ENUM(NSUInteger, FIRMessagingError) {
75-
/// Unknown error.
76-
FIRMessagingErrorUnknown = 0,
78+
typedef NS_ERROR_ENUM(FIRMessagingErrorDomain, FIRMessagingError){
79+
/// Unknown error.
80+
FIRMessagingErrorUnknown = 0,
7781

78-
/// FIRMessaging couldn't validate request from this client.
79-
FIRMessagingErrorAuthentication = 1,
82+
/// FIRMessaging couldn't validate request from this client.
83+
FIRMessagingErrorAuthentication = 1,
8084

81-
/// InstanceID service cannot be accessed.
82-
FIRMessagingErrorNoAccess = 2,
85+
/// InstanceID service cannot be accessed.
86+
FIRMessagingErrorNoAccess = 2,
8387

84-
/// Request to InstanceID backend timed out.
85-
FIRMessagingErrorTimeout = 3,
88+
/// Request to InstanceID backend timed out.
89+
FIRMessagingErrorTimeout = 3,
8690

87-
/// No network available to reach the servers.
88-
FIRMessagingErrorNetwork = 4,
91+
/// No network available to reach the servers.
92+
FIRMessagingErrorNetwork = 4,
8993

90-
/// Another similar operation in progress, bailing this one.
91-
FIRMessagingErrorOperationInProgress = 5,
94+
/// Another similar operation in progress, bailing this one.
95+
FIRMessagingErrorOperationInProgress = 5,
9296

93-
/// Some parameters of the request were invalid.
94-
FIRMessagingErrorInvalidRequest = 7,
97+
/// Some parameters of the request were invalid.
98+
FIRMessagingErrorInvalidRequest = 7,
9599

96-
/// Topic name is invalid for subscription/unsubscription.
97-
FIRMessagingErrorInvalidTopicName = 8,
100+
/// Topic name is invalid for subscription/unsubscription.
101+
FIRMessagingErrorInvalidTopicName = 8,
98102

99103
} NS_SWIFT_NAME(MessagingError);
100104

FirebaseMessaging/Tests/UnitTestsSwift/FIRMessagingAPITest.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ func apis() {
5555
}
5656

5757
// Use random to eliminate the warning that we're evaluating to a constant.
58-
let messagingError: MessagingError = Bool.random() ? .unknown : .authentication
59-
switch messagingError {
58+
let messagingError: MessagingError = Bool
59+
.random() ? MessagingError(.unknown) : MessagingError(.authentication)
60+
switch messagingError.code {
6061
case .unknown: ()
6162
case .authentication: ()
6263
case .noAccess: ()
@@ -92,6 +93,18 @@ func apis() {
9293
let topic = "cat_video"
9394
messaging.subscribe(toTopic: topic)
9495
messaging.unsubscribe(fromTopic: topic)
96+
messaging.unsubscribe(fromTopic: topic, completion: { error in
97+
if let error = error {
98+
switch error {
99+
// Handle errors in the new format.
100+
case MessagingError.timeout:
101+
()
102+
default:
103+
()
104+
}
105+
}
106+
})
107+
95108
messaging.unsubscribe(fromTopic: topic) { _ in
96109
}
97110

@@ -128,5 +141,11 @@ func apiAsync() async throws {
128141
try await messaging.deleteFCMToken(forSenderID: "fakeSenderID")
129142

130143
try await messaging.deleteData()
144+
145+
// Test new handling of errors
146+
do {
147+
try await messaging.unsubscribe(fromTopic: topic)
148+
} catch MessagingError.timeout {
149+
} catch {}
131150
#endif
132151
}

FirebaseRemoteConfig/Sources/Public/FirebaseRemoteConfig/FIRRemoteConfig.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ typedef NS_ENUM(NSInteger, FIRRemoteConfigFetchAndActivateStatus) {
5656
/// Remote Config error domain that handles errors when fetching data from the service.
5757
extern NSString *const _Nonnull FIRRemoteConfigErrorDomain NS_SWIFT_NAME(RemoteConfigErrorDomain);
5858
/// Firebase Remote Config service fetch error.
59-
typedef NS_ENUM(NSInteger, FIRRemoteConfigError) {
60-
/// Unknown or no error.
61-
FIRRemoteConfigErrorUnknown = 8001,
62-
/// Frequency of fetch requests exceeds throttled limit.
63-
FIRRemoteConfigErrorThrottled = 8002,
64-
/// Internal error that covers all internal HTTP errors.
65-
FIRRemoteConfigErrorInternalError = 8003,
59+
typedef NS_ERROR_ENUM(FIRRemoteConfigErrorDomain, FIRRemoteConfigError){
60+
/// Unknown or no error.
61+
FIRRemoteConfigErrorUnknown = 8001,
62+
/// Frequency of fetch requests exceeds throttled limit.
63+
FIRRemoteConfigErrorThrottled = 8002,
64+
/// Internal error that covers all internal HTTP errors.
65+
FIRRemoteConfigErrorInternalError = 8003,
6666
} NS_SWIFT_NAME(RemoteConfigError);
6767

6868
/// Enumerated value that indicates the source of Remote Config data. Data can come from

0 commit comments

Comments
 (0)