You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

470 lines
16 KiB

  1. /*
  2. * node-rdkafka - Node.js wrapper for RdKafka C/C++ library
  3. *
  4. * Copyright (c) 2016 Blizzard Entertainment
  5. *
  6. * This software may be modified and distributed under the terms
  7. * of the MIT license. See the LICENSE.txt file for details.
  8. */
  9. module.exports = LibrdKafkaError;
  10. var util = require('util');
  11. var librdkafka = require('../librdkafka');
  12. util.inherits(LibrdKafkaError, Error);
  13. LibrdKafkaError.create = createLibrdkafkaError;
  14. LibrdKafkaError.wrap = errorWrap;
  15. /**
  16. * Enum for identifying errors reported by the library
  17. *
  18. * You can find this list in the C++ code at
  19. * https://github.com/edenhill/librdkafka/blob/master/src-cpp/rdkafkacpp.h#L148
  20. *
  21. * @readonly
  22. * @enum {number}
  23. * @constant
  24. */
  25. // ====== Generated from librdkafka 1.6.1 file src-cpp/rdkafkacpp.h ======
  26. LibrdKafkaError.codes = {
  27. /* Internal errors to rdkafka: */
  28. /** Begin internal error codes */
  29. ERR__BEGIN: -200,
  30. /** Received message is incorrect */
  31. ERR__BAD_MSG: -199,
  32. /** Bad/unknown compression */
  33. ERR__BAD_COMPRESSION: -198,
  34. /** Broker is going away */
  35. ERR__DESTROY: -197,
  36. /** Generic failure */
  37. ERR__FAIL: -196,
  38. /** Broker transport failure */
  39. ERR__TRANSPORT: -195,
  40. /** Critical system resource */
  41. ERR__CRIT_SYS_RESOURCE: -194,
  42. /** Failed to resolve broker */
  43. ERR__RESOLVE: -193,
  44. /** Produced message timed out*/
  45. ERR__MSG_TIMED_OUT: -192,
  46. /** Reached the end of the topic+partition queue on
  47. * the broker. Not really an error.
  48. * This event is disabled by default,
  49. * see the `enable.partition.eof` configuration property. */
  50. ERR__PARTITION_EOF: -191,
  51. /** Permanent: Partition does not exist in cluster. */
  52. ERR__UNKNOWN_PARTITION: -190,
  53. /** File or filesystem error */
  54. ERR__FS: -189,
  55. /** Permanent: Topic does not exist in cluster. */
  56. ERR__UNKNOWN_TOPIC: -188,
  57. /** All broker connections are down. */
  58. ERR__ALL_BROKERS_DOWN: -187,
  59. /** Invalid argument, or invalid configuration */
  60. ERR__INVALID_ARG: -186,
  61. /** Operation timed out */
  62. ERR__TIMED_OUT: -185,
  63. /** Queue is full */
  64. ERR__QUEUE_FULL: -184,
  65. /** ISR count < required.acks */
  66. ERR__ISR_INSUFF: -183,
  67. /** Broker node update */
  68. ERR__NODE_UPDATE: -182,
  69. /** SSL error */
  70. ERR__SSL: -181,
  71. /** Waiting for coordinator to become available. */
  72. ERR__WAIT_COORD: -180,
  73. /** Unknown client group */
  74. ERR__UNKNOWN_GROUP: -179,
  75. /** Operation in progress */
  76. ERR__IN_PROGRESS: -178,
  77. /** Previous operation in progress, wait for it to finish. */
  78. ERR__PREV_IN_PROGRESS: -177,
  79. /** This operation would interfere with an existing subscription */
  80. ERR__EXISTING_SUBSCRIPTION: -176,
  81. /** Assigned partitions (rebalance_cb) */
  82. ERR__ASSIGN_PARTITIONS: -175,
  83. /** Revoked partitions (rebalance_cb) */
  84. ERR__REVOKE_PARTITIONS: -174,
  85. /** Conflicting use */
  86. ERR__CONFLICT: -173,
  87. /** Wrong state */
  88. ERR__STATE: -172,
  89. /** Unknown protocol */
  90. ERR__UNKNOWN_PROTOCOL: -171,
  91. /** Not implemented */
  92. ERR__NOT_IMPLEMENTED: -170,
  93. /** Authentication failure*/
  94. ERR__AUTHENTICATION: -169,
  95. /** No stored offset */
  96. ERR__NO_OFFSET: -168,
  97. /** Outdated */
  98. ERR__OUTDATED: -167,
  99. /** Timed out in queue */
  100. ERR__TIMED_OUT_QUEUE: -166,
  101. /** Feature not supported by broker */
  102. ERR__UNSUPPORTED_FEATURE: -165,
  103. /** Awaiting cache update */
  104. ERR__WAIT_CACHE: -164,
  105. /** Operation interrupted */
  106. ERR__INTR: -163,
  107. /** Key serialization error */
  108. ERR__KEY_SERIALIZATION: -162,
  109. /** Value serialization error */
  110. ERR__VALUE_SERIALIZATION: -161,
  111. /** Key deserialization error */
  112. ERR__KEY_DESERIALIZATION: -160,
  113. /** Value deserialization error */
  114. ERR__VALUE_DESERIALIZATION: -159,
  115. /** Partial response */
  116. ERR__PARTIAL: -158,
  117. /** Modification attempted on read-only object */
  118. ERR__READ_ONLY: -157,
  119. /** No such entry / item not found */
  120. ERR__NOENT: -156,
  121. /** Read underflow */
  122. ERR__UNDERFLOW: -155,
  123. /** Invalid type */
  124. ERR__INVALID_TYPE: -154,
  125. /** Retry operation */
  126. ERR__RETRY: -153,
  127. /** Purged in queue */
  128. ERR__PURGE_QUEUE: -152,
  129. /** Purged in flight */
  130. ERR__PURGE_INFLIGHT: -151,
  131. /** Fatal error: see RdKafka::Handle::fatal_error() */
  132. ERR__FATAL: -150,
  133. /** Inconsistent state */
  134. ERR__INCONSISTENT: -149,
  135. /** Gap-less ordering would not be guaranteed if proceeding */
  136. ERR__GAPLESS_GUARANTEE: -148,
  137. /** Maximum poll interval exceeded */
  138. ERR__MAX_POLL_EXCEEDED: -147,
  139. /** Unknown broker */
  140. ERR__UNKNOWN_BROKER: -146,
  141. /** Functionality not configured */
  142. ERR__NOT_CONFIGURED: -145,
  143. /** Instance has been fenced */
  144. ERR__FENCED: -144,
  145. /** Application generated error */
  146. ERR__APPLICATION: -143,
  147. /** Assignment lost */
  148. ERR__ASSIGNMENT_LOST: -142,
  149. /** No operation performed */
  150. ERR__NOOP: -141,
  151. /** No offset to automatically reset to */
  152. ERR__AUTO_OFFSET_RESET: -140,
  153. /** End internal error codes */
  154. ERR__END: -100,
  155. /* Kafka broker errors: */
  156. /** Unknown broker error */
  157. ERR_UNKNOWN: -1,
  158. /** Success */
  159. ERR_NO_ERROR: 0,
  160. /** Offset out of range */
  161. ERR_OFFSET_OUT_OF_RANGE: 1,
  162. /** Invalid message */
  163. ERR_INVALID_MSG: 2,
  164. /** Unknown topic or partition */
  165. ERR_UNKNOWN_TOPIC_OR_PART: 3,
  166. /** Invalid message size */
  167. ERR_INVALID_MSG_SIZE: 4,
  168. /** Leader not available */
  169. ERR_LEADER_NOT_AVAILABLE: 5,
  170. /** Not leader for partition */
  171. ERR_NOT_LEADER_FOR_PARTITION: 6,
  172. /** Request timed out */
  173. ERR_REQUEST_TIMED_OUT: 7,
  174. /** Broker not available */
  175. ERR_BROKER_NOT_AVAILABLE: 8,
  176. /** Replica not available */
  177. ERR_REPLICA_NOT_AVAILABLE: 9,
  178. /** Message size too large */
  179. ERR_MSG_SIZE_TOO_LARGE: 10,
  180. /** StaleControllerEpochCode */
  181. ERR_STALE_CTRL_EPOCH: 11,
  182. /** Offset metadata string too large */
  183. ERR_OFFSET_METADATA_TOO_LARGE: 12,
  184. /** Broker disconnected before response received */
  185. ERR_NETWORK_EXCEPTION: 13,
  186. /** Coordinator load in progress */
  187. ERR_COORDINATOR_LOAD_IN_PROGRESS: 14,
  188. /** Group coordinator load in progress */
  189. ERR_GROUP_LOAD_IN_PROGRESS: 14,
  190. /** Coordinator not available */
  191. ERR_COORDINATOR_NOT_AVAILABLE: 15,
  192. /** Group coordinator not available */
  193. ERR_GROUP_COORDINATOR_NOT_AVAILABLE: 15,
  194. /** Not coordinator */
  195. ERR_NOT_COORDINATOR: 16,
  196. /** Not coordinator for group */
  197. ERR_NOT_COORDINATOR_FOR_GROUP: 16,
  198. /** Invalid topic */
  199. ERR_TOPIC_EXCEPTION: 17,
  200. /** Message batch larger than configured server segment size */
  201. ERR_RECORD_LIST_TOO_LARGE: 18,
  202. /** Not enough in-sync replicas */
  203. ERR_NOT_ENOUGH_REPLICAS: 19,
  204. /** Message(s) written to insufficient number of in-sync replicas */
  205. ERR_NOT_ENOUGH_REPLICAS_AFTER_APPEND: 20,
  206. /** Invalid required acks value */
  207. ERR_INVALID_REQUIRED_ACKS: 21,
  208. /** Specified group generation id is not valid */
  209. ERR_ILLEGAL_GENERATION: 22,
  210. /** Inconsistent group protocol */
  211. ERR_INCONSISTENT_GROUP_PROTOCOL: 23,
  212. /** Invalid group.id */
  213. ERR_INVALID_GROUP_ID: 24,
  214. /** Unknown member */
  215. ERR_UNKNOWN_MEMBER_ID: 25,
  216. /** Invalid session timeout */
  217. ERR_INVALID_SESSION_TIMEOUT: 26,
  218. /** Group rebalance in progress */
  219. ERR_REBALANCE_IN_PROGRESS: 27,
  220. /** Commit offset data size is not valid */
  221. ERR_INVALID_COMMIT_OFFSET_SIZE: 28,
  222. /** Topic authorization failed */
  223. ERR_TOPIC_AUTHORIZATION_FAILED: 29,
  224. /** Group authorization failed */
  225. ERR_GROUP_AUTHORIZATION_FAILED: 30,
  226. /** Cluster authorization failed */
  227. ERR_CLUSTER_AUTHORIZATION_FAILED: 31,
  228. /** Invalid timestamp */
  229. ERR_INVALID_TIMESTAMP: 32,
  230. /** Unsupported SASL mechanism */
  231. ERR_UNSUPPORTED_SASL_MECHANISM: 33,
  232. /** Illegal SASL state */
  233. ERR_ILLEGAL_SASL_STATE: 34,
  234. /** Unuspported version */
  235. ERR_UNSUPPORTED_VERSION: 35,
  236. /** Topic already exists */
  237. ERR_TOPIC_ALREADY_EXISTS: 36,
  238. /** Invalid number of partitions */
  239. ERR_INVALID_PARTITIONS: 37,
  240. /** Invalid replication factor */
  241. ERR_INVALID_REPLICATION_FACTOR: 38,
  242. /** Invalid replica assignment */
  243. ERR_INVALID_REPLICA_ASSIGNMENT: 39,
  244. /** Invalid config */
  245. ERR_INVALID_CONFIG: 40,
  246. /** Not controller for cluster */
  247. ERR_NOT_CONTROLLER: 41,
  248. /** Invalid request */
  249. ERR_INVALID_REQUEST: 42,
  250. /** Message format on broker does not support request */
  251. ERR_UNSUPPORTED_FOR_MESSAGE_FORMAT: 43,
  252. /** Policy violation */
  253. ERR_POLICY_VIOLATION: 44,
  254. /** Broker received an out of order sequence number */
  255. ERR_OUT_OF_ORDER_SEQUENCE_NUMBER: 45,
  256. /** Broker received a duplicate sequence number */
  257. ERR_DUPLICATE_SEQUENCE_NUMBER: 46,
  258. /** Producer attempted an operation with an old epoch */
  259. ERR_INVALID_PRODUCER_EPOCH: 47,
  260. /** Producer attempted a transactional operation in an invalid state */
  261. ERR_INVALID_TXN_STATE: 48,
  262. /** Producer attempted to use a producer id which is not
  263. * currently assigned to its transactional id */
  264. ERR_INVALID_PRODUCER_ID_MAPPING: 49,
  265. /** Transaction timeout is larger than the maximum
  266. * value allowed by the broker's max.transaction.timeout.ms */
  267. ERR_INVALID_TRANSACTION_TIMEOUT: 50,
  268. /** Producer attempted to update a transaction while another
  269. * concurrent operation on the same transaction was ongoing */
  270. ERR_CONCURRENT_TRANSACTIONS: 51,
  271. /** Indicates that the transaction coordinator sending a
  272. * WriteTxnMarker is no longer the current coordinator for a
  273. * given producer */
  274. ERR_TRANSACTION_COORDINATOR_FENCED: 52,
  275. /** Transactional Id authorization failed */
  276. ERR_TRANSACTIONAL_ID_AUTHORIZATION_FAILED: 53,
  277. /** Security features are disabled */
  278. ERR_SECURITY_DISABLED: 54,
  279. /** Operation not attempted */
  280. ERR_OPERATION_NOT_ATTEMPTED: 55,
  281. /** Disk error when trying to access log file on the disk */
  282. ERR_KAFKA_STORAGE_ERROR: 56,
  283. /** The user-specified log directory is not found in the broker config */
  284. ERR_LOG_DIR_NOT_FOUND: 57,
  285. /** SASL Authentication failed */
  286. ERR_SASL_AUTHENTICATION_FAILED: 58,
  287. /** Unknown Producer Id */
  288. ERR_UNKNOWN_PRODUCER_ID: 59,
  289. /** Partition reassignment is in progress */
  290. ERR_REASSIGNMENT_IN_PROGRESS: 60,
  291. /** Delegation Token feature is not enabled */
  292. ERR_DELEGATION_TOKEN_AUTH_DISABLED: 61,
  293. /** Delegation Token is not found on server */
  294. ERR_DELEGATION_TOKEN_NOT_FOUND: 62,
  295. /** Specified Principal is not valid Owner/Renewer */
  296. ERR_DELEGATION_TOKEN_OWNER_MISMATCH: 63,
  297. /** Delegation Token requests are not allowed on this connection */
  298. ERR_DELEGATION_TOKEN_REQUEST_NOT_ALLOWED: 64,
  299. /** Delegation Token authorization failed */
  300. ERR_DELEGATION_TOKEN_AUTHORIZATION_FAILED: 65,
  301. /** Delegation Token is expired */
  302. ERR_DELEGATION_TOKEN_EXPIRED: 66,
  303. /** Supplied principalType is not supported */
  304. ERR_INVALID_PRINCIPAL_TYPE: 67,
  305. /** The group is not empty */
  306. ERR_NON_EMPTY_GROUP: 68,
  307. /** The group id does not exist */
  308. ERR_GROUP_ID_NOT_FOUND: 69,
  309. /** The fetch session ID was not found */
  310. ERR_FETCH_SESSION_ID_NOT_FOUND: 70,
  311. /** The fetch session epoch is invalid */
  312. ERR_INVALID_FETCH_SESSION_EPOCH: 71,
  313. /** No matching listener */
  314. ERR_LISTENER_NOT_FOUND: 72,
  315. /** Topic deletion is disabled */
  316. ERR_TOPIC_DELETION_DISABLED: 73,
  317. /** Leader epoch is older than broker epoch */
  318. ERR_FENCED_LEADER_EPOCH: 74,
  319. /** Leader epoch is newer than broker epoch */
  320. ERR_UNKNOWN_LEADER_EPOCH: 75,
  321. /** Unsupported compression type */
  322. ERR_UNSUPPORTED_COMPRESSION_TYPE: 76,
  323. /** Broker epoch has changed */
  324. ERR_STALE_BROKER_EPOCH: 77,
  325. /** Leader high watermark is not caught up */
  326. ERR_OFFSET_NOT_AVAILABLE: 78,
  327. /** Group member needs a valid member ID */
  328. ERR_MEMBER_ID_REQUIRED: 79,
  329. /** Preferred leader was not available */
  330. ERR_PREFERRED_LEADER_NOT_AVAILABLE: 80,
  331. /** Consumer group has reached maximum size */
  332. ERR_GROUP_MAX_SIZE_REACHED: 81,
  333. /** Static consumer fenced by other consumer with same
  334. * group.instance.id. */
  335. ERR_FENCED_INSTANCE_ID: 82,
  336. /** Eligible partition leaders are not available */
  337. ERR_ELIGIBLE_LEADERS_NOT_AVAILABLE: 83,
  338. /** Leader election not needed for topic partition */
  339. ERR_ELECTION_NOT_NEEDED: 84,
  340. /** No partition reassignment is in progress */
  341. ERR_NO_REASSIGNMENT_IN_PROGRESS: 85,
  342. /** Deleting offsets of a topic while the consumer group is
  343. * subscribed to it */
  344. ERR_GROUP_SUBSCRIBED_TO_TOPIC: 86,
  345. /** Broker failed to validate record */
  346. ERR_INVALID_RECORD: 87,
  347. /** There are unstable offsets that need to be cleared */
  348. ERR_UNSTABLE_OFFSET_COMMIT: 88,
  349. /** Throttling quota has been exceeded */
  350. ERR_THROTTLING_QUOTA_EXCEEDED: 89,
  351. /** There is a newer producer with the same transactionalId
  352. * which fences the current one */
  353. ERR_PRODUCER_FENCED: 90,
  354. /** Request illegally referred to resource that does not exist */
  355. ERR_RESOURCE_NOT_FOUND: 91,
  356. /** Request illegally referred to the same resource twice */
  357. ERR_DUPLICATE_RESOURCE: 92,
  358. /** Requested credential would not meet criteria for acceptability */
  359. ERR_UNACCEPTABLE_CREDENTIAL: 93,
  360. /** Indicates that the either the sender or recipient of a
  361. * voter-only request is not one of the expected voters */
  362. ERR_INCONSISTENT_VOTER_SET: 94,
  363. /** Invalid update version */
  364. ERR_INVALID_UPDATE_VERSION: 95,
  365. /** Unable to update finalized features due to server error */
  366. ERR_FEATURE_UPDATE_FAILED: 96,
  367. /** Request principal deserialization failed during forwarding */
  368. ERR_PRINCIPAL_DESERIALIZATION_FAILURE: 97
  369. };
  370. /**
  371. * Representation of a librdkafka error
  372. *
  373. * This can be created by giving either another error
  374. * to piggy-back on. In this situation it tries to parse
  375. * the error string to figure out the intent. However, more usually,
  376. * it is constructed by an error object created by a C++ Baton.
  377. *
  378. * @param {object|error} e - An object or error to wrap
  379. * @property {string} message - The error message
  380. * @property {number} code - The error code.
  381. * @property {string} origin - The origin, whether it is local or remote
  382. * @constructor
  383. */
  384. function LibrdKafkaError(e) {
  385. if (!(this instanceof LibrdKafkaError)) {
  386. return new LibrdKafkaError(e);
  387. }
  388. if (typeof e === 'number') {
  389. this.message = librdkafka.err2str(e);
  390. this.code = e;
  391. this.errno = e;
  392. if (e >= LibrdKafkaError.codes.ERR__END) {
  393. this.origin = 'local';
  394. } else {
  395. this.origin = 'kafka';
  396. }
  397. Error.captureStackTrace(this, this.constructor);
  398. } else if (!util.isError(e)) {
  399. // This is the better way
  400. this.message = e.message;
  401. this.code = e.code;
  402. this.errno = e.code;
  403. if (e.code >= LibrdKafkaError.codes.ERR__END) {
  404. this.origin = 'local';
  405. } else {
  406. this.origin = 'kafka';
  407. }
  408. Error.captureStackTrace(this, this.constructor);
  409. } else {
  410. var message = e.message;
  411. var parsedMessage = message.split(': ');
  412. var origin, msg;
  413. if (parsedMessage.length > 1) {
  414. origin = parsedMessage[0].toLowerCase();
  415. msg = parsedMessage[1].toLowerCase();
  416. } else {
  417. origin = 'unknown';
  418. msg = message.toLowerCase();
  419. }
  420. // special cases
  421. if (msg === 'consumer is disconnected' || msg === 'producer is disconnected') {
  422. this.origin = 'local';
  423. this.code = LibrdKafkaError.codes.ERR__STATE;
  424. this.errno = this.code;
  425. this.message = msg;
  426. } else {
  427. this.origin = origin;
  428. this.message = msg;
  429. this.code = typeof e.code === 'number' ? e.code : -1;
  430. this.errno = this.code;
  431. this.stack = e.stack;
  432. }
  433. }
  434. if (e.hasOwnProperty('isFatal')) this.isFatal = e.isFatal;
  435. if (e.hasOwnProperty('isRetriable')) this.isRetriable = e.isRetriable;
  436. if (e.hasOwnProperty('isTxnRequiresAbort')) this.isTxnRequiresAbort = e.isTxnRequiresAbort;
  437. }
  438. function createLibrdkafkaError(e) {
  439. return new LibrdKafkaError(e);
  440. }
  441. function errorWrap(errorCode, intIsError) {
  442. var returnValue = true;
  443. if (intIsError) {
  444. returnValue = errorCode;
  445. errorCode = typeof errorCode === 'number' ? errorCode : 0;
  446. }
  447. if (errorCode !== LibrdKafkaError.codes.ERR_NO_ERROR) {
  448. var e = LibrdKafkaError.create(errorCode);
  449. throw e;
  450. }
  451. return returnValue;
  452. }