forked from tulios/kafkajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.js
288 lines (256 loc) · 7.33 KB
/
errors.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const pkgJson = require('../package.json')
const { bugs } = pkgJson
class KafkaJSError extends Error {
constructor(e, { retriable = true, cause } = {}) {
super(e, { cause })
Error.captureStackTrace(this, this.constructor)
this.message = e.message || e
this.name = 'KafkaJSError'
this.retriable = retriable
this.helpUrl = e.helpUrl
this.cause = cause
}
}
class KafkaJSNonRetriableError extends KafkaJSError {
constructor(e, { cause } = {}) {
super(e, { retriable: false, cause })
this.name = 'KafkaJSNonRetriableError'
}
}
class KafkaJSProtocolError extends KafkaJSError {
constructor(e, { retriable = e.retriable } = {}) {
super(e, { retriable })
this.type = e.type
this.code = e.code
this.name = 'KafkaJSProtocolError'
}
}
class KafkaJSOffsetOutOfRange extends KafkaJSProtocolError {
constructor(e, { topic, partition }) {
super(e)
this.topic = topic
this.partition = partition
this.name = 'KafkaJSOffsetOutOfRange'
}
}
class KafkaJSMemberIdRequired extends KafkaJSProtocolError {
constructor(e, { memberId }) {
super(e)
this.memberId = memberId
this.name = 'KafkaJSMemberIdRequired'
}
}
class KafkaJSNumberOfRetriesExceeded extends KafkaJSNonRetriableError {
constructor(e, { retryCount, retryTime }) {
super(e, { cause: e })
this.stack = `${this.name}\n Caused by: ${e.stack}`
this.retryCount = retryCount
this.retryTime = retryTime
this.name = 'KafkaJSNumberOfRetriesExceeded'
}
}
class KafkaJSConnectionError extends KafkaJSError {
/**
* @param {string} e
* @param {object} options
* @param {string} [options.broker]
* @param {string} [options.code]
*/
constructor(e, { broker, code } = {}) {
super(e)
this.broker = broker
this.code = code
this.name = 'KafkaJSConnectionError'
}
}
class KafkaJSConnectionClosedError extends KafkaJSConnectionError {
constructor(e, { host, port } = {}) {
super(e, { broker: `${host}:${port}` })
this.host = host
this.port = port
this.name = 'KafkaJSConnectionClosedError'
}
}
class KafkaJSRequestTimeoutError extends KafkaJSError {
constructor(e, { broker, correlationId, createdAt, sentAt, pendingDuration } = {}) {
super(e)
this.broker = broker
this.correlationId = correlationId
this.createdAt = createdAt
this.sentAt = sentAt
this.pendingDuration = pendingDuration
this.name = 'KafkaJSRequestTimeoutError'
}
}
class KafkaJSMetadataNotLoaded extends KafkaJSError {
constructor() {
super(...arguments)
this.name = 'KafkaJSMetadataNotLoaded'
}
}
class KafkaJSTopicMetadataNotLoaded extends KafkaJSMetadataNotLoaded {
constructor(e, { topic } = {}) {
super(e)
this.topic = topic
this.name = 'KafkaJSTopicMetadataNotLoaded'
}
}
class KafkaJSStaleTopicMetadataAssignment extends KafkaJSError {
constructor(e, { topic, unknownPartitions } = {}) {
super(e)
this.topic = topic
this.unknownPartitions = unknownPartitions
this.name = 'KafkaJSStaleTopicMetadataAssignment'
}
}
class KafkaJSDeleteGroupsError extends KafkaJSError {
constructor(e, groups = []) {
super(e)
this.groups = groups
this.name = 'KafkaJSDeleteGroupsError'
}
}
class KafkaJSServerDoesNotSupportApiKey extends KafkaJSNonRetriableError {
constructor(e, { apiKey, apiName } = {}) {
super(e)
this.apiKey = apiKey
this.apiName = apiName
this.name = 'KafkaJSServerDoesNotSupportApiKey'
}
}
class KafkaJSBrokerNotFound extends KafkaJSError {
constructor() {
super(...arguments)
this.name = 'KafkaJSBrokerNotFound'
}
}
class KafkaJSPartialMessageError extends KafkaJSNonRetriableError {
constructor() {
super(...arguments)
this.name = 'KafkaJSPartialMessageError'
}
}
class KafkaJSSASLAuthenticationError extends KafkaJSNonRetriableError {
constructor() {
super(...arguments)
this.name = 'KafkaJSSASLAuthenticationError'
}
}
class KafkaJSGroupCoordinatorNotFound extends KafkaJSNonRetriableError {
constructor() {
super(...arguments)
this.name = 'KafkaJSGroupCoordinatorNotFound'
}
}
class KafkaJSNotImplemented extends KafkaJSNonRetriableError {
constructor() {
super(...arguments)
this.name = 'KafkaJSNotImplemented'
}
}
class KafkaJSTimeout extends KafkaJSNonRetriableError {
constructor() {
super(...arguments)
this.name = 'KafkaJSTimeout'
}
}
class KafkaJSLockTimeout extends KafkaJSTimeout {
constructor() {
super(...arguments)
this.name = 'KafkaJSLockTimeout'
}
}
class KafkaJSUnsupportedMagicByteInMessageSet extends KafkaJSNonRetriableError {
constructor() {
super(...arguments)
this.name = 'KafkaJSUnsupportedMagicByteInMessageSet'
}
}
class KafkaJSDeleteTopicRecordsError extends KafkaJSError {
constructor({ partitions }) {
/*
* This error is retriable if all the errors were retriable
*/
const retriable = partitions
.filter(({ error }) => error != null)
.every(({ error }) => error.retriable === true)
super('Error while deleting records', { retriable })
this.name = 'KafkaJSDeleteTopicRecordsError'
this.partitions = partitions
}
}
const issueUrl = bugs ? bugs.url : null
class KafkaJSInvariantViolation extends KafkaJSNonRetriableError {
constructor(e) {
const message = e.message || e
super(`Invariant violated: ${message}. This is likely a bug and should be reported.`)
this.name = 'KafkaJSInvariantViolation'
if (issueUrl !== null) {
const issueTitle = encodeURIComponent(`Invariant violation: ${message}`)
this.helpUrl = `${issueUrl}/new?assignees=&labels=bug&template=bug_report.md&title=${issueTitle}`
}
}
}
class KafkaJSInvalidVarIntError extends KafkaJSNonRetriableError {
constructor() {
super(...arguments)
this.name = 'KafkaJSNonRetriableError'
}
}
class KafkaJSInvalidLongError extends KafkaJSNonRetriableError {
constructor() {
super(...arguments)
this.name = 'KafkaJSNonRetriableError'
}
}
class KafkaJSCreateTopicError extends KafkaJSProtocolError {
constructor(e, topicName) {
super(e)
this.topic = topicName
this.name = 'KafkaJSCreateTopicError'
}
}
class KafkaJSAggregateError extends Error {
constructor(message, errors) {
super(message)
this.errors = errors
this.name = 'KafkaJSAggregateError'
}
}
class KafkaJSFetcherRebalanceError extends Error {}
const isRebalancing = e =>
e.type === 'REBALANCE_IN_PROGRESS' || e.type === 'NOT_COORDINATOR_FOR_GROUP'
const isKafkaJSError = e => e instanceof KafkaJSError
module.exports = {
KafkaJSError,
KafkaJSNonRetriableError,
KafkaJSPartialMessageError,
KafkaJSBrokerNotFound,
KafkaJSProtocolError,
KafkaJSConnectionError,
KafkaJSConnectionClosedError,
KafkaJSRequestTimeoutError,
KafkaJSSASLAuthenticationError,
KafkaJSNumberOfRetriesExceeded,
KafkaJSOffsetOutOfRange,
KafkaJSMemberIdRequired,
KafkaJSGroupCoordinatorNotFound,
KafkaJSNotImplemented,
KafkaJSMetadataNotLoaded,
KafkaJSTopicMetadataNotLoaded,
KafkaJSStaleTopicMetadataAssignment,
KafkaJSDeleteGroupsError,
KafkaJSTimeout,
KafkaJSLockTimeout,
KafkaJSServerDoesNotSupportApiKey,
KafkaJSUnsupportedMagicByteInMessageSet,
KafkaJSDeleteTopicRecordsError,
KafkaJSInvariantViolation,
KafkaJSInvalidVarIntError,
KafkaJSInvalidLongError,
KafkaJSCreateTopicError,
KafkaJSAggregateError,
KafkaJSFetcherRebalanceError,
isRebalancing,
isKafkaJSError,
}