@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 testErrorCountsNoTopLevelError() {
        for (short version : LEADER_AND_ISR.allVersions()) {
            LeaderAndIsrResponse response;
            if (version < 5) {
                List<LeaderAndIsrPartitionError> partitions = createPartitions("foo",
                        asList(Errors.NONE, Errors.CLUSTER_AUTHORIZATION_FAILED));
                response = new LeaderAndIsrResponse(new LeaderAndIsrResponseData()
                        .setErrorCode(Errors.NONE.code())
                        .setPartitionErrors(partitions), version);
            } else {
                Uuid id = Uuid.randomUuid();
                LeaderAndIsrTopicErrorCollection topics = createTopic(id, asList(Errors.NONE, Errors.CLUSTER_AUTHORIZATION_FAILED));
                response = new LeaderAndIsrResponse(new LeaderAndIsrResponseData()
                        .setErrorCode(Errors.NONE.code())
                        .setTopics(topics), version);
            }
            Map<Errors, Integer> errorCounts = response.errorCounts();
            assertEquals(2, errorCounts.size());
            assertEquals(2, errorCounts.get(Errors.NONE).intValue());
            assertEquals(1, errorCounts.get(Errors.CLUSTER_AUTHORIZATION_FAILED).intValue());
        }
    }