avoid unnecessary Toasts

This commit is contained in:
Adam Velebil 2024-04-11 12:28:12 +02:00
parent b15bd954c4
commit b04639f113
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
2 changed files with 27 additions and 5 deletions

View File

@ -306,7 +306,7 @@ class OathManager(
return useOathSessionNfc(OathActionDescription.AddAccount) { session ->
// We need to check for duplicates here since we haven't yet read the credentials
if (session.credentials.any { it.id.contentEquals(credentialData.id) }) {
throw Exception("A credential with this ID already exists!")
throw IllegalArgumentException()
}
val credential = session.putCredential(credentialData, requireTouch)

View File

@ -29,6 +29,7 @@ import '../../app/models.dart';
import '../../app/state.dart';
import '../../app/views/user_interaction.dart';
import '../../core/models.dart';
import '../../exception/apdu_exception.dart';
import '../../exception/cancellation_exception.dart';
import '../../exception/no_data_exception.dart';
import '../../exception/platform_exception_decoder.dart';
@ -130,6 +131,29 @@ class _AndroidOathStateNotifier extends OathStateNotifier {
}
}
// Converts Platform exception during Add Account operation
// Returns CancellationException for situations we don't want to show a Toast
Exception _decodeAddAccountException(PlatformException platformException) {
final decodedException = platformException.decode();
// Auth required, the app will show Unlock dialog
if (decodedException is ApduException && decodedException.sw == 0x6982) {
_log.error('Add account failed: Auth required');
return CancellationException();
}
// Thrown in native code when the account already exists on the YubiKey
// The entry dialog will show an error message and that is why we convert
// this to CancellationException to avoid showing a Toast
if (platformException.code == 'IllegalArgumentException') {
_log.error('Add account failed: Account already exists');
return CancellationException();
}
// original exception
return decodedException;
}
final addCredentialToAnyProvider =
Provider((ref) => (Uri credentialUri, {bool requireTouch = false}) async {
try {
@ -142,8 +166,7 @@ final addCredentialToAnyProvider =
var result = jsonDecode(resultString);
return OathCredential.fromJson(result['credential']);
} on PlatformException catch (pe) {
_log.error('Failed to add account.', pe);
throw pe.decode();
throw _decodeAddAccountException(pe);
}
});
@ -267,8 +290,7 @@ class _AndroidCredentialListNotifier extends OathCredentialListNotifier {
var result = jsonDecode(resultString);
return OathCredential.fromJson(result['credential']);
} on PlatformException catch (pe) {
_log.error('Failed to add account.', pe);
throw pe.decode();
throw _decodeAddAccountException(pe);
}
}