Skip to content

Commit

Permalink
feat: adding error display feature (#436)
Browse files Browse the repository at this point in the history
* feat: adding error display feature

* feat: complete adding error display feature

* refactor: handle error globally

* refactor: minor change

* refactor: update error handling method

* refactor: format files

* refactor: format files

* refactor: remove files

* rollback modifications on out of scope files

* feat(wallet_kit): remove hardcoded 'Failed to add account" in wallet kit default error handler

---------

Co-authored-by: Patrice Tisserand <[email protected]>
  • Loading branch information
Hoossayn and ptisserand authored Feb 26, 2025
1 parent b071ad1 commit d11675e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
20 changes: 7 additions & 13 deletions packages/secure_store/lib/src/password_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,13 @@ class PasswordStore implements SecureStore {
if (password == null) {
throw Exception('Password must be provided');
}
try {
return bytesToString(
decrypt(
password: stringToBytes(password!),
encryptedSecret: base64Decode(cipherText as String),
),
);
} catch (e) {
if (kDebugMode) {
print(e);
}
throw Exception('Wrong password');
}

return bytesToString(
decrypt(
password: stringToBytes(password!),
encryptedSecret: base64Decode(cipherText as String),
),
);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/wallet_kit/lib/errors/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'wallet_kit_error.dart';
32 changes: 32 additions & 0 deletions packages/wallet_kit/lib/errors/wallet_kit_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:flutter/material.dart';

class WalletKitErrorHandler {
static final WalletKitErrorHandler _instance =
WalletKitErrorHandler._internal();

WalletKitErrorHandler._internal();

factory WalletKitErrorHandler() => _instance;

void handleError(
BuildContext context,
WalletKitError error,
String? message,
) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('${message ?? "error"}: $error')),
);
Navigator.of(context).pop();
}
}

class WalletKitError implements Exception {
final String message;
final int? code;

WalletKitError(this.message, {this.code});

@override
String toString() =>
'WalletKitError: $message${code != null ? ' (Code: $code)' : ''}';
}
19 changes: 12 additions & 7 deletions packages/wallet_kit/lib/wallet_state/wallet_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:secure_store/secure_store.dart';
import 'package:starknet/starknet.dart' as s;
import 'package:starknet_provider/starknet_provider.dart' as sp;

import '../errors/wallet_kit_error.dart';
import '../utils/persisted_notifier_state.dart';
import '../wallet_kit.dart';

Expand Down Expand Up @@ -46,25 +46,30 @@ class Wallets extends _$Wallets with PersistedState<WalletsState> {
updateWallet(wallet: walletWithAccount, accountId: account.id);
}

addAccount({
Future<void> addAccount({
required String walletId,
required Future<String?> Function() getPassword,
SecureStore? secureStore,
}) async {
final wallet = state.wallets[walletId];
if (wallet == null) {
throw Exception("Wallet not found");
throw WalletKitError("Wallet not found");
}
secureStore = secureStore ??
await getSecureStore(
getPassword: getPassword,
type: wallet.secureStoreType,
);
final seedPhrase = await secureStore.getSecret(
key: seedPhraseKey(walletId),
);
late final String? seedPhrase;
try {
seedPhrase = await secureStore.getSecret(
key: seedPhraseKey(walletId),
);
} catch (e) {
throw WalletKitError("Wrong password");
}
if (seedPhrase == null) {
throw Exception("Seed phrase not found");
throw WalletKitError("Secret not found");
}
final (walletWithAccount, account) = await WalletService.addAccount(
secureStore: secureStore,
Expand Down
20 changes: 15 additions & 5 deletions packages/wallet_kit/lib/widgets/wallet_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../errors/wallet_kit_error.dart';
import '../services/index.dart';
import '../ui/index.dart';
import '../utils/index.dart';
Expand Down Expand Up @@ -205,11 +206,20 @@ class WalletCell extends HookConsumerWidget {
child: TextButton.icon(
label: const Text('Add account'),
onPressed: () async {
ref.read(walletsProvider.notifier).addAccount(
walletId: wallet.id,
getPassword: () => showPasswordModal(context),
);
Navigator.of(context).pop();
try {
await ref.read(walletsProvider.notifier).addAccount(
walletId: wallet.id,
getPassword: () => showPasswordModal(context),
);
if (context.mounted) {
Navigator.of(context).pop();
}
} on WalletKitError catch (error) {
if (context.mounted) {
WalletKitErrorHandler().handleError(
context, error, "Failed to add account");
}
}
},
icon: Icon(
Icons.add_circle_outline_rounded,
Expand Down

0 comments on commit d11675e

Please sign in to comment.