Skip to content

Instantly share code, notes, and snippets.

@Catherine-K-George
Last active June 7, 2021 18:44
Show Gist options
  • Save Catherine-K-George/11a94271bd0cff282e019cd2613d089b to your computer and use it in GitHub Desktop.
Save Catherine-K-George/11a94271bd0cff282e019cd2613d089b to your computer and use it in GitHub Desktop.
Swift AWS Cognito exception handler with customized descriptions
import AWSCognitoIdentityProvider
extension Error {
var customizedDescription: String {
let nsError = self as NSError
if nsError.domain == AWSCognitoIdentityProviderErrorDomain, let code = AWSCognitoIdentityProviderErrorType(rawValue: nsError.code) {
switch code {
case .expiredCode: return "Verification code has expired. Please try again"
case .codeMismatch: return "Incorrect verification code. Please enter the correct code to continue."
case .notAuthorized: return "Authentication failed. Please enter the correct credentials to continue."
case .usernameExists: return "Username already exists."
case .invalidPassword: return "Invalid password. Please try again."
case .userNotConfirmed: return "User not confirmed. Please verify your account."
default: return nsError.userInfo["message"] as? String ?? "AWS cognito failed with error code: \(nsError.code)"
}
}
return localizedDescription
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment