toHeaderString method

String toHeaderString()

Implementation

String toHeaderString() {
  final buffer = StringBuffer();
  var listMemberCount = 0;
  var separator = '';

  for (final entry in _keyValues.entries) {
    if (listMemberCount >= _maxListMember) {
      logger?.call(
        SentryLevel.info,
        'Baggage key ${entry.key} dropped because of max list member.',
      );
      break;
    }
    try {
      final encodedKey = _urlEncode(entry.key);
      final encodedValue = _urlEncode(entry.value);
      final encodedKeyValue = '$separator$encodedKey=$encodedValue';

      final totalLengthIfValueAdded = buffer.length + encodedKeyValue.length;

      if (totalLengthIfValueAdded >= _maxChars) {
        logger?.call(
          SentryLevel.info,
          'Baggage key ${entry.key} dropped because of max baggage chars.',
        );
        continue;
      }

      listMemberCount++;
      buffer.write(encodedKeyValue);
      separator = ',';
    } catch (exception, stackTrace) {
      logger?.call(
        SentryLevel.error,
        'Failed to parse the baggage key ${entry.key}.',
        exception: exception,
        stackTrace: stackTrace,
      );
      // TODO rethrow in options.automatedTestMode (currently not available here to check)
    }
  }

  return buffer.toString();
}