public void write(Struct struct) { struct.set(ERROR_CODE, error.code()); if (error != Errors.NONE) struct.setIfExists(ERROR_MESSAGE, message); }
public ApiException exception() { return error.exception(message); }
public SaslHandshakeResponse(Struct struct) { error = Errors.forCode(struct.get(ERROR_CODE)); Object[] mechanisms = struct.getArray(ENABLED_MECHANISMS_KEY_NAME); ArrayList<String> enabledMechanisms = new ArrayList<>(); for (Object mechanism : mechanisms) enabledMechanisms.add((String) mechanism); this.enabledMechanisms = enabledMechanisms; }
public static ApiError fromThrowable(Throwable t) { // Avoid populating the error message if it's a generic one Errors error = Errors.forException(t); String message = error.message().equals(t.getMessage()) ? null : t.getMessage(); return new ApiError(error, message); }
@Test public void testUniqueExceptions() { Set<Class> exceptionSet = new HashSet<>(); for (Errors error : Errors.values()) { if (error != Errors.NONE) exceptionSet.add(error.exception().getClass()); } assertEquals("Exceptions must be unique", exceptionSet.size(), Errors.values().length - 1); // Ignore NONE }
error == Errors.REPLICA_NOT_AVAILABLE || error == Errors.KAFKA_STORAGE_ERROR) { log.debug("Error in fetch for partition {}: {}", tp, error.exceptionName()); this.metadata.requestUpdate(); } else if (error == Errors.UNKNOWN_TOPIC_OR_PARTITION) { log.warn("Unknown error fetching data for topic-partition {}", tp); } else { throw new IllegalStateException("Unexpected error code " + error.code() + " while fetching data");
private ElectPreferredLeadersResponse createElectPreferredLeadersResponse() { ElectPreferredLeadersResponseData data = new ElectPreferredLeadersResponseData().setThrottleTimeMs(200); ReplicaElectionResult resultsByTopic = new ReplicaElectionResult().setTopic("myTopic"); resultsByTopic.partitionResult().add(new PartitionResult().setPartitionId(0) .setErrorCode(Errors.NONE.code()) .setErrorMessage(Errors.NONE.message())); resultsByTopic.partitionResult().add(new PartitionResult().setPartitionId(1) .setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code()) .setErrorMessage(Errors.UNKNOWN_TOPIC_OR_PARTITION.message())); data.replicaElectionResults().add(resultsByTopic); return new ElectPreferredLeadersResponse(data); }
@Override public TxnOffsetCommitResponse getErrorResponse(int throttleTimeMs, Throwable e) { Errors error = Errors.forException(e); Map<TopicPartition, Errors> errors = new HashMap<>(offsets.size()); for (TopicPartition partition : offsets.keySet()) errors.put(partition, error); return new TxnOffsetCommitResponse(throttleTimeMs, errors); }
@Test public void testExceptionName() { String exceptionName = Errors.UNKNOWN_SERVER_ERROR.exceptionName(); assertEquals("org.apache.kafka.common.errors.UnknownServerException", exceptionName); exceptionName = Errors.NONE.exceptionName(); assertNull(exceptionName); exceptionName = Errors.INVALID_TOPIC_EXCEPTION.exceptionName(); assertEquals("org.apache.kafka.common.errors.InvalidTopicException", exceptionName); }
@Override public Map<Errors, Integer> errorCounts(Throwable e) { Errors error = Errors.forException(e); return Collections.singletonMap(error, partitions().size()); }
@Override protected Struct toStruct(short version) { Struct struct = new Struct(ApiKeys.DELETE_GROUPS.responseSchema(version)); struct.setIfExists(THROTTLE_TIME_MS, throttleTimeMs); List<Struct> groupErrorCodeStructs = new ArrayList<>(errors.size()); for (Map.Entry<String, Errors> groupError : errors.entrySet()) { Struct groupErrorCodeStruct = struct.instance(GROUP_ERROR_CODES_KEY_NAME); groupErrorCodeStruct.set(GROUP_ID, groupError.getKey()); groupErrorCodeStruct.set(ERROR_CODE, groupError.getValue().code()); groupErrorCodeStructs.add(groupErrorCodeStruct); } struct.set(GROUP_ERROR_CODES_KEY_NAME, groupErrorCodeStructs.toArray()); return struct; }
/** * Raise an error. The request will be marked as failed. * @param error corresponding error to be passed to caller */ public void raise(Errors error) { raise(error.exception()); }
public LeaderAndIsrResponse(Struct struct) { responses = new HashMap<>(); for (Object responseDataObj : struct.get(PARTITIONS)) { Struct responseData = (Struct) responseDataObj; String topic = responseData.get(TOPIC_NAME); int partition = responseData.get(PARTITION_ID); Errors error = Errors.forCode(responseData.get(ERROR_CODE)); responses.put(new TopicPartition(topic, partition), error); } error = Errors.forCode(struct.get(ERROR_CODE)); }
@Override public WriteTxnMarkersResponse getErrorResponse(int throttleTimeMs, Throwable e) { Errors error = Errors.forException(e); Map<Long, Map<TopicPartition, Errors>> errors = new HashMap<>(markers.size()); for (TxnMarkerEntry entry : markers) { Map<TopicPartition, Errors> errorsPerPartition = new HashMap<>(entry.partitions.size()); for (TopicPartition partition : entry.partitions) errorsPerPartition.put(partition, error); errors.put(entry.producerId, errorsPerPartition); } return new WriteTxnMarkersResponse(errors); }
@Override protected Struct toStruct(short version) { Struct struct = new Struct(ApiKeys.DELETE_TOPICS.responseSchema(version)); struct.setIfExists(THROTTLE_TIME_MS, throttleTimeMs); List<Struct> topicErrorCodeStructs = new ArrayList<>(errors.size()); for (Map.Entry<String, Errors> topicError : errors.entrySet()) { Struct topicErrorCodeStruct = struct.instance(TOPIC_ERROR_CODES_KEY_NAME); topicErrorCodeStruct.set(TOPIC_NAME, topicError.getKey()); topicErrorCodeStruct.set(ERROR_CODE, topicError.getValue().code()); topicErrorCodeStructs.add(topicErrorCodeStruct); } struct.set(TOPIC_ERROR_CODES_KEY_NAME, topicErrorCodeStructs.toArray()); return struct; }
private boolean handleFindCoordinatorError(FindCoordinatorResponse response, KafkaFutureImpl<?> future) { Errors error = response.error(); if (error.exception() instanceof RetriableException) { throw error.exception(); } else if (response.hasError()) { future.completeExceptionally(error.exception()); return true; } return false; }
public StopReplicaResponse(Struct struct) { responses = new HashMap<>(); for (Object responseDataObj : struct.get(PARTITIONS)) { Struct responseData = (Struct) responseDataObj; String topic = responseData.get(TOPIC_NAME); int partition = responseData.get(PARTITION_ID); Errors error = Errors.forCode(responseData.get(ERROR_CODE)); responses.put(new TopicPartition(topic, partition), error); } error = Errors.forCode(struct.get(ERROR_CODE)); }
@Override public AddPartitionsToTxnResponse getErrorResponse(int throttleTimeMs, Throwable e) { final HashMap<TopicPartition, Errors> errors = new HashMap<>(); for (TopicPartition partition : partitions) { errors.put(partition, Errors.forException(e)); } return new AddPartitionsToTxnResponse(throttleTimeMs, errors); }