forked from tulios/kafkajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.js
43 lines (36 loc) · 1.09 KB
/
response.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const Decoder = require('../../../decoder')
const { failure, createErrorFromCode } = require('../../../error')
const { KafkaJSAggregateError, KafkaJSCreateTopicError } = require('../../../../errors')
/**
* CreateTopics Response (Version: 0) => [topic_errors]
* topic_errors => topic error_code
* topic => STRING
* error_code => INT16
*/
const topicNameComparator = (a, b) => a.topic.localeCompare(b.topic)
const topicErrors = decoder => ({
topic: decoder.readString(),
errorCode: decoder.readInt16(),
})
const decode = async rawData => {
const decoder = new Decoder(rawData)
return {
topicErrors: decoder.readArray(topicErrors).sort(topicNameComparator),
}
}
const parse = async data => {
const topicsWithError = data.topicErrors.filter(({ errorCode }) => failure(errorCode))
if (topicsWithError.length > 0) {
throw new KafkaJSAggregateError(
'Topic creation errors',
topicsWithError.map(
error => new KafkaJSCreateTopicError(createErrorFromCode(error.errorCode), error.topic)
)
)
}
return data
}
module.exports = {
decode,
parse,
}