errorCallback static method

void Function(Object error, StackTrace stack) errorCallback({
  1. String prefix = defaultPrefix,
  2. bool includeStack = true,
  3. bool useDeveloperLog = true,
})

Creates a DriftDebugOnError-compatible callback that logs errors and optionally stack traces.

prefix is included in the log name for filtering. When includeStack is true (default), the stack trace is passed to developer.log only in debug builds (avoid_stack_trace_in_production). Logging never throws. Empty prefix uses the default prefix constant for the log name. useDeveloperLog is reserved for future use.

Returns a function that logs the given error and stack with optional prefix.

Implementation

static void Function(Object error, StackTrace stack) errorCallback({
  String prefix = defaultPrefix,
  bool includeStack = true,
  bool useDeveloperLog = true,
}) {
  return (Object error, StackTrace stack) {
    try {
      final String name = prefix.isEmpty ? defaultPrefix : prefix;
      final bool includeTrace = includeStack && _isDebugEnvironment();

      // Structured log for DevTools.
      developer.log(
        error.toString(),
        name: name,
        level: _severityLevel,
        error: error,
        stackTrace: includeTrace ? stack : null,
      );

      // Console output so errors are visible without DevTools.
      final String line = '[$name] $error';
      // ignore: avoid_print
      print(line);
      if (includeTrace) {
        // ignore: avoid_print
        print(stack);
      }
    } on Object catch (e, st) {
      // Defensive: error callback must not throw so server catch blocks stay safe.
      developer.log(
        'Error callback failed',
        name: _kLoggerFailureName,
        error: e,
        stackTrace: _isDebugEnvironment() ? st : null,
      );
    }
  };
}