AWS Amplify Authentication — Error Handling in iOS

Chinthaka Perera
4 min readDec 24, 2022

--

I wanted to share my experience with all of you about error handling with the AWS Amplify Authentication implementation in iOS. I noticed that the lack of resources is a huge issue when we are new to AWS Amplify.

This is based on Swift 5, AWS Amplify 1.28.3, and Xcode 14.1.

Amplify package installment
Xcode version

Let’s start,

We can use AWS Amplify Auth for the following requirements,

  • SignUp
  • SignIn
  • SignOut
  • Verify the current Auth session
  • Reset Password
  • Resend SignUp verification code

In addition to those, we have to support the following things with the above implementations,

  • Confirm SignUp with the verification code.
  • Confirm the reset password with the verification code.

Amplify Documentation for your reference

Each of the above Amplify.Auth.<Behavior> gives the errors with the type AuthError enumeration.

Screenshot: https://github.com/aws-amplify/amplify-swift/blob/main/Amplify/Categories/Auth/Error/AuthError.swift

Game On,

If you refer to the above image of the AuthError, you can see the available cases in a switch case which we can use to handle the available categories with the error.

Here is a sample.

This each AuthError category contains the following variables for handling separately,

ErrorDescription: Message describing what error occurred.

RecoverySuggestion: Message describing how one might recover from the failure.

Error?: Error that caused the error condition. (Optional)

Screenshot: https://github.com/aws-amplify/amplify-swift/blob/main/Amplify/Core/Support/AmplifyError.swift

Since the errorDescription and the recoverySuggestion are non-optional, we can simply use those 2 to notify the error of the user.

But this is for those who wish to go into little-bit deep,

Now you know that all AuthError categories contain an Error object. We can dig a bit deep using that.

But, this error object is optional. I would love it if we can have that error object with all the AuthErrors we receive.

Why:

We should be able to give our custom error messages considering an error code or an enumeration case from their end. Not every one of us just needs to use their errorDescription and/or the recoverySuggestion.

So, I use swift guard statement to validate the error object and use their errorDescription and/or recoverySuggestion if the error object is not available,

Here is a sample

Let’s validate the Error object,

Now again, the type of the error object is normally AWSCognitoAuthError.

Screenshot: https://github.com/aws-amplify/amplify-swift/blob/7f094a9e88612b5b8fda1bce01ce71e7f1915053/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Models/Errors/AWSCognitoAuthError.swift

But not all we receive are AWSCognitoAuthErrors. Problem and YES it is.

You have to validate the error object again to check the type of it.

Here is a sample

We have to import AWSCognitoAuthPlugin, in addition, to Amplify to use AWSCognitoAuthError in our code.

Handle an AWSCognitoAuthError type error,

Now, simply we can use a switch statement to handle available cases with the AWSCognitoAuthError.

Here is a sample

Handle a casted error object to NSError,

You may know an NSError has 2 variables, called domain and code. We can use those 2 variables to recognize the error specifically. Unfortunately, I was unable to find documentation with the list of available domains and codes with Amplify.

Error domains and codes for now

Above are the error domains and error codes I was able to find for now.

Here is a sample

We are able to use the userInfo variable of NSError to get the specific error message of it if needed.

Here is a sample

This is all I found and learned with my research. Please share your thoughts and hope this will help you for deciding how to handle AWS Amplify Auth errors.

--

--