handleAuthErrorAndRetry<T> method

Future<T> handleAuthErrorAndRetry<T>(
  1. Future<T> request(),
  2. Object error,
  3. StackTrace stackTrace
)

A helper to handle auth errors by reconnecting and retrying the request.

This is called when a 401/403 error occurs, indicating an expired token. It attempts a silent reconnect to refresh the token and then retries the original function request.

Implementation

Future<T> handleAuthErrorAndRetry<T>(
    Future<T> Function() request, Object error, StackTrace stackTrace) async {
  debugPrint('Authentication error occurred. Attempting to reconnect...');
  isAuthenticated = false;
  // Silently try to reconnect to refresh the auth token.
  final reconnectedProvider = await GoogleDriveProvider.connect();
  if (reconnectedProvider != null && reconnectedProvider.isAuthenticated) {
    debugPrint('Successfully reconnected. Retrying the original request.');
    // Retry the original request closure.
    return await request();
  } else {
    debugPrint(
        'Failed to reconnect after auth error. Throwing original error.');
    // If reconnection fails, rethrow the original error to the caller.
    throw error;
  }
}