Skip to content

Commit

Permalink
fix(auth): conform to LocalizedError in AmplifyError
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh62 committed Dec 9, 2024
1 parent 69d0159 commit c5d61df
Show file tree
Hide file tree
Showing 29 changed files with 97 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Amplify/Categories/API/Error/APIError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension APIError: AmplifyError {
}
}

public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .unknown(let errorDescription, _, _):
return "Unexpected error occurred with message: \(errorDescription)"
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Categories/API/Response/GraphQLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public enum GraphQLResponseError<ResponseType: Decodable>: AmplifyError {
/// An unknown error occurred
case unknown(ErrorDescription, RecoverySuggestion, Error?)

public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .error(let errors):
return "GraphQL service returned a successful response containing errors: \(errors)"
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Categories/Analytics/Error/AnalyticsError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum AnalyticsError {

extension AnalyticsError: AmplifyError {
/// Error Description
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .configuration(let errorDescription, _, _):
return errorDescription
Expand Down
77 changes: 63 additions & 14 deletions Amplify/Categories/Auth/Error/AuthError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ extension AuthError: AmplifyError {
}
}

public var errorDescription: ErrorDescription {
switch self {
case .configuration(let errorDescription, _, _),
.service(let errorDescription, _, _),
.validation(_, let errorDescription, _, _),
.notAuthorized(let errorDescription, _, _),
.signedOut(let errorDescription, _, _),
.sessionExpired(let errorDescription, _, _),
.invalidState(let errorDescription, _, _):
return errorDescription
case .unknown(let errorDescription, _):
return "Unexpected error occurred with message: \(errorDescription)"
}
}
// public var errorDescription: ErrorDescription? {
// switch self {
// case .configuration(let errorDescription, _, _),
// .service(let errorDescription, _, _),
// .validation(_, let errorDescription, _, _),
// .notAuthorized(let errorDescription, _, _),
// .signedOut(let errorDescription, _, _),
// .sessionExpired(let errorDescription, _, _),
// .invalidState(let errorDescription, _, _):
// return errorDescription
// case .unknown(let errorDescription, _):
// return "Unexpected error occurred with message: \(errorDescription)"
// }
// }

public var recoverySuggestion: RecoverySuggestion {
switch self {
Expand Down Expand Up @@ -113,3 +113,52 @@ extension AuthError: Equatable {
}
}
}

extension AuthError {
public var errorDescription: ErrorDescription? {
var message = ""
switch self {
case .configuration(let errorDescription, let recoverySuggestion, let error):
message = "Configuration error: \(errorDescription). \(recoverySuggestion)"
if let error = error {
message += " Underlying error: \(error.localizedDescription)"
}
case .service(let errorDescription, let recoverySuggestion, let error):
message = "Service error: \(errorDescription). \(recoverySuggestion)"
if let error = error {
message += " Underlying error: \(error.localizedDescription)"
}
case .unknown(let errorDescription, let error):
message = "Unknown error: \(errorDescription)"
if let error = error {
message += " Underlying error: \(error.localizedDescription)"
}
case .validation(let field, let errorDescription, let recoverySuggestion, let error):
message = "Validation error for field: \(field). \(errorDescription). \(recoverySuggestion)"
if let error = error {
message += " Underlying error: \(error.localizedDescription)"
}
case .notAuthorized(let errorDescription, let recoverySuggestion, let error):
message = "Not authorized error: \(errorDescription). \(recoverySuggestion)"
if let error = error {
message += " Underlying error: \(error.localizedDescription)"
}
case .signedOut(let errorDescription, let recoverySuggestion, let error):
message = "Signed out: \(errorDescription). \(recoverySuggestion)"
if let error = error {
message += " Underlying error: \(error.localizedDescription)"
}
case .sessionExpired(let errorDescription, let recoverySuggestion, let error):
message = "Session expired: \(errorDescription). \(recoverySuggestion)"
if let error = error {
message += " Underlying error: \(error.localizedDescription)"
}
case .invalidState(let errorDescription, let recoverySuggestion, let error):
message = "Invalid state: \(errorDescription). \(recoverySuggestion)"
if let error = error {
message += " Underlying error: \(error.localizedDescription)"
}
}
return "\(String(describing: Self.self)): \(message)"
}
}
2 changes: 1 addition & 1 deletion Amplify/Categories/DataStore/DataStoreError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum DataStoreError: Error {
// MARK: - AmplifyError

extension DataStoreError: AmplifyError {
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .api(let error, _):
return error.errorDescription
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Categories/Geo/Types/Geo+Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension Geo.Error: AmplifyError {
}

/// Error Description
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .invalidConfiguration(let errorDescription, _, _),
.networkError(let errorDescription, _, _),
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Categories/Hub/HubError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum HubError {
}

extension HubError: AmplifyError {
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .configuration(let description, _, _),
.unknownError(let description, _, _):
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Categories/Logging/LoggingError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum LoggingError {
}

extension LoggingError: AmplifyError {
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .configuration(let errorDescription, _, _):
return errorDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum PushNotificationsError {
}

extension PushNotificationsError: AmplifyError {
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .configuration(let description, _, _),
.network(let description, _, _),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum PredictionsError {
}

extension PredictionsError: AmplifyError {
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .client(let clientError):
return "A client error occurred with message:\(clientError.description)"
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Categories/Storage/Error/StorageError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public enum StorageError {
}

extension StorageError: AmplifyError {
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .accessDenied(let errorDescription, _, _),
.authError(let errorDescription, _, _),
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Core/Configuration/ConfigurationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public enum ConfigurationError {

extension ConfigurationError: AmplifyError {
/// - Tag: ConfigurationError.errorDescription
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .amplifyAlreadyConfigured(let description, _, _),
.invalidAmplifyConfigurationFile(let description, _, _),
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Core/Error/CoreError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum CoreError {

extension CoreError: AmplifyError {
/// - Tag: CoreError.errorDescription
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .listOperation(let errorDescription, _, _),
.clientValidation(let errorDescription, _, _):
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Core/Plugin/PluginError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public enum PluginError {
}

extension PluginError: AmplifyError {
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .mismatchedPlugin(let description, _, _),
.noSuchPlugin(let description, _, _),
Expand Down
6 changes: 3 additions & 3 deletions Amplify/Core/Support/AmplifyError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public typealias TargetIdentityId = String
/// recovery suggestions and error messages.
///
/// - Tag: AmplifyError
public protocol AmplifyError: Error, CustomDebugStringConvertible {
public protocol AmplifyError: LocalizedError, CustomDebugStringConvertible {

/// A localized message describing what error occurred.
///
/// - Tag: AmplifyError.errorDescription
var errorDescription: ErrorDescription { get }
var errorDescription: ErrorDescription? { get }

/// A localized message describing how one might recover from the failure.
///
Expand All @@ -66,7 +66,7 @@ public extension AmplifyError {
var debugDescription: String {
let errorType = type(of: self)

var components = ["\(errorType): \(errorDescription)"]
var components = ["\(errorType): \(errorDescription ?? "")"]

if !recoverySuggestion.isEmpty {
components.append("Recovery suggestion: \(recoverySuggestion)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum AWSAPICategoryPluginError {
}

extension AWSAPICategoryPluginError: AmplifyError {
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .configuration(let errorDescription, _, _):
return errorDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum AnalyticsErrorHelper {
case let error as AnalyticsErrorConvertible:
return error.analyticsError
case let error as AuthError:
return .configuration(error.errorDescription, error.recoverySuggestion, error)
return .configuration(error.errorDescription ?? "", error.recoverySuggestion, error)
default:
return getDefaultError(error as NSError)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ struct CompleteTOTPSetup: Action {
await dispatcher.send(responseEvent)

} catch let error as SignInError {
logError(error.authError.errorDescription, environment: environment)
logError(error.authError.errorDescription ?? "", environment: environment)
let errorEvent = SignInEvent(eventType: .throwAuthError(error))
logVerbose("\(#fileID) Sending event \(errorEvent)",
environment: environment)
await dispatcher.send(errorEvent)
} catch {
let error = SignInError.service(error: error)
logError(error.authError.errorDescription, environment: environment)
logError(error.authError.errorDescription ?? "", environment: environment)
let errorEvent = SignInEvent(eventType: .throwAuthError(error))
logVerbose("\(#fileID) Sending event \(errorEvent)",
environment: environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ struct SetUpTOTP: Action {
environment: environment)
await dispatcher.send(responseEvent)
} catch let error as SignInError {
logError(error.authError.errorDescription, environment: environment)
logError(error.authError.errorDescription ?? "", environment: environment)
let errorEvent = SetUpTOTPEvent(eventType: .throwError(error))
logVerbose("\(#fileID) Sending event \(errorEvent)",
environment: environment)
await dispatcher.send(errorEvent)
} catch {
let error = SignInError.service(error: error)
logError(error.authError.errorDescription, environment: environment)
logError(error.authError.errorDescription ?? "", environment: environment)
let errorEvent = SetUpTOTPEvent(eventType: .throwError(error))
logVerbose("\(#fileID) Sending event \(errorEvent)",
environment: environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ struct VerifyTOTPSetup: Action {
environment: environment)
await dispatcher.send(responseEvent)
} catch let error as SignInError {
logError(error.authError.errorDescription, environment: environment)
logError(error.authError.errorDescription ?? "", environment: environment)
let errorEvent = SetUpTOTPEvent(eventType: .throwError(error))
logVerbose("\(#fileID) Sending event \(errorEvent)",
environment: environment)
await dispatcher.send(errorEvent)
} catch {
let error = SignInError.service(error: error)
logError(error.authError.errorDescription, environment: environment)
logError(error.authError.errorDescription ?? "", environment: environment)
let errorEvent = SetUpTOTPEvent(eventType: .throwError(error))
logVerbose("\(#fileID) Sending event \(errorEvent)",
environment: environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public extension AWSCognitoAuthPlugin {
}

func getCurrentUser() async throws -> AuthUser {
print(AuthError.service("test", "test", AWSCognitoAuthError.aliasExists))
let taskHelper = AWSAuthTaskHelper(authStateMachine: authStateMachine)
return try await taskHelper.getCurrentUser()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ extension KeychainStoreError: AuthErrorConvertible {
case .codingError(let errorDescription, let error):
return .configuration(errorDescription, self.recoverySuggestion, error)
case .itemNotFound:
return .service(self.errorDescription, self.recoverySuggestion)
return .service(self.errorDescription ?? "", self.recoverySuggestion)
case .securityError:
return .service(self.errorDescription, self.recoverySuggestion)
return .service(self.errorDescription ?? "", self.recoverySuggestion)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension KeychainStoreError: AmplifyError {
}

/// Error Description
public var errorDescription: ErrorDescription {
public var errorDescription: ErrorDescription? {
switch self {
case .conversionError(let errorDescription, _), .codingError(let errorDescription, _):
return errorDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Amplify
import SmithyHTTPAPI

struct FoundationClientEngineError: AmplifyError {
let errorDescription: ErrorDescription
let errorDescription: ErrorDescription?
let recoverySuggestion: RecoverySuggestion
let underlyingError: Error?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ class RemoteSyncEngine: RemoteSyncEngineBehavior {
}

if case .failure(let dataStoreError) = result {
self.log.error(dataStoreError.errorDescription)
self.log.error(dataStoreError.errorDescription ?? "")
self.log.error(dataStoreError.recoverySuggestion)
if let underlyingError = dataStoreError.underlyingError {
self.log.error("\(underlyingError)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ final class IncomingAsyncSubscriptionEventToAnyModelMapper: Subscriber, AmplifyC
case .success(let mutationSync):
modelsFromSubscription.send(.payload(mutationSync))
case .failure(let failure):
log.error(failure.errorDescription)
log.error(failure.errorDescription ?? "")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
/// Represents domain-specific errors within the AWSCloudWatchLogPlugin subsystem.
struct AWSCloudWatchLoggingError: AmplifyError {

var errorDescription: String
var errorDescription: String?

var recoverySuggestion: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension StorageAccessLevelAwarePrefixResolver: AWSS3PluginPrefixResolver {
guard let authError = error as? AuthError else {
throw StorageError.unknown("Unknown Auth Error", error)
}
throw StorageError.authError(authError.errorDescription, authError.recoverySuggestion)
throw StorageError.authError(authError.errorDescription ?? "", authError.recoverySuggestion)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension StorageRequestUtils {

static func getSize(_ file: URL) throws -> UInt64 {
if let error = validateFileExists(file) {
throw StorageError.localFileNotFound(error.errorDescription, error.recoverySuggestion)
throw StorageError.localFileNotFound(error.errorDescription ?? "", error.recoverySuggestion)
}

do {
Expand Down

0 comments on commit c5d61df

Please sign in to comment.