Skip to content

Commit 068bd3c

Browse files
authored
fix(Authenticator): Allowing to only override the desired errors when invoking the errorMap functions (#93)
1 parent eb0fd93 commit 068bd3c

19 files changed

+40
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.1.7 (2024-09-13)
4+
5+
### Bug Fixes
6+
- **Authenticator**: Allowing to only override the desired errors when invoking the errorMap functions (#93)
7+
38
## 1.1.6 (2024-08-13)
49

510
### Bug Fixes

Sources/Authenticator/Authenticator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public struct Authenticator<LoadingContent: View,
290290

291291
/// Sets a custom error mapping function for the `AuthError`s that are displayed
292292
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
293-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
293+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
294294
for contentState in contentStates.allObjects {
295295
contentState.errorTransform = errorTransform
296296
}

Sources/Authenticator/Constants/ComponentInformation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
import Foundation
99

1010
public class ComponentInformation {
11-
public static let version = "1.1.6"
11+
public static let version = "1.1.7"
1212
public static let name = "amplify-ui-swift-authenticator"
1313
}

Sources/Authenticator/States/AuthenticatorBaseState.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class AuthenticatorBaseState: ObservableObject {
2323

2424
@ObservedObject var credentials: Credentials
2525

26-
var errorTransform: ((AuthError) -> AuthenticatorError)? = nil
26+
var errorTransform: ((AuthError) -> AuthenticatorError?)? = nil
2727
private(set) var authenticatorState: AuthenticatorStateProtocol = .empty
2828

2929
init(credentials: Credentials) {
@@ -183,8 +183,8 @@ public class AuthenticatorBaseState: ObservableObject {
183183
return .unknown(from: error)
184184
}
185185

186-
if let errorTransform = errorTransform {
187-
return errorTransform(authError)
186+
if let customError = errorTransform?(authError) {
187+
return customError
188188
}
189189

190190
if let localizedMessage = localizedMessage(for: authError) {

Sources/Authenticator/Views/ConfirmResetPasswordView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public struct ConfirmResetPasswordView<Header: View,
135135

136136
/// Sets a custom error mapping function for the `AuthError`s that are displayed
137137
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
138-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
138+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
139139
state.errorTransform = errorTransform
140140
return self
141141
}

Sources/Authenticator/Views/ConfirmSignInWithCustomChallengeView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public struct ConfirmSignInWithCustomChallengeView<Header: View,
4242

4343
/// Sets a custom error mapping function for the `AuthError`s that are displayed
4444
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
45-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
45+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
4646
state.errorTransform = errorTransform
4747
return self
4848
}

Sources/Authenticator/Views/ConfirmSignInWithMFACodeView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public struct ConfirmSignInWithMFACodeView<Header: View,
4848

4949
/// Sets a custom error mapping function for the `AuthError`s that are displayed
5050
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
51-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
51+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
5252
state.errorTransform = errorTransform
5353
return self
5454
}

Sources/Authenticator/Views/ConfirmSignInWithNewPasswordView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public struct ConfirmSignInWithNewPasswordView<Header: View,
111111

112112
/// Sets a custom error mapping function for the `AuthError`s that are displayed
113113
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
114-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
114+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
115115
state.errorTransform = errorTransform
116116
return self
117117
}

Sources/Authenticator/Views/ConfirmSignInWithTOTPCodeView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public struct ConfirmSignInWithTOTPView<Header: View,
4343

4444
/// Sets a custom error mapping function for the `AuthError`s that are displayed
4545
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
46-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
46+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
4747
state.errorTransform = errorTransform
4848
return self
4949
}

Sources/Authenticator/Views/ConfirmSignUpView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public struct ConfirmSignUpView<Header: View,
9898

9999
/// Sets a custom error mapping function for the `AuthError`s that are displayed
100100
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
101-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
101+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
102102
state.errorTransform = errorTransform
103103
return self
104104
}

Sources/Authenticator/Views/ConfirmVerifyUserView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public struct ConfirmVerifyUserView<Header: View,
8282

8383
/// Sets a custom error mapping function for the `AuthError`s that are displayed
8484
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
85-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
85+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
8686
state.errorTransform = errorTransform
8787
return self
8888
}

Sources/Authenticator/Views/ContinueSignInWithMFASelectionView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public struct ContinueSignInWithMFASelectionView<Header: View,
8989

9090
/// Sets a custom error mapping function for the `AuthError`s that are displayed
9191
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
92-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
92+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
9393
state.errorTransform = errorTransform
9494
return self
9595
}

Sources/Authenticator/Views/ContinueSignInWithTOTPSetupView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public struct ContinueSignInWithTOTPSetupView<Header: View,
120120

121121
/// Sets a custom error mapping function for the `AuthError`s that are displayed
122122
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
123-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
123+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
124124
state.errorTransform = errorTransform
125125
return self
126126
}

Sources/Authenticator/Views/Internal/ConfirmSignInWithCodeView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct ConfirmSignInWithCodeView<Header: View,
2525
@ViewBuilder footerContent: () -> Footer = {
2626
EmptyView()
2727
},
28-
errorTransform: ((AuthError) -> AuthenticatorError)? = nil,
28+
errorTransform: ((AuthError) -> AuthenticatorError?)? = nil,
2929
mfaType: AuthenticatorMFAType
3030
) {
3131
self.state = state

Sources/Authenticator/Views/ResetPasswordView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public struct ResetPasswordView<Header: View,
113113

114114
/// Sets a custom error mapping function for the `AuthError`s that are displayed
115115
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
116-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
116+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
117117
state.errorTransform = errorTransform
118118
return self
119119
}

Sources/Authenticator/Views/SignInView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public struct SignInView<Header: View,
127127

128128
/// Sets a custom error mapping function for the `AuthError`s that are displayed
129129
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
130-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
130+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
131131
state.errorTransform = errorTransform
132132
return self
133133
}

Sources/Authenticator/Views/SignUpView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public struct SignUpView<Header: View,
9191

9292
/// Sets a custom error mapping function for the `AuthError`s that are displayed
9393
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
94-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
94+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
9595
state.errorTransform = errorTransform
9696
return self
9797
}

Sources/Authenticator/Views/VerifyUserView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public struct VerifyUserView<Header: View,
7878

7979
/// Sets a custom error mapping function for the `AuthError`s that are displayed
8080
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
81-
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
81+
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
8282
state.errorTransform = errorTransform
8383
return self
8484
}

Tests/AuthenticatorTests/States/AuthenticatorBaseStateTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ class AuthenticatorBaseStateTests: XCTestCase {
239239
XCTAssertEqual(authenticatorError.content, "A custom error")
240240
}
241241

242+
func testError_withCustomErrorTransformThatReturnsNil_shouldReturnDefaultError() {
243+
var closureCount = 0
244+
state.errorTransform = { error in
245+
closureCount = 1
246+
if case .service = error {
247+
return .error(message: "A service error")
248+
}
249+
return nil
250+
}
251+
let authenticatorError = state.error(for: AuthError.notAuthorized("description", "recovery", nil))
252+
XCTAssertEqual(closureCount, 1)
253+
XCTAssertEqual(authenticatorError.style, .error)
254+
let expectedMessage = "authenticator.authError.incorrectCredentials".localized()
255+
XCTAssertEqual(authenticatorError.content, expectedMessage)
256+
}
257+
242258
func testError_withLocalizedCognitoError_shouldReturnLocalizedError() {
243259
let cognitoError = AWSCognitoAuthError.userNotFound
244260
let authenticatorError = state.error(for: AuthError.service("description", "recovery", cognitoError))

0 commit comments

Comments
 (0)