From 3acc5cb98fafa55385c4a17a464586c3b0fded25 Mon Sep 17 00:00:00 2001 From: Dillon Nys Date: Mon, 26 Jul 2021 09:56:47 -0700 Subject: [PATCH] Fix error handling --- .../amplify_auth_cognito/AuthErrorHandler.kt | 15 ++---------- .../device/DeviceHandler.kt | 4 ++-- .../lib/amplify_auth_error_handling.dart | 15 ++++++++++++ .../lib/method_channel_auth_cognito.dart | 12 +++------- .../device_not_tracked_exception.dart | 12 ++++------ ...lid_user_pool_configuration_exception.dart | 24 +++++++++++++++++++ .../lib/src/types.dart | 1 + 7 files changed, 51 insertions(+), 32 deletions(-) create mode 100644 packages/amplify_auth_plugin_interface/lib/src/Exceptions/invalid_user_pool_configuration_exception.dart diff --git a/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AuthErrorHandler.kt b/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AuthErrorHandler.kt index 373a2bf085..f7c05b3547 100644 --- a/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AuthErrorHandler.kt +++ b/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AuthErrorHandler.kt @@ -23,19 +23,7 @@ import com.amazonaws.amplify.amplify_auth_cognito.types.FlutterInvalidStateExcep import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil import com.amazonaws.amplify.amplify_core.exception.ExceptionMessages import com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoCodeExpiredException -import com.amazonaws.services.cognitoidentityprovider.model.InvalidLambdaResponseException -import com.amazonaws.services.cognitoidentityprovider.model.MFAMethodNotFoundException -import com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException -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.UnexpectedLambdaException -import com.amazonaws.services.cognitoidentityprovider.model.UserLambdaValidationException -import com.amazonaws.services.cognitoidentityprovider.model.LimitExceededException -import com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException -import com.amazonaws.services.cognitoidentityprovider.model.ExpiredCodeException -import com.amazonaws.services.cognitoidentityprovider.model.CodeMismatchException -import com.amazonaws.services.cognitoidentityprovider.model.CodeDeliveryFailureException +import com.amazonaws.services.cognitoidentityprovider.model.* import com.amplifyframework.AmplifyException import com.amplifyframework.auth.AuthException @@ -89,6 +77,7 @@ class AuthErrorHandler { is ExpiredCodeException -> errorCode = "CodeExpiredException" is CodeMismatchException -> errorCode = "CodeMismatchException" is CodeDeliveryFailureException -> errorCode = "CodeDeliveryFailureException" + is InvalidUserPoolConfigurationException -> errorCode = "ConfigurationException" } } } diff --git a/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/device/DeviceHandler.kt b/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/device/DeviceHandler.kt index 371b752269..baec03c39d 100644 --- a/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/device/DeviceHandler.kt +++ b/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/device/DeviceHandler.kt @@ -59,8 +59,8 @@ class DeviceHandler(private val errorHandler: AuthErrorHandler) : val deviceJson = (call.arguments as? Map<*, *> ?: emptyMap()) as Map var device: AuthDevice? = null if (deviceJson.isNotEmpty()) { - val deviceId by deviceJson - device = AuthDevice.fromId(deviceId as String) + val id by deviceJson + device = AuthDevice.fromId(id as String) } forgetDevice(result, device) } diff --git a/packages/amplify_auth_cognito/lib/amplify_auth_error_handling.dart b/packages/amplify_auth_cognito/lib/amplify_auth_error_handling.dart index 49ad792425..56df5be87e 100644 --- a/packages/amplify_auth_cognito/lib/amplify_auth_error_handling.dart +++ b/packages/amplify_auth_cognito/lib/amplify_auth_error_handling.dart @@ -41,6 +41,8 @@ Exception castAndReturnPlatformException(PlatformException e) { return CodeMismatchException.fromMap( Map.from(e.details)); } + case 'ConfigurationException': + return InvalidUserPoolConfigurationException.fromMap(e.details as Map); case 'DeviceNotTrackedException': return DeviceNotTrackedException.fromMap(e.details as Map); case "FailedAttemptsLimitExceededException": @@ -174,3 +176,16 @@ Exception castAndReturnPlatformException(PlatformException e) { } } } + +/// Transforms exceptions related to the Devices API. +Exception transformDeviceException(PlatformException e) { + final parsedException = castAndReturnPlatformException(e); + // Translate Android error to common exception. + if (parsedException is ResourceNotFoundException) { + return DeviceNotTrackedException( + recoverySuggestion: parsedException.recoverySuggestion, + underlyingException: parsedException.underlyingException, + ); + } + return parsedException; +} diff --git a/packages/amplify_auth_cognito/lib/method_channel_auth_cognito.dart b/packages/amplify_auth_cognito/lib/method_channel_auth_cognito.dart index 6e50b3e846..908380c1af 100644 --- a/packages/amplify_auth_cognito/lib/method_channel_auth_cognito.dart +++ b/packages/amplify_auth_cognito/lib/method_channel_auth_cognito.dart @@ -510,7 +510,7 @@ class AmplifyAuthCognitoMethodChannel extends AmplifyAuthCognito { try { await _channel.invokeMethod('rememberDevice'); } on PlatformException catch (e) { - throw castAndReturnPlatformException(e); + throw transformDeviceException(e); } } @@ -519,7 +519,7 @@ class AmplifyAuthCognitoMethodChannel extends AmplifyAuthCognito { try { await _channel.invokeMethod('forgetDevice', device?.toJson()); } on PlatformException catch (e) { - throw castAndReturnPlatformException(e); + throw transformDeviceException(e); } } @@ -529,13 +529,7 @@ class AmplifyAuthCognitoMethodChannel extends AmplifyAuthCognito { final devicesJson = await _channel.invokeListMethod('fetchDevices'); return devicesJson?.map((e) => CognitoDevice.fromJson(e)).toList() ?? []; } on PlatformException catch (e) { - final parsedException = castAndReturnPlatformException(e); - // Translate Android error to common exception. - if (parsedException is ResourceNotFoundException) { - throw DeviceNotTrackedException( - underlyingException: parsedException.underlyingException); - } - throw parsedException; + throw transformDeviceException(e); } } } diff --git a/packages/amplify_auth_plugin_interface/lib/src/Exceptions/device_not_tracked_exception.dart b/packages/amplify_auth_plugin_interface/lib/src/Exceptions/device_not_tracked_exception.dart index 873c7a4971..4692090f2a 100644 --- a/packages/amplify_auth_plugin_interface/lib/src/Exceptions/device_not_tracked_exception.dart +++ b/packages/amplify_auth_plugin_interface/lib/src/Exceptions/device_not_tracked_exception.dart @@ -21,21 +21,17 @@ import 'package:amplify_auth_plugin_interface/amplify_auth_plugin_interface.dart /// {@endtemplate} class DeviceNotTrackedException extends AuthException { /// {@macro device_not_tracked_exception} - const DeviceNotTrackedException({String? underlyingException}) + const DeviceNotTrackedException( + {String? recoverySuggestion, String? underlyingException}) : super( 'This device does not have an id, either it was never tracked or previously forgotten.', - recoverySuggestion: 'Call remeberDevice to track the device.', + recoverySuggestion: recoverySuggestion, underlyingException: underlyingException, ); static DeviceNotTrackedException fromMap(Map map) => DeviceNotTrackedException( + recoverySuggestion: map['recoverySuggestion'] as String?, underlyingException: map['underlyingException'] as String?, ); - - @override - String toString() { - return 'DeviceNotTrackedException{message=$message, recoverySuggestion=$recoverySuggestion, ' - 'underlyingException=$underlyingException}'; - } } diff --git a/packages/amplify_auth_plugin_interface/lib/src/Exceptions/invalid_user_pool_configuration_exception.dart b/packages/amplify_auth_plugin_interface/lib/src/Exceptions/invalid_user_pool_configuration_exception.dart new file mode 100644 index 0000000000..c610cf1f42 --- /dev/null +++ b/packages/amplify_auth_plugin_interface/lib/src/Exceptions/invalid_user_pool_configuration_exception.dart @@ -0,0 +1,24 @@ +import 'package:amplify_auth_plugin_interface/amplify_auth_plugin_interface.dart'; + +/// {@template invalid_user_pool_configuration_exception} +/// Thrown when a user pool is is not configured for the requested action. +/// {@endtemplate} +class InvalidUserPoolConfigurationException extends AuthException { + /// {@macro invalid_user_pool_configuration_exception} + const InvalidUserPoolConfigurationException({ + required String message, + String? recoverySuggestion, + String? underlyingException, + }) : super( + message, + recoverySuggestion: recoverySuggestion, + underlyingException: underlyingException, + ); + + static InvalidUserPoolConfigurationException fromMap(Map map) => + InvalidUserPoolConfigurationException( + message: map['message'] as String, + recoverySuggestion: map['recoverySuggestion'] as String?, + underlyingException: map['underlyingException'] as String?, + ); +} diff --git a/packages/amplify_auth_plugin_interface/lib/src/types.dart b/packages/amplify_auth_plugin_interface/lib/src/types.dart index a424f76c67..7e1264d63c 100644 --- a/packages/amplify_auth_plugin_interface/lib/src/types.dart +++ b/packages/amplify_auth_plugin_interface/lib/src/types.dart @@ -78,6 +78,7 @@ export 'Exceptions/InvalidAccountTypeException.dart'; export 'Exceptions/InvalidParameterException.dart'; export 'Exceptions/InvalidPasswordException.dart'; export 'Exceptions/InvalidStateException.dart'; +export 'Exceptions/invalid_user_pool_configuration_exception.dart'; export 'Exceptions/LambdaException.dart'; export 'Exceptions/LimitExceededException.dart'; export 'Exceptions/MFAMethodNotFoundException.dart';