Search in sources :

Example 91 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class CreateTopicsRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(Throwable e) {
    Map<String, CreateTopicsResponse.Error> topicErrors = new HashMap<>();
    for (String topic : topics.keySet()) {
        Errors error = Errors.forException(e);
        // Avoid populating the error message if it's a generic one
        String message = error.message().equals(e.getMessage()) ? null : e.getMessage();
        topicErrors.put(topic, new CreateTopicsResponse.Error(error, message));
    }
    short versionId = version();
    switch(versionId) {
        case 0:
        case 1:
            return new CreateTopicsResponse(topicErrors);
        default:
            throw new IllegalArgumentException(String.format("Version %d is not valid. Valid versions for %s are 0 to %d", versionId, this.getClass().getSimpleName(), ApiKeys.CREATE_TOPICS.latestVersion()));
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap)

Example 92 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class MetadataRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(Throwable e) {
    List<MetadataResponse.TopicMetadata> topicMetadatas = new ArrayList<>();
    Errors error = Errors.forException(e);
    List<MetadataResponse.PartitionMetadata> partitions = Collections.emptyList();
    if (topics != null) {
        for (String topic : topics) topicMetadatas.add(new MetadataResponse.TopicMetadata(error, topic, false, partitions));
    }
    short versionId = version();
    switch(versionId) {
        case 0:
        case 1:
        case 2:
            return new MetadataResponse(Collections.<Node>emptyList(), null, MetadataResponse.NO_CONTROLLER_ID, topicMetadatas);
        default:
            throw new IllegalArgumentException(String.format("Version %d is not valid. Valid versions for %s are 0 to %d", versionId, this.getClass().getSimpleName(), ApiKeys.METADATA.latestVersion()));
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) ArrayList(java.util.ArrayList)

Example 93 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class StopReplicaResponse method toStruct.

@Override
protected Struct toStruct(short version) {
    Struct struct = new Struct(ApiKeys.STOP_REPLICA.responseSchema(version));
    List<Struct> responseDatas = new ArrayList<>(responses.size());
    for (Map.Entry<TopicPartition, Errors> response : responses.entrySet()) {
        Struct partitionData = struct.instance(PARTITIONS_KEY_NAME);
        TopicPartition partition = response.getKey();
        partitionData.set(PARTITIONS_TOPIC_KEY_NAME, partition.topic());
        partitionData.set(PARTITIONS_PARTITION_KEY_NAME, partition.partition());
        partitionData.set(PARTITIONS_ERROR_CODE_KEY_NAME, response.getValue().code());
        responseDatas.add(partitionData);
    }
    struct.set(PARTITIONS_KEY_NAME, responseDatas.toArray());
    struct.set(ERROR_CODE_KEY_NAME, error.code());
    return struct;
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 94 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class LeaderAndIsrRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(Throwable e) {
    Map<TopicPartition, Errors> responses = new HashMap<>(partitionStates.size());
    for (TopicPartition partition : partitionStates.keySet()) {
        responses.put(partition, Errors.forException(e));
    }
    short versionId = version();
    switch(versionId) {
        case 0:
            return new LeaderAndIsrResponse(Errors.NONE, responses);
        default:
            throw new IllegalArgumentException(String.format("Version %d is not valid. Valid versions for %s are 0 to %d", versionId, this.getClass().getSimpleName(), ApiKeys.LEADER_AND_ISR.latestVersion()));
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition)

Example 95 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class StopReplicaRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(Throwable e) {
    Map<TopicPartition, Errors> responses = new HashMap<>(partitions.size());
    for (TopicPartition partition : partitions) {
        responses.put(partition, Errors.forException(e));
    }
    short versionId = version();
    switch(versionId) {
        case 0:
            return new StopReplicaResponse(Errors.NONE, responses);
        default:
            throw new IllegalArgumentException(String.format("Version %d is not valid. Valid versions for %s are 0 to %d", versionId, this.getClass().getSimpleName(), ApiKeys.STOP_REPLICA.latestVersion()));
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition)

Aggregations

Errors (org.apache.kafka.common.protocol.Errors)167 HashMap (java.util.HashMap)115 TopicPartition (org.apache.kafka.common.TopicPartition)87 Map (java.util.Map)61 ArrayList (java.util.ArrayList)46 LinkedHashMap (java.util.LinkedHashMap)31 Test (org.junit.jupiter.api.Test)31 List (java.util.List)19 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)19 HashSet (java.util.HashSet)18 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)18 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)17 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)17 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)17 KafkaException (org.apache.kafka.common.KafkaException)16 Node (org.apache.kafka.common.Node)16 Cluster (org.apache.kafka.common.Cluster)15 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)14 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)14 Collections (java.util.Collections)13