-
-
Notifications
You must be signed in to change notification settings - Fork 532
/
response.js
37 lines (30 loc) · 881 Bytes
/
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
const Decoder = require('../../../decoder')
const { failure, createErrorFromCode } = require('../../../error')
/**
* 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 createErrorFromCode(topicsWithError[0].errorCode)
}
return data
}
module.exports = {
decode,
parse,
}