diff --git a/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPlugin.java b/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPlugin.java index 84d7e6a6b5..d0342cdaf5 100644 --- a/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPlugin.java +++ b/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPlugin.java @@ -553,11 +553,8 @@ public void onResult(UserStateDetails result) { @Override public void onError(Exception exception) { - onException.accept(new AuthException( - "An error occurred while attempting to retrieve your user details", - exception, - "See attached exception for more details" - )); + onException.accept(CognitoAuthExceptionConverter.lookup( + exception, "Fetching authorization session failed.")); } }); } catch (Throwable exception) { @@ -582,11 +579,8 @@ public void onResult(Void result) { @Override public void onError(Exception exception) { - onException.accept(new AuthException( - "An error occurred while remembering a device", - exception, - "See attached exception for more details" - )); + onException.accept(CognitoAuthExceptionConverter.lookup( + exception, "Remember device failed.")); } }); } @@ -604,11 +598,8 @@ public void onResult(Void result) { @Override public void onError(Exception exception) { - onException.accept(new AuthException( - "An error occurred while forgetting a device", - exception, - "See attached exception for more details" - )); + onException.accept(CognitoAuthExceptionConverter.lookup( + exception, "Forget device failed.")); } }); } @@ -627,11 +618,8 @@ public void onResult(Void result) { @Override public void onError(Exception exception) { - onException.accept(new AuthException( - "An error occurred while forgetting a device", - exception, - "See attached exception for more details" - )); + onException.accept(CognitoAuthExceptionConverter.lookup( + exception, "Forget device failed.")); } }); } @@ -653,11 +641,8 @@ public void onResult(ListDevicesResult result) { @Override public void onError(Exception exception) { - onException.accept(new AuthException( - "An error occurred while fetching remembered devices.", - exception, - "See attached exception for more details" - )); + onException.accept(CognitoAuthExceptionConverter.lookup( + exception, "Fetching devices failed.")); } }); } @@ -690,11 +675,8 @@ public void onResult(ForgotPasswordResult result) { @Override public void onError(Exception exception) { - onException.accept(new AuthException( - "An error occurred triggering password recovery", - exception, - "See attached exception for more details" - )); + onException.accept(CognitoAuthExceptionConverter.lookup( + exception, "Reset password failed.")); } }); } @@ -725,11 +707,8 @@ public void onResult(ForgotPasswordResult result) { @Override public void onError(Exception error) { - onException.accept(new AuthException( - "An error occurred confirming password recovery code", - error, - "See attached exception for more details" - )); + onException.accept(CognitoAuthExceptionConverter.lookup( + error, "Confirm reset password failed.")); } } ); @@ -749,12 +728,9 @@ public void onResult(Void result) { } @Override - public void onError(Exception error) { - onException.accept(new AuthException( - "Failed to change password", - error, - "See attached exception for more details" - )); + public void onError(Exception exception) { + onException.accept(CognitoAuthExceptionConverter.lookup( + exception, "Update password failed.")); } }); } @@ -777,11 +753,8 @@ public void onResult(Map result) { @Override public void onError(Exception error) { - onError.accept(new AuthException( - "Failed to fetch user attributes", - error, - "Ensure that you are logged in and online" - )); + onError.accept(CognitoAuthExceptionConverter.lookup( + error, "Fetching user attributes failed.")); } }); } @@ -932,11 +905,8 @@ public void onResult(Void result) { @Override public void onError(Exception error) { - onError.accept(new AuthException( - "An error occurred confirming user attribute", - error, - "See attached exception for more details" - )); + onError.accept(CognitoAuthExceptionConverter.lookup( + error, "Confirming user attributes failed.")); } }); } diff --git a/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/util/CognitoAuthExceptionConverter.java b/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/util/CognitoAuthExceptionConverter.java index 89bddcb072..a0f8d2766b 100644 --- a/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/util/CognitoAuthExceptionConverter.java +++ b/aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/util/CognitoAuthExceptionConverter.java @@ -26,9 +26,13 @@ import com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException; import com.amazonaws.services.cognitoidentityprovider.model.InvalidPasswordException; import com.amazonaws.services.cognitoidentityprovider.model.LimitExceededException; +import com.amazonaws.services.cognitoidentityprovider.model.MFAMethodNotFoundException; +import com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException; import com.amazonaws.services.cognitoidentityprovider.model.PasswordResetRequiredException; import com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException; +import com.amazonaws.services.cognitoidentityprovider.model.SoftwareTokenMFANotFoundException; import com.amazonaws.services.cognitoidentityprovider.model.TooManyFailedAttemptsException; +import com.amazonaws.services.cognitoidentityprovider.model.TooManyRequestsException; import com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException; import com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException; import com.amazonaws.services.cognitoidentityprovider.model.UsernameExistsException; @@ -91,14 +95,30 @@ public static AuthException lookup(@NonNull Exception error, @NonNull String fal return new AuthException.LimitExceededException(error); } + if (error instanceof MFAMethodNotFoundException) { + return new AuthException.MFAMethodNotFoundException(error); + } + + if (error instanceof NotAuthorizedException) { + return new AuthException.NotAuthorizedException(error); + } + if (error instanceof ResourceNotFoundException) { return new AuthException.ResourceNotFoundException(error); } + if (error instanceof SoftwareTokenMFANotFoundException) { + return new AuthException.SoftwareTokenMFANotFoundException(error); + } + if (error instanceof TooManyFailedAttemptsException) { return new AuthException.FailedAttemptsLimitExceededException(error); } + if (error instanceof TooManyRequestsException) { + return new AuthException.TooManyRequestsException(error); + } + if (error instanceof PasswordResetRequiredException) { return new AuthException.PasswordResetRequiredException(error); } diff --git a/core-kotlin/src/main/java/com/amplifyframework/kotlin/datastore/KotlinDataStoreFacade.kt b/core-kotlin/src/main/java/com/amplifyframework/kotlin/datastore/KotlinDataStoreFacade.kt index e60bf21645..21119a1f9e 100644 --- a/core-kotlin/src/main/java/com/amplifyframework/kotlin/datastore/KotlinDataStoreFacade.kt +++ b/core-kotlin/src/main/java/com/amplifyframework/kotlin/datastore/KotlinDataStoreFacade.kt @@ -67,10 +67,10 @@ class KotlinDataStoreFacade(private val delegate: Delegate = Amplify.DataStore) } @Throws(DataStoreException::class) - override suspend fun delete(itemClass: KClass, filter: QueryPredicate) { + override suspend fun delete(byClass: KClass, filter: QueryPredicate) { return suspendCoroutine { continuation -> delegate.delete( - itemClass.java, + byClass.java, filter, { continuation.resume(Unit) }, { continuation.resumeWithException(it) } diff --git a/core/src/main/java/com/amplifyframework/auth/AuthException.java b/core/src/main/java/com/amplifyframework/auth/AuthException.java index 73fdb7bd2d..72820dda6c 100644 --- a/core/src/main/java/com/amplifyframework/auth/AuthException.java +++ b/core/src/main/java/com/amplifyframework/auth/AuthException.java @@ -396,6 +396,42 @@ public LimitExceededException(Throwable cause) { } } + /** + * Could not find multi-factor authentication (MFA) method in AWS Cognito. + */ + public static class MFAMethodNotFoundException extends AuthException { + private static final long serialVersionUID = 1L; + private static final String MESSAGE = "Could not find multi-factor authentication (MFA) method."; + private static final String RECOVERY_SUGGESTION = + "Configure multi-factor authentication using Amplify CLI or AWS Cognito console."; + + /** + * Default message/recovery suggestion with a cause. + * @param cause The original error. + */ + public MFAMethodNotFoundException(Throwable cause) { + super(MESSAGE, cause, RECOVERY_SUGGESTION); + } + } + + /** + * Could not perform the operation since user is not authorized. + */ + public static class NotAuthorizedException extends AuthException { + private static final long serialVersionUID = 1L; + private static final String MESSAGE = "Failed since user is not authorized."; + private static final String RECOVERY_SUGGESTION = + "Check whether the given values are correct and the user is authorized to perform the operation."; + + /** + * Default message/recovery suggestion with a cause. + * @param cause The original error. + */ + public NotAuthorizedException(Throwable cause) { + super(MESSAGE, cause, RECOVERY_SUGGESTION); + } + } + /** * Could not perform the action because password needs to be reset. */ @@ -431,6 +467,24 @@ public ResourceNotFoundException(Throwable cause) { } } + /** + * Could not find software token MFA for the user. + */ + public static class SoftwareTokenMFANotFoundException extends AuthException { + private static final long serialVersionUID = 1L; + private static final String MESSAGE = "Could not find software token MFA."; + private static final String RECOVERY_SUGGESTION = + "Enable the software token MFA for the user."; + + /** + * Default message/recovery suggestion with a cause. + * @param cause The original error. + */ + public SoftwareTokenMFANotFoundException(Throwable cause) { + super(MESSAGE, cause, RECOVERY_SUGGESTION); + } + } + /** * Could not perform the action because user made too many failed attempts for a given action. */ @@ -449,6 +503,24 @@ public FailedAttemptsLimitExceededException(Throwable cause) { } } + /** + * Could not perform the operation since user made too many requests. + */ + public static class TooManyRequestsException extends AuthException { + private static final long serialVersionUID = 1L; + private static final String MESSAGE = "Failed since the user made too many requests."; + private static final String RECOVERY_SUGGESTION = + "Make sure the requests send are controlled and the errors are properly handled."; + + /** + * Default message/recovery suggestion with a cause. + * @param cause The original error. + */ + public TooManyRequestsException(Throwable cause) { + super(MESSAGE, cause, RECOVERY_SUGGESTION); + } + } + /** * Could not complete an action because it was cancelled by the user. */