diff --git a/CHANGELOG.md b/CHANGELOG.md index c6cd0a3..e61d6bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.1.7 (2024-09-13) + +### Bug Fixes +- **Authenticator**: Allowing to only override the desired errors when invoking the errorMap functions (#93) + ## 1.1.6 (2024-08-13) ### Bug Fixes diff --git a/Sources/Authenticator/Authenticator.swift b/Sources/Authenticator/Authenticator.swift index b0d1fbb..f1e5e7e 100644 --- a/Sources/Authenticator/Authenticator.swift +++ b/Sources/Authenticator/Authenticator.swift @@ -290,7 +290,7 @@ public struct Authenticator AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { for contentState in contentStates.allObjects { contentState.errorTransform = errorTransform } diff --git a/Sources/Authenticator/Constants/ComponentInformation.swift b/Sources/Authenticator/Constants/ComponentInformation.swift index a999414..e64239e 100644 --- a/Sources/Authenticator/Constants/ComponentInformation.swift +++ b/Sources/Authenticator/Constants/ComponentInformation.swift @@ -8,6 +8,6 @@ import Foundation public class ComponentInformation { - public static let version = "1.1.6" + public static let version = "1.1.7" public static let name = "amplify-ui-swift-authenticator" } diff --git a/Sources/Authenticator/States/AuthenticatorBaseState.swift b/Sources/Authenticator/States/AuthenticatorBaseState.swift index 32f82d1..e109f04 100644 --- a/Sources/Authenticator/States/AuthenticatorBaseState.swift +++ b/Sources/Authenticator/States/AuthenticatorBaseState.swift @@ -23,7 +23,7 @@ public class AuthenticatorBaseState: ObservableObject { @ObservedObject var credentials: Credentials - var errorTransform: ((AuthError) -> AuthenticatorError)? = nil + var errorTransform: ((AuthError) -> AuthenticatorError?)? = nil private(set) var authenticatorState: AuthenticatorStateProtocol = .empty init(credentials: Credentials) { @@ -183,8 +183,8 @@ public class AuthenticatorBaseState: ObservableObject { return .unknown(from: error) } - if let errorTransform = errorTransform { - return errorTransform(authError) + if let customError = errorTransform?(authError) { + return customError } if let localizedMessage = localizedMessage(for: authError) { diff --git a/Sources/Authenticator/Views/ConfirmResetPasswordView.swift b/Sources/Authenticator/Views/ConfirmResetPasswordView.swift index 8d7a909..978aa23 100644 --- a/Sources/Authenticator/Views/ConfirmResetPasswordView.swift +++ b/Sources/Authenticator/Views/ConfirmResetPasswordView.swift @@ -135,7 +135,7 @@ public struct ConfirmResetPasswordView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/ConfirmSignInWithCustomChallengeView.swift b/Sources/Authenticator/Views/ConfirmSignInWithCustomChallengeView.swift index a491c29..a56daf9 100644 --- a/Sources/Authenticator/Views/ConfirmSignInWithCustomChallengeView.swift +++ b/Sources/Authenticator/Views/ConfirmSignInWithCustomChallengeView.swift @@ -42,7 +42,7 @@ public struct ConfirmSignInWithCustomChallengeView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/ConfirmSignInWithMFACodeView.swift b/Sources/Authenticator/Views/ConfirmSignInWithMFACodeView.swift index 2acb8c4..8a74fb8 100644 --- a/Sources/Authenticator/Views/ConfirmSignInWithMFACodeView.swift +++ b/Sources/Authenticator/Views/ConfirmSignInWithMFACodeView.swift @@ -48,7 +48,7 @@ public struct ConfirmSignInWithMFACodeView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/ConfirmSignInWithNewPasswordView.swift b/Sources/Authenticator/Views/ConfirmSignInWithNewPasswordView.swift index 2838aff..aa57ef9 100644 --- a/Sources/Authenticator/Views/ConfirmSignInWithNewPasswordView.swift +++ b/Sources/Authenticator/Views/ConfirmSignInWithNewPasswordView.swift @@ -111,7 +111,7 @@ public struct ConfirmSignInWithNewPasswordView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/ConfirmSignInWithTOTPCodeView.swift b/Sources/Authenticator/Views/ConfirmSignInWithTOTPCodeView.swift index 9afc20a..dd7111c 100644 --- a/Sources/Authenticator/Views/ConfirmSignInWithTOTPCodeView.swift +++ b/Sources/Authenticator/Views/ConfirmSignInWithTOTPCodeView.swift @@ -43,7 +43,7 @@ public struct ConfirmSignInWithTOTPView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/ConfirmSignUpView.swift b/Sources/Authenticator/Views/ConfirmSignUpView.swift index 1261a64..94e8a2d 100644 --- a/Sources/Authenticator/Views/ConfirmSignUpView.swift +++ b/Sources/Authenticator/Views/ConfirmSignUpView.swift @@ -98,7 +98,7 @@ public struct ConfirmSignUpView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/ConfirmVerifyUserView.swift b/Sources/Authenticator/Views/ConfirmVerifyUserView.swift index 2cc3a55..d10de31 100644 --- a/Sources/Authenticator/Views/ConfirmVerifyUserView.swift +++ b/Sources/Authenticator/Views/ConfirmVerifyUserView.swift @@ -82,7 +82,7 @@ public struct ConfirmVerifyUserView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/ContinueSignInWithMFASelectionView.swift b/Sources/Authenticator/Views/ContinueSignInWithMFASelectionView.swift index b2cd7f6..fc47880 100644 --- a/Sources/Authenticator/Views/ContinueSignInWithMFASelectionView.swift +++ b/Sources/Authenticator/Views/ContinueSignInWithMFASelectionView.swift @@ -89,7 +89,7 @@ public struct ContinueSignInWithMFASelectionView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/ContinueSignInWithTOTPSetupView.swift b/Sources/Authenticator/Views/ContinueSignInWithTOTPSetupView.swift index 28e76b7..62347e0 100644 --- a/Sources/Authenticator/Views/ContinueSignInWithTOTPSetupView.swift +++ b/Sources/Authenticator/Views/ContinueSignInWithTOTPSetupView.swift @@ -120,7 +120,7 @@ public struct ContinueSignInWithTOTPSetupView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/Internal/ConfirmSignInWithCodeView.swift b/Sources/Authenticator/Views/Internal/ConfirmSignInWithCodeView.swift index 4362cc7..4a40737 100644 --- a/Sources/Authenticator/Views/Internal/ConfirmSignInWithCodeView.swift +++ b/Sources/Authenticator/Views/Internal/ConfirmSignInWithCodeView.swift @@ -25,7 +25,7 @@ struct ConfirmSignInWithCodeView Footer = { EmptyView() }, - errorTransform: ((AuthError) -> AuthenticatorError)? = nil, + errorTransform: ((AuthError) -> AuthenticatorError?)? = nil, mfaType: AuthenticatorMFAType ) { self.state = state diff --git a/Sources/Authenticator/Views/ResetPasswordView.swift b/Sources/Authenticator/Views/ResetPasswordView.swift index 100d18a..f55e207 100644 --- a/Sources/Authenticator/Views/ResetPasswordView.swift +++ b/Sources/Authenticator/Views/ResetPasswordView.swift @@ -113,7 +113,7 @@ public struct ResetPasswordView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/SignInView.swift b/Sources/Authenticator/Views/SignInView.swift index 304fbed..319ebfe 100644 --- a/Sources/Authenticator/Views/SignInView.swift +++ b/Sources/Authenticator/Views/SignInView.swift @@ -127,7 +127,7 @@ public struct SignInView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/SignUpView.swift b/Sources/Authenticator/Views/SignUpView.swift index 201402f..e322fe4 100644 --- a/Sources/Authenticator/Views/SignUpView.swift +++ b/Sources/Authenticator/Views/SignUpView.swift @@ -91,7 +91,7 @@ public struct SignUpView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Sources/Authenticator/Views/VerifyUserView.swift b/Sources/Authenticator/Views/VerifyUserView.swift index 6a21aec..9cecebd 100644 --- a/Sources/Authenticator/Views/VerifyUserView.swift +++ b/Sources/Authenticator/Views/VerifyUserView.swift @@ -78,7 +78,7 @@ public struct VerifyUserView AuthenticatorError) -> Self { + public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self { state.errorTransform = errorTransform return self } diff --git a/Tests/AuthenticatorTests/States/AuthenticatorBaseStateTests.swift b/Tests/AuthenticatorTests/States/AuthenticatorBaseStateTests.swift index bc2a5cc..2e5a841 100644 --- a/Tests/AuthenticatorTests/States/AuthenticatorBaseStateTests.swift +++ b/Tests/AuthenticatorTests/States/AuthenticatorBaseStateTests.swift @@ -239,6 +239,22 @@ class AuthenticatorBaseStateTests: XCTestCase { XCTAssertEqual(authenticatorError.content, "A custom error") } + func testError_withCustomErrorTransformThatReturnsNil_shouldReturnDefaultError() { + var closureCount = 0 + state.errorTransform = { error in + closureCount = 1 + if case .service = error { + return .error(message: "A service error") + } + return nil + } + let authenticatorError = state.error(for: AuthError.notAuthorized("description", "recovery", nil)) + XCTAssertEqual(closureCount, 1) + XCTAssertEqual(authenticatorError.style, .error) + let expectedMessage = "authenticator.authError.incorrectCredentials".localized() + XCTAssertEqual(authenticatorError.content, expectedMessage) + } + func testError_withLocalizedCognitoError_shouldReturnLocalizedError() { let cognitoError = AWSCognitoAuthError.userNotFound let authenticatorError = state.error(for: AuthError.service("description", "recovery", cognitoError))