@Override
    public LeaderAndIsrResponse getErrorResponse(int throttleTimeMs, Throwable e) {
        LeaderAndIsrResponseData responseData = new LeaderAndIsrResponseData();
        Errors error = Errors.forException(e);
        responseData.setErrorCode(error.code());

        if (version() < 5) {
            List<LeaderAndIsrPartitionError> partitions = new ArrayList<>();
            for (LeaderAndIsrPartitionState partition : partitionStates()) {
                partitions.add(new LeaderAndIsrPartitionError()
                        .setTopicName(partition.topicName())
                        .setPartitionIndex(partition.partitionIndex())
                        .setErrorCode(error.code()));
            }
            responseData.setPartitionErrors(partitions);
        } else {
            for (LeaderAndIsrTopicState topicState : data.topicStates()) {
                List<LeaderAndIsrPartitionError> partitions = new ArrayList<>(
                    topicState.partitionStates().size());
                for (LeaderAndIsrPartitionState partition : topicState.partitionStates()) {
                    partitions.add(new LeaderAndIsrPartitionError()
                        .setPartitionIndex(partition.partitionIndex())
                        .setErrorCode(error.code()));
                }
                responseData.topics().add(new LeaderAndIsrTopicError()
                    .setTopicId(topicState.topicId())
                    .setPartitionErrors(partitions));
            }
        }

        return new LeaderAndIsrResponse(responseData, version());
    }
@Test
    public void testGetErrorResponse() {
        Uuid topicId = Uuid.randomUuid();
        String topicName = "topic";
        int partition = 0;
        for (short version : LEADER_AND_ISR.allVersions()) {
            LeaderAndIsrRequest request = new LeaderAndIsrRequest.Builder(version, 0, 0, 0,
                Collections.singletonList(new LeaderAndIsrPartitionState()
                    .setTopicName(topicName)
                    .setPartitionIndex(partition)),
                Collections.singletonMap(topicName, topicId),
                Collections.emptySet()
            ).build(version);

            LeaderAndIsrResponse response = request.getErrorResponse(0,
                new ClusterAuthorizationException("Not authorized"));

            assertEquals(Errors.CLUSTER_AUTHORIZATION_FAILED, response.error());

            if (version < 5) {
                assertEquals(
                    Collections.singletonList(new LeaderAndIsrPartitionError()
                        .setTopicName(topicName)
                        .setPartitionIndex(partition)
                        .setErrorCode(Errors.CLUSTER_AUTHORIZATION_FAILED.code())),
                    response.data().partitionErrors());
                assertEquals(0, response.data().topics().size());
            } else {
                LeaderAndIsrTopicError topicState = response.topics().find(topicId);
                assertEquals(topicId, topicState.topicId());
                assertEquals(
                    Collections.singletonList(new LeaderAndIsrPartitionError()
                        .setPartitionIndex(partition)
                        .setErrorCode(Errors.CLUSTER_AUTHORIZATION_FAILED.code())),
                    topicState.partitionErrors());
                assertEquals(0, response.data().partitionErrors().size());
            }
        }
    }