Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Authenticator/Authenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public struct Authenticator<LoadingContent: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
for contentState in contentStates.allObjects {
contentState.errorTransform = errorTransform
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
6 changes: 3 additions & 3 deletions Sources/Authenticator/States/AuthenticatorBaseState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public struct ConfirmResetPasswordView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct ConfirmSignInWithCustomChallengeView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public struct ConfirmSignInWithMFACodeView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public struct ConfirmSignInWithNewPasswordView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public struct ConfirmSignInWithTOTPView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Authenticator/Views/ConfirmSignUpView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public struct ConfirmSignUpView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Authenticator/Views/ConfirmVerifyUserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public struct ConfirmVerifyUserView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public struct ContinueSignInWithMFASelectionView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public struct ContinueSignInWithTOTPSetupView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct ConfirmSignInWithCodeView<Header: View,
@ViewBuilder footerContent: () -> Footer = {
EmptyView()
},
errorTransform: ((AuthError) -> AuthenticatorError)? = nil,
errorTransform: ((AuthError) -> AuthenticatorError?)? = nil,
mfaType: AuthenticatorMFAType
) {
self.state = state
Expand Down
2 changes: 1 addition & 1 deletion Sources/Authenticator/Views/ResetPasswordView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public struct ResetPasswordView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Authenticator/Views/SignInView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public struct SignInView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Authenticator/Views/SignUpView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public struct SignUpView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Authenticator/Views/VerifyUserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public struct VerifyUserView<Header: View,

/// Sets a custom error mapping function for the `AuthError`s that are displayed
/// - Parameter errorTransform: A closure that takes an `AuthError` and returns a ``AuthenticatorError`` that will be displayed.
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError) -> Self {
public func errorMap(_ errorTransform: @escaping (AuthError) -> AuthenticatorError?) -> Self {
state.errorTransform = errorTransform
return self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading