kafkajs
Version:
A modern Apache Kafka client for node.js
43 lines (35 loc) • 1.08 kB
JavaScript
const Decoder = require('../../../decoder')
const { failure, createErrorFromCode } = require('../../../error')
/*
* CreatePartitions Response (Version: 0) => throttle_time_ms [topic_errors]
* throttle_time_ms => INT32
* topic_errors => topic error_code error_message
* topic => STRING
* error_code => INT16
* error_message => NULLABLE_STRING
*/
const topicNameComparator = (a, b) => a.topic.localeCompare(b.topic)
const topicErrors = decoder => ({
topic: decoder.readString(),
errorCode: decoder.readInt16(),
errorMessage: decoder.readString(),
})
const decode = async rawData => {
const decoder = new Decoder(rawData)
const throttleTime = decoder.readInt32()
return {
throttleTime,
topicErrors: decoder.readArray(topicErrors).sort(topicNameComparator),
}
}
const parse = async data => {
const topicsWithError = data.topicErrors.filter(({ errorCode }) => failure(errorCode))
if (topicsWithError.length > 0) {
throw createErrorFromCode(topicsWithError[0].errorCode)
}
return data
}
module.exports = {
decode,
parse,
}