Files
TravelMate/lib/services/error_service.dart
Dayron 0162eb67f5 feat: Implement centralized error handling with ErrorService; replace print statements with logging in services and blocs
feat: Add ErrorContent widget for displaying error messages in dialogs and bottom sheets
refactor: Update GroupBloc and GroupRepository to utilize ErrorService for error logging
refactor: Enhance user and trip services to log errors using ErrorService
refactor: Clean up debug print statements in GroupContent and related components
2025-10-15 11:43:21 +02:00

82 lines
2.4 KiB
Dart
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import '../components/error/error_content.dart';
class ErrorService {
static final ErrorService _instance = ErrorService._internal();
factory ErrorService() => _instance;
ErrorService._internal();
// GlobalKey pour accéder au context depuis n'importe où
static GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
// Afficher une erreur en dialog
void showError({
required String message,
String title = 'Erreur',
VoidCallback? onRetry,
IconData icon = Icons.error_outline,
Color? iconColor,
}) {
final context = navigatorKey.currentContext;
if (context != null) {
showErrorDialog(
context,
title: title,
message: message,
icon: icon,
iconColor: iconColor,
onRetry: onRetry,
);
}
}
// Afficher une erreur en snackbar
void showSnackbar({
required String message,
VoidCallback? onRetry,
bool isError = true,
}) {
final context = navigatorKey.currentContext;
if (context != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: isError ? Colors.red[400] : Colors.green[600],
duration: const Duration(seconds: 4),
action: onRetry != null
? SnackBarAction(
label: 'Réessayer',
textColor: Colors.white,
onPressed: onRetry,
)
: null,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
);
}
}
// Logger dans la console (développement)
void logError(String source, dynamic error, [StackTrace? stackTrace]) {
print('═══════════════════════════════════');
print('❌ ERREUR dans $source');
print('Message: $error');
if (stackTrace != null) {
print('StackTrace: $stackTrace');
}
print('═══════════════════════════════════');
}
// Logger une info (développement)
void logInfo(String source, String message) {
print(' [$source] $message');
}
// Logger un succès
void logSuccess(String source, String message) {
print('✅ [$source] $message');
}
}