hwhub/lib/utils/error_utils.dart
2026-03-09 18:32:45 +01:00

67 lines
1.6 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
/// Base app-level exception for expected, user-facing failures.
class AppException implements Exception {
final String message;
const AppException(this.message);
@override
String toString() => message;
}
class AuthRequiredException extends AppException {
const AuthRequiredException(super.message);
}
class PermissionDeniedException extends AppException {
const PermissionDeniedException(super.message);
}
class NotFoundException extends AppException {
const NotFoundException(super.message);
}
class ValidationException extends AppException {
const ValidationException(super.message);
}
String userMessageForError(
Object error, {
String fallback = 'Something went wrong. Please try again.',
}) {
if (error is AppException) {
return error.message;
}
if (error is AuthException) {
return error.message;
}
final raw = error.toString();
final normalized = raw.toLowerCase();
if (normalized.contains('socket') ||
normalized.contains('network') ||
normalized.contains('timeout')) {
return 'Network issue. Please check your connection and try again.';
}
if (normalized.contains('permission') || normalized.contains('not allowed')) {
return 'You do not have permission for this action.';
}
if (normalized.contains('signed in')) {
return 'Please sign in again and retry.';
}
return fallback;
}
void logError(String scope, Object error, [StackTrace? stackTrace]) {
debugPrint('[$scope] $error');
if (stackTrace != null) {
debugPrint('$stackTrace');
}
}