Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding error display feature #436

Merged
merged 14 commits into from
Feb 26, 2025
Merged

feat: adding error display feature #436

merged 14 commits into from
Feb 26, 2025

Conversation

Hoossayn
Copy link
Contributor

@Hoossayn Hoossayn commented Jan 9, 2025

Summary by CodeRabbit

  • New Features

    • Enhanced wallet error notifications now provide users with clear, descriptive messages during wallet operations.
    • The wallet account addition process has been upgraded to asynchronous execution, resulting in a smoother, more responsive experience.
  • Bug Fixes

    • Refined error handling has been implemented across wallet functionalities, ensuring that issues such as incorrect credentials or missing wallet details are communicated more accurately.

Copy link

docs-page bot commented Jan 9, 2025

To view this pull requests documentation preview, visit the following URL:

docs.page/focustree/starknet.dart~436

Documentation is deployed and generated using docs.page.

Copy link
Contributor

coderabbitai bot commented Jan 9, 2025

Walkthrough

This pull request modifies error handling and flow control across multiple modules. In the secure store, the decryption method’s try-catch block has been removed, so decryption errors are now unhandled. In the wallet kit package, new error classes and a singleton error handler have been introduced and exported for structured error management. Additionally, the account addition flow has been updated to be asynchronous with improved error checking and conditional navigation in both the provider and UI widget.

Changes

File(s) Change Summary
packages/secure_store/.../password_store.dart Removed try-catch in getSecret to eliminate decryption error handling, allowing exceptions to propagate.
packages/wallet_kit/.../errors/(index.dart, wallet_kit_error.dart) Added an export for wallet_kit_error.dart and introduced new classes WalletKitError and WalletKitErrorHandler for structured error management.
packages/wallet_kit/.../(wallet_provider.dart, wallet_list.dart) Updated addAccount to return a Future<void> and enhanced error handling using WalletKitError, with conditional modal navigation in the widget.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant WC as WalletCell (UI)
    participant WP as WalletProvider
    participant EH as WalletKitErrorHandler

    U->>WC: Press "Add Account" button
    WC->>WP: Call addAccount(context)
    alt Success
        WP-->>WC: Complete account addition
        WC->>Navigation: Pop modal (if context mounted)
    else Error occurs
        WP-->>WC: Throws WalletKitError ("Wallet not found", "Wrong password", or "Secret not found")
        WC->>EH: Invoke handleError(context, error)
        EH-->>WC: Display error message & navigate back
    end
Loading

Poem

I'm a bunny coding all night,
Hopping through errors with delight,
Decryptions now fly unchecked in the breeze,
Wallets and errors now flow with ease,
Carrots and bytes in a joyful spree,
Hoppin' along in our bug-free jubilee!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Hoossayn Hoossayn requested a review from ptisserand January 13, 2025 15:01
@Hoossayn Hoossayn marked this pull request as ready for review January 13, 2025 15:02
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
packages/wallet_kit/lib/errors/wallet_kit_error.dart (1)

1-9: Consider enhancing the error class implementation.

The error class could benefit from the following improvements:

  1. Make the class final to prevent inheritance
  2. Add documentation for the message and code fields
  3. Consider adding error codes as constants for common errors
+/// Represents errors that occur during wallet operations.
+/// 
+/// The [message] provides a human-readable description of the error.
+/// The [code] can be used to identify specific error types programmatically.
-class WalletKitError implements Exception {
+final class WalletKitError implements Exception {
   final String message;
+  /// Optional error code for programmatic error handling.
+  /// Positive integers are reserved for system use.
   final int? code;

   WalletKitError(this.message, {this.code});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 97c67ed and d770f22.

⛔ Files ignored due to path filters (1)
  • examples/wallet_app/ios/Podfile.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • examples/wallet_app/.gitignore (1 hunks)
  • packages/secure_store/lib/src/password_store.dart (1 hunks)
  • packages/wallet_kit/lib/errors/index.dart (1 hunks)
  • packages/wallet_kit/lib/errors/wallet_kit_error.dart (1 hunks)
  • packages/wallet_kit/lib/wallet_state/wallet_provider.dart (3 hunks)
  • packages/wallet_kit/lib/widgets/wallet_list.dart (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/wallet_kit/lib/errors/index.dart
🔇 Additional comments (1)
examples/wallet_app/.gitignore (1)

8-8: LGTM! Appropriate additions for Swift/SPM projects.

The new entries .build/ and .swiftpm/ are standard ignore patterns for Swift and Swift Package Manager projects. These directories contain build artifacts and project-specific metadata that should not be version controlled.

Also applies to: 12-12

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
packages/wallet_kit/lib/errors/wallet_kit_error.dart (2)

3-8: Consider clarifying the purpose and usage of _context and scaffoldKey.
Currently, these fields are declared but not utilized. If you plan to display UI-based error handling (e.g., SnackBars, dialogs), integrate the ScaffoldState or BuildContext in handleError, or remove them if they are not needed.


16-19: Refine logging statement.
The string "WalleeeeetKit Error" appears to contain repeated letters. Consider ensuring consistency before shipping to end users. Also, replace print with debugPrint or a logger package for better debugging control.

- print("WalleeeeetKit Error: $error");
+ debugPrint("WalletKitError: $error");
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d770f22 and 2774ec3.

📒 Files selected for processing (9)
  • examples/wallet_app/lib/main.dart (2 hunks)
  • melos_monorepo.iml (1 hunks)
  • packages/wallet_kit/ios/Flutter/Generated.xcconfig (1 hunks)
  • packages/wallet_kit/ios/Flutter/flutter_export_environment.sh (1 hunks)
  • packages/wallet_kit/lib/errors/wallet_kit_error.dart (1 hunks)
  • packages/wallet_kit/lib/wallet_state/wallet_provider.dart (3 hunks)
  • packages/wallet_kit/lib/widgets/wallet_list.dart (1 hunks)
  • packages/wallet_kit/macos/Flutter/ephemeral/Flutter-Generated.xcconfig (1 hunks)
  • packages/wallet_kit/macos/Flutter/ephemeral/flutter_export_environment.sh (1 hunks)
✅ Files skipped from review due to trivial changes (4)
  • packages/wallet_kit/macos/Flutter/ephemeral/Flutter-Generated.xcconfig
  • packages/wallet_kit/ios/Flutter/flutter_export_environment.sh
  • packages/wallet_kit/ios/Flutter/Generated.xcconfig
  • packages/wallet_kit/macos/Flutter/ephemeral/flutter_export_environment.sh
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/wallet_kit/lib/widgets/wallet_list.dart
🔇 Additional comments (4)
melos_monorepo.iml (1)

7-9: LGTM! Standard exclusions for Dart/Flutter projects.

The added exclusions for .dart_tool, .pub, and build directories follow best practices for Dart/Flutter projects. These directories contain generated files and caches that should not be indexed by the IDE.

packages/wallet_kit/lib/errors/wallet_kit_error.dart (1)

22-30: Good introduction of a custom exception class.
The WalletKitError class is straightforward and enables passing both a message and a code. This design simplifies structured error reporting.

packages/wallet_kit/lib/wallet_state/wallet_provider.dart (2)

49-49: Excellent decision to make addAccount asynchronous.
By returning Future<void>, external callers can properly await for its completion and handle related exceptions.


63-79: Enhance error message granularity.
This catch block throws a single “Wrong password” message for all errors. If the issue is that the seed phrase was never created or there's a different decryption error, the user still sees “Wrong password.” Consider passing the original error or differentiating the error cases for more precise feedback.

} catch (e) {
-  final error = WalletKitError("Wrong password");
-  WalletKitErrorHandler().handleError(error);
+  if (e.toString().contains('decrypt')) {
+    WalletKitErrorHandler().handleError(WalletKitError("Wrong password"));
+  } else {
+    WalletKitErrorHandler().handleError(WalletKitError("Failed to add account: ${e.toString()}"));
+  }
}

# Conflicts:
#	examples/wallet_app/lib/main.dart
#	packages/wallet_kit/lib/wallet_state/wallet_provider.dart
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 810c56f and b06699e.

📒 Files selected for processing (5)
  • examples/wallet_app/lib/main.dart (1 hunks)
  • examples/wallet_app/macos/Flutter/GeneratedPluginRegistrant.swift (1 hunks)
  • packages/wallet_kit/lib/errors/wallet_kit_error.dart (1 hunks)
  • packages/wallet_kit/lib/wallet_state/wallet_provider.dart (3 hunks)
  • packages/wallet_kit/lib/widgets/wallet_list.dart (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/wallet_kit/lib/widgets/wallet_list.dart
  • examples/wallet_app/lib/main.dart
  • packages/wallet_kit/lib/wallet_state/wallet_provider.dart
🔇 Additional comments (2)
examples/wallet_app/macos/Flutter/GeneratedPluginRegistrant.swift (1)

1-16: Skip reviewing generated file.

This is an auto-generated file that should not be manually edited. The changes appear to be automatically generated, likely due to a plugin update.

packages/wallet_kit/lib/errors/wallet_kit_error.dart (1)

18-26: LGTM! Well-structured error class implementation.

The WalletKitError class is well-designed with:

  • Clear separation of required and optional parameters
  • Clean toString() implementation for error formatting
  • Proper Exception interface implementation

Comment on lines 10 to 15
void handleError(WalletKitError error, BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to add account: $error')),
);
Navigator.of(context).pop();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Make error handling more robust and generic.

The current implementation has two issues:

  1. The error message is hardcoded to "Failed to add account", which may not be appropriate for all error scenarios.
  2. Unconditionally popping the navigation stack could crash the app if called from the root route.

Apply this diff to make the error handling more robust:

   void handleError(WalletKitError error, BuildContext context) {
     ScaffoldMessenger.of(context).showSnackBar(
-      SnackBar(content: Text('Failed to add account: $error')),
+      SnackBar(content: Text('$error')),
     );
-    Navigator.of(context).pop();
+    if (Navigator.of(context).canPop()) {
+      Navigator.of(context).pop();
+    }
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
void handleError(WalletKitError error, BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to add account: $error')),
);
Navigator.of(context).pop();
}
void handleError(WalletKitError error, BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('$error')),
);
if (Navigator.of(context).canPop()) {
Navigator.of(context).pop();
}
}

Copy link
Collaborator

@ptisserand ptisserand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contributions!
I really like to have a default error handler but I'm concerned about how a developer can easily customized this behavior without wallet_kit source code modifications.

Why not using notifier instead of a callback ?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
packages/wallet_kit/lib/errors/wallet_kit_error.dart (1)

16-20: ⚠️ Potential issue

Make error display and navigation handling more robust.

The current implementation has two issues that should be addressed:

  1. The error message follows a format that may not be ideal for all scenarios, with a potentially redundant prefix.
  2. Unconditionally popping the navigation stack could crash the app if called from a route that can't be popped.

Apply this diff to make the error handling more robust:

  void handleError(
    BuildContext context,
    WalletKitError error,
    String? message,
  ) {
    ScaffoldMessenger.of(context).showSnackBar(
-      SnackBar(content: Text('${message ?? "error"}: $error')),
+      SnackBar(content: Text(message != null ? '$message: $error' : '$error')),
    );
-    Navigator.of(context).pop();
+    if (Navigator.of(context).canPop()) {
+      Navigator.of(context).pop();
+    }
  }
🧹 Nitpick comments (2)
packages/wallet_kit/lib/errors/wallet_kit_error.dart (2)

11-15: Consider providing additional context handling options.

The error handler only supports showing a snackbar and popping the navigation stack, which may not be appropriate for all error scenarios.

Consider adding flexible options to customize the error handling behavior:

  void handleError(
    BuildContext context,
    WalletKitError error,
    String? message,
+   {bool popNavigation = true, 
+   Duration snackBarDuration = const Duration(seconds: 4)}
  ) {
    ScaffoldMessenger.of(context).showSnackBar(
-      SnackBar(content: Text('${message ?? "error"}: $error')),
+      SnackBar(
+        content: Text(message != null ? '$message: $error' : '$error'),
+        duration: snackBarDuration,
+      ),
    );
-    Navigator.of(context).pop();
+    if (popNavigation && Navigator.of(context).canPop()) {
+      Navigator.of(context).pop();
+    }
  }

3-21: Consider exposing the singleton instance directly.

The Singleton pattern implementation is good, but accessing it requires calling the constructor function, which might be slightly less intuitive than directly accessing a static instance.

Consider exposing the instance directly for easier access:

class WalletKitErrorHandler {
  static final WalletKitErrorHandler _instance =
      WalletKitErrorHandler._internal();
+  static WalletKitErrorHandler get instance => _instance;

  WalletKitErrorHandler._internal();

  factory WalletKitErrorHandler() => _instance;
  
  // rest of the class
}

// Usage would then be:
// WalletKitErrorHandler.instance.handleError(...) instead of WalletKitErrorHandler().handleError(...)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 38a99bf and c6ac3ff.

📒 Files selected for processing (3)
  • packages/wallet_kit/lib/errors/wallet_kit_error.dart (1 hunks)
  • packages/wallet_kit/lib/wallet_state/wallet_provider.dart (2 hunks)
  • packages/wallet_kit/lib/widgets/wallet_list.dart (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/wallet_kit/lib/wallet_state/wallet_provider.dart
  • packages/wallet_kit/lib/widgets/wallet_list.dart
🔇 Additional comments (1)
packages/wallet_kit/lib/errors/wallet_kit_error.dart (1)

23-32: LGTM! Well-structured error class implementation.

The WalletKitError class is well-designed with:

  • Proper implementation of the Exception interface
  • Good error formatting with optional code inclusion
  • Clear constructor definition

Copy link
Collaborator

@ptisserand ptisserand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created the following issue: #475 to track the remaining customization issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants