private static AddPartitionsToTxnTopicResultCollection topicCollectionForErrors(Map<TopicPartition, Errors> errors) {
        Map<String, AddPartitionsToTxnPartitionResultCollection> resultMap = new HashMap<>();

        for (Map.Entry<TopicPartition, Errors> entry : errors.entrySet()) {
            TopicPartition topicPartition = entry.getKey();
            String topicName = topicPartition.topic();

            AddPartitionsToTxnPartitionResult partitionResult =
                    new AddPartitionsToTxnPartitionResult()
                        .setPartitionErrorCode(entry.getValue().code())
                        .setPartitionIndex(topicPartition.partition());

            AddPartitionsToTxnPartitionResultCollection partitionResultCollection = resultMap.getOrDefault(
                    topicName, new AddPartitionsToTxnPartitionResultCollection()
            );

            partitionResultCollection.add(partitionResult);
            resultMap.put(topicName, partitionResultCollection);
        }

        AddPartitionsToTxnTopicResultCollection topicCollection = new AddPartitionsToTxnTopicResultCollection();
        for (Map.Entry<String, AddPartitionsToTxnPartitionResultCollection> entry : resultMap.entrySet()) {
            topicCollection.add(new AddPartitionsToTxnTopicResult()
                .setName(entry.getKey())
                .setResultsByPartition(entry.getValue()));
        }
        return topicCollection;
    }
Possible error codes:

  - {@link Errors#NOT_COORDINATOR}
  - {@link Errors#COORDINATOR_NOT_AVAILABLE}
  - {@link Errors#COORDINATOR_LOAD_IN_PROGRESS}
  - {@link Errors#INVALID_TXN_STATE}
  - {@link Errors#INVALID_PRODUCER_ID_MAPPING}
  - {@link Errors#INVALID_PRODUCER_EPOCH} // for version <=1
  - {@link Errors#PRODUCER_FENCED}
  - {@link Errors#TOPIC_AUTHORIZATION_FAILED}
  - {@link Errors#TRANSACTIONAL_ID_AUTHORIZATION_FAILED}
  - {@link Errors#UNKNOWN_TOPIC_OR_PARTITION}
clients/src/main/java/org/apache/kafka/common/requests/AddPartitionsToTxnResponse.java