error method

  1. @override
Future<void> error(
  1. LogEntry entry
)
override

Records an error or a structured event with an error to Sentry.

entry - The complete log entry containing error message, level, timestamp, context, stackTrace, and event.

Implementation

@override
Future<void> error(LogEntry entry) async {
  try {
    if (shouldLog(event: entry.event)) {
      // Merge context from entry.context and event.parameters
      final context = <String, dynamic>{};
      if (entry.context != null) {
        context.addAll(entry.context!);
      }
      if (entry.event?.parameters != null) {
        context.addAll(entry.event!.parameters!);
      }

      // Add context to Sentry using structured contexts
      if (context.isNotEmpty) {
        Sentry.configureScope((scope) {
          scope.setContexts('log_context', context);
        });
      }

      Sentry.captureException(entry.message, stackTrace: entry.stackTrace);
    }
  } catch (e, stack) {
    developer.log(
      'Error during error handling in Sentry Strategy',
      name: 'SentryLogStrategy',
      error: e,
      stackTrace: stack,
    );
  }
}