handleError method

Future<RecoveryAction> handleError(
  1. LocusError error, {
  2. bool isTrackingActive = false,
  3. int? batteryLevel,
  4. bool? isCharging,
  5. bool? networkAvailable,
})

Handles an error and determines recovery action.

Returns the recovery action to take.

Implementation

Future<RecoveryAction> handleError(
  LocusError error, {
  bool isTrackingActive = false,
  int? batteryLevel,
  bool? isCharging,
  bool? networkAvailable,
}) async {
  // Check if we should ignore this error type
  if (_config.ignoreTypes.contains(error.type)) {
    return RecoveryAction.ignore;
  }

  // Track retry counts
  final retryCount = _retryCounts[error.type] ?? 0;
  _retryCounts[error.type] = retryCount + 1;

  // Track first occurrence
  _firstOccurrences[error.type] ??= DateTime.now();
  final timeSinceFirst =
      DateTime.now().difference(_firstOccurrences[error.type]!);

  // Build context
  final context = ErrorContext(
    retryCount: retryCount,
    timeSinceFirstOccurrence: timeSinceFirst,
    isTrackingActive: isTrackingActive,
    batteryLevel: batteryLevel,
    isCharging: isCharging,
    networkAvailable: networkAvailable,
  );

  // Log if configured
  if (_config.logErrors) {
    debugPrint('[Locus] Error: ${error.type.name} - ${error.message} '
        '(attempt ${retryCount + 1}/${_config.maxRetries})');
  }

  // Let user decide
  RecoveryAction action;
  if (_config.onError != null) {
    action = _config.onError!(error, context);
  } else {
    action = _determineRecoveryAction(error, context);
  }

  // Emit error
  _errorController.add(error);

  return action;
}