error method

  1. @override
void error(
  1. String message, {
  2. Object? error,
  3. StackTrace? stackTrace,
  4. Map<String, Object?>? extras,
})
override

Logs an error for failures and exceptions.

Error messages represent failures that impact functionality, such as network failures or unexpected exceptions that require investigation. Implementations should send errors to remote monitoring (Sentry) for production debugging.

The message describes what failed. The optional error and stackTrace identify the caught exception. The extras map may include additional context such as request payloads or application state at the time of failure.

Example:

try {
  await placeOrder();
} catch (e, stackTrace) {
  logger.error(
    'Failed to place order',
    error: e,
    stackTrace: stackTrace,
    extras: {'orderId': '789'},
  );
}

Implementation

@override
void error(String message, {Object? error, StackTrace? stackTrace, Map<String, Object?>? extras}) {
  _breadcrumb(message, level: SentryLevel.error, category: 'app.error', extras: extras);

  if (error != null) {
    unawaited(
      Sentry.captureException(
        error,
        stackTrace: stackTrace,
        withScope: (Scope scope) {
          scope.level = SentryLevel.error;
          scope.setContexts('details', <String, Object?>{'message': message, ...?extras});
        },
      ),
    );
  } else {
    unawaited(
      Sentry.captureMessage(
        message,
        level: SentryLevel.error,
        withScope: (Scope scope) {
          if (extras != null && extras.isNotEmpty) scope.setContexts('details', extras);
        },
      ),
    );
  }

  if (kDebugMode) {
    debugPrint('❌ $message');
    if (error != null) debugPrint('❌ error: $error');
    if (stackTrace != null) debugPrint('❌ stack: $stackTrace');
  }
}