@Override
    public Map<Errors, Integer> errorCounts() {
        Errors error = error();
        if (error != Errors.NONE) {
            // Minor optimization since the top-level error applies to all partitions
            if (version < 5) 
                return Collections.singletonMap(error, data.partitionErrors().size() + 1);
            return Collections.singletonMap(error, 
                    data.topics().stream().mapToInt(t -> t.partitionErrors().size()).sum() + 1);
        }
        Map<Errors, Integer> errors;
        if (version < 5)
            errors = errorCounts(data.partitionErrors().stream().map(l -> Errors.forCode(l.errorCode())));
        else
            errors = errorCounts(data.topics().stream().flatMap(t -> t.partitionErrors().stream()).map(l ->
                Errors.forCode(l.errorCode())));
        updateErrorCounts(errors, Errors.NONE);
        return errors;
    }
@Test
    public void testErrorCountsFromGetErrorResponse() {
        List<LeaderAndIsrPartitionState> partitionStates = new ArrayList<>();
        partitionStates.add(new LeaderAndIsrPartitionState()
            .setTopicName("foo")
            .setPartitionIndex(0)
            .setControllerEpoch(15)
            .setLeader(1)
            .setLeaderEpoch(10)
            .setIsr(Collections.singletonList(10))
            .setPartitionEpoch(20)
            .setReplicas(Collections.singletonList(10))
            .setIsNew(false));
        partitionStates.add(new LeaderAndIsrPartitionState()
            .setTopicName("foo")
            .setPartitionIndex(1)
            .setControllerEpoch(15)
            .setLeader(1)
            .setLeaderEpoch(10)
            .setIsr(Collections.singletonList(10))
            .setPartitionEpoch(20)
            .setReplicas(Collections.singletonList(10))
            .setIsNew(false));
        Map<String, Uuid> topicIds = Collections.singletonMap("foo", Uuid.randomUuid());

        LeaderAndIsrRequest request = new LeaderAndIsrRequest.Builder(ApiKeys.LEADER_AND_ISR.latestVersion(),
                15, 20, 0, partitionStates, topicIds, Collections.emptySet()).build();
        LeaderAndIsrResponse response = request.getErrorResponse(0, Errors.CLUSTER_AUTHORIZATION_FAILED.exception());
        assertEquals(Collections.singletonMap(Errors.CLUSTER_AUTHORIZATION_FAILED, 3), response.errorCounts());
    }