Skip to content

Commit ab08caa

Browse files
use enum for errors
1 parent c8b8acb commit ab08caa

2 files changed

Lines changed: 22 additions & 28 deletions

File tree

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Views/FacebookButtonView.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import FirebaseAuth
55
import FirebaseAuthSwiftUI
66
import SwiftUI
77

8+
public enum FacebookError: Error {
9+
case accessToken(String)
10+
case authenticationToken(String)
11+
}
12+
813
@MainActor
914
public struct FacebookButtonView {
1015
@Environment(AuthService.self) private var authService
@@ -82,13 +87,10 @@ public struct FacebookButtonView {
8287
.credential(withAccessToken: token.tokenString)
8388
try await authService.signIn(with: credential)
8489
} else {
85-
throw NSError(
86-
domain: "FacebookSwiftErrorDomain",
87-
code: 1,
88-
userInfo: [
89-
NSLocalizedDescriptionKey: "Access token has expired or not available. Please sign-in with Facebook before attempting to create a Facebook provider credential",
90-
]
91-
)
90+
throw FacebookError
91+
.accessToken(
92+
"Access token has expired or not available. Please sign-in with Facebook before attempting to create a Facebook provider credential"
93+
)
9294
}
9395
} catch {
9496
errorMessage = authService.string.localizedErrorMessage(
@@ -105,13 +107,10 @@ public struct FacebookButtonView {
105107
rawNonce: rawNonce)
106108
try await authService.signIn(with: credential)
107109
} else {
108-
throw NSError(
109-
domain: "FacebookSwiftErrorDomain",
110-
code: 2,
111-
userInfo: [
112-
NSLocalizedDescriptionKey: "Authentication is not available. Please sign-in with Facebook before attempting to create a Facebook provider credential",
113-
]
114-
)
110+
throw FacebookError
111+
.authenticationToken(
112+
"Authentication is not available. Please sign-in with Facebook before attempting to create a Facebook provider credential"
113+
)
115114
}
116115
} catch {
117116
errorMessage = authService.string.localizedErrorMessage(

FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ let kGoogleUserInfoEmailScope = "https://www.googleapis.com/auth/userinfo.email"
66
let kGoogleUserInfoProfileScope = "https://www.googleapis.com/auth/userinfo.profile"
77
let kDefaultScopes = [kGoogleUserInfoEmailScope, kGoogleUserInfoProfileScope]
88

9+
public enum GoogleError: Error {
10+
case rootViewController(String)
11+
case authenticationToken(String)
12+
case user(String)
13+
}
14+
915
public class GoogleProviderSwift: @preconcurrency GoogleProviderProtocol {
1016
let scopes: [String]
1117
let shortName = "Google"
@@ -21,13 +27,8 @@ public class GoogleProviderSwift: @preconcurrency GoogleProviderProtocol {
2127
@MainActor public func signInWithGoogle(clientID: String) async throws -> AuthCredential {
2228
guard let presentingViewController = await (UIApplication.shared.connectedScenes
2329
.first as? UIWindowScene)?.windows.first?.rootViewController else {
24-
throw NSError(
25-
domain: "GoogleProviderSwiftErrorDomain",
26-
code: 1,
27-
userInfo: [
28-
NSLocalizedDescriptionKey: "Root View controller is not available to present Google sign-in View.",
29-
]
30-
)
30+
throw GoogleError
31+
.rootViewController("Root View controller is not available to present Google sign-in View.")
3132
}
3233

3334
let config = GIDConfiguration(clientID: clientID)
@@ -44,13 +45,7 @@ public class GoogleProviderSwift: @preconcurrency GoogleProviderProtocol {
4445

4546
guard let user = result?.user,
4647
let idToken = user.idToken?.tokenString else {
47-
continuation.resume(throwing: NSError(
48-
domain: "GoogleProviderSwiftErrorDomain",
49-
code: 2,
50-
userInfo: [
51-
NSLocalizedDescriptionKey: "Failed to retrieve user or idToken.",
52-
]
53-
))
48+
continuation.resume(throwing: GoogleError.user("Failed to retrieve user or idToken."))
5449
return
5550
}
5651

0 commit comments

Comments
 (0)