handleError method

Future<void> handleError(
  1. dynamic error,
  2. StackTrace stackTrace, {
  3. String? source,
  4. Map<String, dynamic>? additionalData,
  5. bool displayToUser = true,
})

Implementation

Future<void> handleError(
  dynamic error,
  StackTrace stackTrace, {
  String? source,
  Map<String, dynamic>? additionalData,
  bool displayToUser = true,
}) async {
  // Log the error
  await _saveErrorToStorage(error, stackTrace, source, additionalData);

  // Send to trackers
  for (final tracker in _trackers) {
    if (tracker.shouldHandleCrashlog()) {
      await tracker.trackError(
        error: error,
        stackTrace: stackTrace,
        source: source,
        additionalData: additionalData,
      );
    }
  }

  // Determine if we should display the error to the user
  final bool shouldDisplay = displayToUser && (_shouldDisplayErrorCallback?.call(error, stackTrace) ?? true);

  // Display the error to the user if needed
  if (shouldDisplay && _errorDisplayCallback != null) {
    String? errorMessage;

    // Try to translate the error
    if (_errorTranslationCallback != null) {
      errorMessage = _errorTranslationCallback!(error, stackTrace, source);
    }

    // Use default error message if translation failed
    errorMessage ??= _defaultErrorMessage;

    // Display the error
    _errorDisplayCallback!(errorMessage, additionalData: additionalData);
  }
}