@Override
    public Map<Errors, Integer> errorCounts() {
        if (data.errorCode() != Errors.NONE.code())
            // Minor optimization since the top-level error applies to all partitions
            return Collections.singletonMap(error(), data.partitionErrors().size() + 1);
        Map<Errors, Integer> errors = errorCounts(data.partitionErrors().stream().map(p -> Errors.forCode(p.errorCode())));
        updateErrorCounts(errors, Errors.forCode(data.errorCode())); // top level error
        return errors;
    }
@Test
    public void testErrorCountsWithTopLevelError() {
        List<StopReplicaPartitionError> errors = new ArrayList<>();
        errors.add(new StopReplicaPartitionError().setTopicName("foo").setPartitionIndex(0));
        errors.add(new StopReplicaPartitionError().setTopicName("foo").setPartitionIndex(1)
            .setErrorCode(Errors.NOT_LEADER_OR_FOLLOWER.code()));
        StopReplicaResponse response = new StopReplicaResponse(new StopReplicaResponseData()
            .setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code())
            .setPartitionErrors(errors));
        assertEquals(Collections.singletonMap(Errors.UNKNOWN_SERVER_ERROR, 3), response.errorCounts());
    }