formatError function

String formatError(
  1. Object error
)

Formats an error into a human-readable string, with truncation for very long messages.

Implementation

String formatError(Object error) {
  if (error is AbortError) {
    return error.message.isNotEmpty
        ? error.message
        : interruptMessageForToolUse;
  }
  if (error is! Exception && error is! Error) {
    return error.toString();
  }

  final parts = getErrorParts(error);
  final fullMessage = parts.where((p) => p.isNotEmpty).join('\n').trim();
  final result = fullMessage.isEmpty
      ? 'Command failed with no output'
      : fullMessage;

  if (result.length <= 10000) return result;

  const halfLength = 5000;
  final start = result.substring(0, halfLength);
  final end = result.substring(result.length - halfLength);
  return '$start\n\n... [${result.length - 10000} characters truncated] ...\n\n$end';
}