Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Dillon Nys committed Jul 26, 2021
1 parent 5143491 commit 3acc5cb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,6 +77,7 @@ class AuthErrorHandler {
is ExpiredCodeException -> errorCode = "CodeExpiredException"
is CodeMismatchException -> errorCode = "CodeMismatchException"
is CodeDeliveryFailureException -> errorCode = "CodeDeliveryFailureException"
is InvalidUserPoolConfigurationException -> errorCode = "ConfigurationException"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class DeviceHandler(private val errorHandler: AuthErrorHandler) :
val deviceJson = (call.arguments as? Map<*, *> ?: emptyMap<String, Any?>()) as Map<String, Any?>
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)
}
Expand Down
15 changes: 15 additions & 0 deletions packages/amplify_auth_cognito/lib/amplify_auth_error_handling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Exception castAndReturnPlatformException(PlatformException e) {
return CodeMismatchException.fromMap(
Map<String, String>.from(e.details));
}
case 'ConfigurationException':
return InvalidUserPoolConfigurationException.fromMap(e.details as Map);
case 'DeviceNotTrackedException':
return DeviceNotTrackedException.fromMap(e.details as Map);
case "FailedAttemptsLimitExceededException":
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ class AmplifyAuthCognitoMethodChannel extends AmplifyAuthCognito {
try {
await _channel.invokeMethod<void>('rememberDevice');
} on PlatformException catch (e) {
throw castAndReturnPlatformException(e);
throw transformDeviceException(e);
}
}

Expand All @@ -519,7 +519,7 @@ class AmplifyAuthCognitoMethodChannel extends AmplifyAuthCognito {
try {
await _channel.invokeMethod<void>('forgetDevice', device?.toJson());
} on PlatformException catch (e) {
throw castAndReturnPlatformException(e);
throw transformDeviceException(e);
}
}

Expand All @@ -529,13 +529,7 @@ class AmplifyAuthCognitoMethodChannel extends AmplifyAuthCognito {
final devicesJson = await _channel.invokeListMethod<Map>('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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}';
}
}
Original file line number Diff line number Diff line change
@@ -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?,
);
}
1 change: 1 addition & 0 deletions packages/amplify_auth_plugin_interface/lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 3acc5cb

Please sign in to comment.