captureException static method

Future<void> captureException(
  1. Object exception,
  2. StackTrace stackTrace, {
  3. ErrorSeverity severity = ErrorSeverity.error,
  4. Map<String, dynamic>? extra,
})

Capture exception

Implementation

static Future<void> captureException(
  Object exception,
  StackTrace stackTrace, {
  ErrorSeverity severity = ErrorSeverity.error,
  Map<String, dynamic>? extra,
}) async {
  if (!_config.enabled) return;

  // Sample rate check
  if (_config.sampleRate < 1.0) {
    if (DateTime.now().millisecondsSinceEpoch % 100 >= _config.sampleRate * 100) {
      return;
    }
  }

  // Build error context
  final errorContext = _buildErrorContext(extra);

  // Store in history
  _addToHistory(ErrorEvent(
    exception: exception,
    stackTrace: stackTrace,
    severity: severity,
    context: errorContext,
    timestamp: DateTime.now(),
  ));

  // Call custom handler
  if (_config.onError != null) {
    try {
      await _config.onError!(exception, stackTrace, errorContext);
    } catch (e) {
      debugPrint('Error in custom error handler: $e');
    }
  }

  // Log to console in debug mode
  if (kDebugMode) {
    debugPrint('SwiftErrorTracker: $severity - $exception');
    debugPrint('Stack trace: $stackTrace');
  }
}