arsync_firebase_errors_handler 0.0.1 copy "arsync_firebase_errors_handler: ^0.0.1" to clipboard
arsync_firebase_errors_handler: ^0.0.1 copied to clipboard

Firebase error handlers for the Arsync Exception Toolkit

Arsync Firebase Errors Handler #

A specialized package for handling Firebase-specific errors with the Arsync Exception Toolkit.

pub package License: MIT

Features #

  • ✨ Specialized Firebase Error Handling: Provides human-friendly error messages for Firebase service exceptions
  • πŸ” Service-Specific Handlers: Dedicated handlers for Auth, Firestore, Functions, Storage, and Core Firebase errors
  • 🎯 Detailed Error Information: Includes detailed technical information for debugging
  • πŸš€ Easy Integration: Simple extensions to add Firebase handling to your exception toolkit
  • πŸ› οΈ Highly Customizable: Override default error messages and behaviors

Installation #

dependencies:
  arsync_exception_toolkit: ^0.1.0
  arsync_firebase_errors_handler: ^0.1.0
  # Other Firebase packages as needed

Basic Usage #

1. Add Firebase handlers to your toolkit #

// Create a toolkit with all Firebase handlers
final toolkit = ArsyncExceptionToolkit()
  .withAllFirebaseHandlers();

// Or add specific handlers
final toolkit = ArsyncExceptionToolkit()
  .withFirebaseAuthHandler()
  .withFirestoreHandler();

2. Use in try-catch blocks #

try {
  // Firebase operation that might fail
  await FirebaseAuth.instance.signInWithEmailAndPassword(
    email: email,
    password: password,
  );
} catch (e) {
  // The toolkit will automatically detect Firebase Auth errors
  final exception = toolkit.handleException(e);
  
  // User-friendly error information is available
  print(exception.title); // "Incorrect Password"
  print(exception.message); // "The password you entered is incorrect..."
  
  // Show the error to the user
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(content: Text(exception.briefMessage)),
  );
}

Advanced Features #

Customizing Error Messages #

You can customize the error messages for specific error codes using ArsyncException objects:

// Create a toolkit with custom Firebase Auth error messages
final toolkit = ArsyncExceptionToolkit()
  .withFirebaseAuthHandler(
    customExceptions: {
      FirebaseErrorCodes.wrongPassword: ArsyncException(
        icon: Icons.lock_outline,
        title: 'Wrong Password',
        message: 'The password you entered doesn\'t match our records. Please try again or use the "Forgot Password" option.',
        briefTitle: 'Login Failed',
        briefMessage: 'Incorrect password',
        exceptionCode: 'firebase_auth_wrong_password',
      ),
    },
  );

Using Specific Service Handlers #

You can choose which Firebase service handlers to include:

// Only include handlers for the services you use
final toolkit = ArsyncExceptionToolkit()
  .withFirebaseAuthHandler()
  .withFirestoreHandler();

Accessing Technical Details #

For debugging, you can access the technical details of the exception:

try {
  await FirebaseFirestore.instance.collection('users').doc('nonexistent').get();
} catch (e) {
  final exception = toolkit.handleException(e);
  
  // Log the technical details for debugging
  print(exception.technicalDetails);
  
  // Show a dialog with technical details in development
  if (kDebugMode) {
    showDialog(
      context: context,
      builder: (_) => AlertDialog(
        title: Text(exception.title),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(exception.message),
            const SizedBox(height: 16),
            const Text('Technical Details:', style: TextStyle(fontWeight: FontWeight.bold)),
            Text(exception.technicalDetails ?? 'Not available'),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('OK'),
          ),
        ],
      ),
    );
  }
}

Handler Classes #

The package includes these specialized handlers:

  1. FirebaseAuthHandler: Handles Firebase Authentication errors
  2. FirestoreHandler: Handles Firestore database errors
  3. FirebaseFunctionsHandler: Handles Cloud Functions errors
  4. FirebaseStorageHandler: Handles Firebase Storage errors
  5. FirebaseCoreHandler: Handles general Firebase Core errors
  6. FirebaseErrorsHandler: A combined handler that uses all of the above

Author #

Atif Siddiqui

About Arsync Solutions #

Arsync Solutions, We build Flutter apps for iOS, Android, and the web.

License #

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing #

Contributions are welcome! If you find a bug or want a feature, please open an issue.