errors.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. package sarama
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. // ErrOutOfBrokers is the error returned when the client has run out of brokers to talk to because all of them errored
  7. // or otherwise failed to respond.
  8. var ErrOutOfBrokers = errors.New("kafka: client has run out of available brokers to talk to (Is your cluster reachable?)")
  9. // ErrBrokerNotFound is the error returned when there's no broker found for the requested ID.
  10. var ErrBrokerNotFound = errors.New("kafka: broker for ID is not found")
  11. // ErrClosedClient is the error returned when a method is called on a client that has been closed.
  12. var ErrClosedClient = errors.New("kafka: tried to use a client that was closed")
  13. // ErrIncompleteResponse is the error returned when the server returns a syntactically valid response, but it does
  14. // not contain the expected information.
  15. var ErrIncompleteResponse = errors.New("kafka: response did not contain all the expected topic/partition blocks")
  16. // ErrInvalidPartition is the error returned when a partitioner returns an invalid partition index
  17. // (meaning one outside of the range [0...numPartitions-1]).
  18. var ErrInvalidPartition = errors.New("kafka: partitioner returned an invalid partition index")
  19. // ErrAlreadyConnected is the error returned when calling Open() on a Broker that is already connected or connecting.
  20. var ErrAlreadyConnected = errors.New("kafka: broker connection already initiated")
  21. // ErrNotConnected is the error returned when trying to send or call Close() on a Broker that is not connected.
  22. var ErrNotConnected = errors.New("kafka: broker not connected")
  23. // ErrInsufficientData is returned when decoding and the packet is truncated. This can be expected
  24. // when requesting messages, since as an optimization the server is allowed to return a partial message at the end
  25. // of the message set.
  26. var ErrInsufficientData = errors.New("kafka: insufficient data to decode packet, more bytes expected")
  27. // ErrShuttingDown is returned when a producer receives a message during shutdown.
  28. var ErrShuttingDown = errors.New("kafka: message received by producer in process of shutting down")
  29. // ErrMessageTooLarge is returned when the next message to consume is larger than the configured Consumer.Fetch.Max
  30. var ErrMessageTooLarge = errors.New("kafka: message is larger than Consumer.Fetch.Max")
  31. // ErrConsumerOffsetNotAdvanced is returned when a partition consumer didn't advance its offset after parsing
  32. // a RecordBatch.
  33. var ErrConsumerOffsetNotAdvanced = errors.New("kafka: consumer offset was not advanced after a RecordBatch")
  34. // ErrControllerNotAvailable is returned when server didn't give correct controller id. May be kafka server's version
  35. // is lower than 0.10.0.0.
  36. var ErrControllerNotAvailable = errors.New("kafka: controller is not available")
  37. // ErrNoTopicsToUpdateMetadata is returned when Meta.Full is set to false but no specific topics were found to update
  38. // the metadata.
  39. var ErrNoTopicsToUpdateMetadata = errors.New("kafka: no specific topics to update metadata")
  40. // ErrUnknownScramMechanism is returned when user tries to AlterUserScramCredentials with unknown SCRAM mechanism
  41. var ErrUnknownScramMechanism = errors.New("kafka: unknown SCRAM mechanism provided")
  42. // PacketEncodingError is returned from a failure while encoding a Kafka packet. This can happen, for example,
  43. // if you try to encode a string over 2^15 characters in length, since Kafka's encoding rules do not permit that.
  44. type PacketEncodingError struct {
  45. Info string
  46. }
  47. func (err PacketEncodingError) Error() string {
  48. return fmt.Sprintf("kafka: error encoding packet: %s", err.Info)
  49. }
  50. // PacketDecodingError is returned when there was an error (other than truncated data) decoding the Kafka broker's response.
  51. // This can be a bad CRC or length field, or any other invalid value.
  52. type PacketDecodingError struct {
  53. Info string
  54. }
  55. func (err PacketDecodingError) Error() string {
  56. return fmt.Sprintf("kafka: error decoding packet: %s", err.Info)
  57. }
  58. // ConfigurationError is the type of error returned from a constructor (e.g. NewClient, or NewConsumer)
  59. // when the specified configuration is invalid.
  60. type ConfigurationError string
  61. func (err ConfigurationError) Error() string {
  62. return "kafka: invalid configuration (" + string(err) + ")"
  63. }
  64. // KError is the type of error that can be returned directly by the Kafka broker.
  65. // See https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-ErrorCodes
  66. type KError int16
  67. // MultiError is used to contain multi error.
  68. type MultiError struct {
  69. Errors *[]error
  70. }
  71. func (mErr MultiError) Error() string {
  72. errString := ""
  73. for _, err := range *mErr.Errors {
  74. errString += err.Error() + ","
  75. }
  76. return errString
  77. }
  78. func (mErr MultiError) PrettyError() string {
  79. errString := ""
  80. for _, err := range *mErr.Errors {
  81. errString += err.Error() + "\n"
  82. }
  83. return errString
  84. }
  85. // ErrDeleteRecords is the type of error returned when fail to delete the required records
  86. type ErrDeleteRecords struct {
  87. MultiError
  88. }
  89. func (err ErrDeleteRecords) Error() string {
  90. return "kafka server: failed to delete records " + err.MultiError.Error()
  91. }
  92. type ErrReassignPartitions struct {
  93. MultiError
  94. }
  95. func (err ErrReassignPartitions) Error() string {
  96. return fmt.Sprintf("failed to reassign partitions for topic: \n%s", err.MultiError.PrettyError())
  97. }
  98. // Numeric error codes returned by the Kafka server.
  99. const (
  100. ErrNoError KError = 0
  101. ErrUnknown KError = -1
  102. ErrOffsetOutOfRange KError = 1
  103. ErrInvalidMessage KError = 2
  104. ErrUnknownTopicOrPartition KError = 3
  105. ErrInvalidMessageSize KError = 4
  106. ErrLeaderNotAvailable KError = 5
  107. ErrNotLeaderForPartition KError = 6
  108. ErrRequestTimedOut KError = 7
  109. ErrBrokerNotAvailable KError = 8
  110. ErrReplicaNotAvailable KError = 9
  111. ErrMessageSizeTooLarge KError = 10
  112. ErrStaleControllerEpochCode KError = 11
  113. ErrOffsetMetadataTooLarge KError = 12
  114. ErrNetworkException KError = 13
  115. ErrOffsetsLoadInProgress KError = 14
  116. ErrConsumerCoordinatorNotAvailable KError = 15
  117. ErrNotCoordinatorForConsumer KError = 16
  118. ErrInvalidTopic KError = 17
  119. ErrMessageSetSizeTooLarge KError = 18
  120. ErrNotEnoughReplicas KError = 19
  121. ErrNotEnoughReplicasAfterAppend KError = 20
  122. ErrInvalidRequiredAcks KError = 21
  123. ErrIllegalGeneration KError = 22
  124. ErrInconsistentGroupProtocol KError = 23
  125. ErrInvalidGroupId KError = 24
  126. ErrUnknownMemberId KError = 25
  127. ErrInvalidSessionTimeout KError = 26
  128. ErrRebalanceInProgress KError = 27
  129. ErrInvalidCommitOffsetSize KError = 28
  130. ErrTopicAuthorizationFailed KError = 29
  131. ErrGroupAuthorizationFailed KError = 30
  132. ErrClusterAuthorizationFailed KError = 31
  133. ErrInvalidTimestamp KError = 32
  134. ErrUnsupportedSASLMechanism KError = 33
  135. ErrIllegalSASLState KError = 34
  136. ErrUnsupportedVersion KError = 35
  137. ErrTopicAlreadyExists KError = 36
  138. ErrInvalidPartitions KError = 37
  139. ErrInvalidReplicationFactor KError = 38
  140. ErrInvalidReplicaAssignment KError = 39
  141. ErrInvalidConfig KError = 40
  142. ErrNotController KError = 41
  143. ErrInvalidRequest KError = 42
  144. ErrUnsupportedForMessageFormat KError = 43
  145. ErrPolicyViolation KError = 44
  146. ErrOutOfOrderSequenceNumber KError = 45
  147. ErrDuplicateSequenceNumber KError = 46
  148. ErrInvalidProducerEpoch KError = 47
  149. ErrInvalidTxnState KError = 48
  150. ErrInvalidProducerIDMapping KError = 49
  151. ErrInvalidTransactionTimeout KError = 50
  152. ErrConcurrentTransactions KError = 51
  153. ErrTransactionCoordinatorFenced KError = 52
  154. ErrTransactionalIDAuthorizationFailed KError = 53
  155. ErrSecurityDisabled KError = 54
  156. ErrOperationNotAttempted KError = 55
  157. ErrKafkaStorageError KError = 56
  158. ErrLogDirNotFound KError = 57
  159. ErrSASLAuthenticationFailed KError = 58
  160. ErrUnknownProducerID KError = 59
  161. ErrReassignmentInProgress KError = 60
  162. ErrDelegationTokenAuthDisabled KError = 61
  163. ErrDelegationTokenNotFound KError = 62
  164. ErrDelegationTokenOwnerMismatch KError = 63
  165. ErrDelegationTokenRequestNotAllowed KError = 64
  166. ErrDelegationTokenAuthorizationFailed KError = 65
  167. ErrDelegationTokenExpired KError = 66
  168. ErrInvalidPrincipalType KError = 67
  169. ErrNonEmptyGroup KError = 68
  170. ErrGroupIDNotFound KError = 69
  171. ErrFetchSessionIDNotFound KError = 70
  172. ErrInvalidFetchSessionEpoch KError = 71
  173. ErrListenerNotFound KError = 72
  174. ErrTopicDeletionDisabled KError = 73
  175. ErrFencedLeaderEpoch KError = 74
  176. ErrUnknownLeaderEpoch KError = 75
  177. ErrUnsupportedCompressionType KError = 76
  178. ErrStaleBrokerEpoch KError = 77
  179. ErrOffsetNotAvailable KError = 78
  180. ErrMemberIdRequired KError = 79
  181. ErrPreferredLeaderNotAvailable KError = 80
  182. ErrGroupMaxSizeReached KError = 81
  183. ErrFencedInstancedId KError = 82
  184. ErrEligibleLeadersNotAvailable KError = 83
  185. ErrElectionNotNeeded KError = 84
  186. ErrNoReassignmentInProgress KError = 85
  187. ErrGroupSubscribedToTopic KError = 86
  188. ErrInvalidRecord KError = 87
  189. ErrUnstableOffsetCommit KError = 88
  190. )
  191. func (err KError) Error() string {
  192. // Error messages stolen/adapted from
  193. // https://kafka.apache.org/protocol#protocol_error_codes
  194. switch err {
  195. case ErrNoError:
  196. return "kafka server: Not an error, why are you printing me?"
  197. case ErrUnknown:
  198. return "kafka server: Unexpected (unknown?) server error."
  199. case ErrOffsetOutOfRange:
  200. return "kafka server: The requested offset is outside the range of offsets maintained by the server for the given topic/partition."
  201. case ErrInvalidMessage:
  202. return "kafka server: Message contents does not match its CRC."
  203. case ErrUnknownTopicOrPartition:
  204. return "kafka server: Request was for a topic or partition that does not exist on this broker."
  205. case ErrInvalidMessageSize:
  206. return "kafka server: The message has a negative size."
  207. case ErrLeaderNotAvailable:
  208. return "kafka server: In the middle of a leadership election, there is currently no leader for this partition and hence it is unavailable for writes."
  209. case ErrNotLeaderForPartition:
  210. return "kafka server: Tried to send a message to a replica that is not the leader for some partition. Your metadata is out of date."
  211. case ErrRequestTimedOut:
  212. return "kafka server: Request exceeded the user-specified time limit in the request."
  213. case ErrBrokerNotAvailable:
  214. return "kafka server: Broker not available. Not a client facing error, we should never receive this!!!"
  215. case ErrReplicaNotAvailable:
  216. return "kafka server: Replica information not available, one or more brokers are down."
  217. case ErrMessageSizeTooLarge:
  218. return "kafka server: Message was too large, server rejected it to avoid allocation error."
  219. case ErrStaleControllerEpochCode:
  220. return "kafka server: StaleControllerEpochCode (internal error code for broker-to-broker communication)."
  221. case ErrOffsetMetadataTooLarge:
  222. return "kafka server: Specified a string larger than the configured maximum for offset metadata."
  223. case ErrNetworkException:
  224. return "kafka server: The server disconnected before a response was received."
  225. case ErrOffsetsLoadInProgress:
  226. return "kafka server: The broker is still loading offsets after a leader change for that offset's topic partition."
  227. case ErrConsumerCoordinatorNotAvailable:
  228. return "kafka server: Offset's topic has not yet been created."
  229. case ErrNotCoordinatorForConsumer:
  230. return "kafka server: Request was for a consumer group that is not coordinated by this broker."
  231. case ErrInvalidTopic:
  232. return "kafka server: The request attempted to perform an operation on an invalid topic."
  233. case ErrMessageSetSizeTooLarge:
  234. return "kafka server: The request included message batch larger than the configured segment size on the server."
  235. case ErrNotEnoughReplicas:
  236. return "kafka server: Messages are rejected since there are fewer in-sync replicas than required."
  237. case ErrNotEnoughReplicasAfterAppend:
  238. return "kafka server: Messages are written to the log, but to fewer in-sync replicas than required."
  239. case ErrInvalidRequiredAcks:
  240. return "kafka server: The number of required acks is invalid (should be either -1, 0, or 1)."
  241. case ErrIllegalGeneration:
  242. return "kafka server: The provided generation id is not the current generation."
  243. case ErrInconsistentGroupProtocol:
  244. return "kafka server: The provider group protocol type is incompatible with the other members."
  245. case ErrInvalidGroupId:
  246. return "kafka server: The provided group id was empty."
  247. case ErrUnknownMemberId:
  248. return "kafka server: The provided member is not known in the current generation."
  249. case ErrInvalidSessionTimeout:
  250. return "kafka server: The provided session timeout is outside the allowed range."
  251. case ErrRebalanceInProgress:
  252. return "kafka server: A rebalance for the group is in progress. Please re-join the group."
  253. case ErrInvalidCommitOffsetSize:
  254. return "kafka server: The provided commit metadata was too large."
  255. case ErrTopicAuthorizationFailed:
  256. return "kafka server: The client is not authorized to access this topic."
  257. case ErrGroupAuthorizationFailed:
  258. return "kafka server: The client is not authorized to access this group."
  259. case ErrClusterAuthorizationFailed:
  260. return "kafka server: The client is not authorized to send this request type."
  261. case ErrInvalidTimestamp:
  262. return "kafka server: The timestamp of the message is out of acceptable range."
  263. case ErrUnsupportedSASLMechanism:
  264. return "kafka server: The broker does not support the requested SASL mechanism."
  265. case ErrIllegalSASLState:
  266. return "kafka server: Request is not valid given the current SASL state."
  267. case ErrUnsupportedVersion:
  268. return "kafka server: The version of API is not supported."
  269. case ErrTopicAlreadyExists:
  270. return "kafka server: Topic with this name already exists."
  271. case ErrInvalidPartitions:
  272. return "kafka server: Number of partitions is invalid."
  273. case ErrInvalidReplicationFactor:
  274. return "kafka server: Replication-factor is invalid."
  275. case ErrInvalidReplicaAssignment:
  276. return "kafka server: Replica assignment is invalid."
  277. case ErrInvalidConfig:
  278. return "kafka server: Configuration is invalid."
  279. case ErrNotController:
  280. return "kafka server: This is not the correct controller for this cluster."
  281. case ErrInvalidRequest:
  282. return "kafka server: This most likely occurs because of a request being malformed by the client library or the message was sent to an incompatible broker. See the broker logs for more details."
  283. case ErrUnsupportedForMessageFormat:
  284. return "kafka server: The requested operation is not supported by the message format version."
  285. case ErrPolicyViolation:
  286. return "kafka server: Request parameters do not satisfy the configured policy."
  287. case ErrOutOfOrderSequenceNumber:
  288. return "kafka server: The broker received an out of order sequence number."
  289. case ErrDuplicateSequenceNumber:
  290. return "kafka server: The broker received a duplicate sequence number."
  291. case ErrInvalidProducerEpoch:
  292. return "kafka server: Producer attempted an operation with an old epoch."
  293. case ErrInvalidTxnState:
  294. return "kafka server: The producer attempted a transactional operation in an invalid state."
  295. case ErrInvalidProducerIDMapping:
  296. return "kafka server: The producer attempted to use a producer id which is not currently assigned to its transactional id."
  297. case ErrInvalidTransactionTimeout:
  298. return "kafka server: The transaction timeout is larger than the maximum value allowed by the broker (as configured by max.transaction.timeout.ms)."
  299. case ErrConcurrentTransactions:
  300. return "kafka server: The producer attempted to update a transaction while another concurrent operation on the same transaction was ongoing."
  301. case ErrTransactionCoordinatorFenced:
  302. return "kafka server: The transaction coordinator sending a WriteTxnMarker is no longer the current coordinator for a given producer."
  303. case ErrTransactionalIDAuthorizationFailed:
  304. return "kafka server: Transactional ID authorization failed."
  305. case ErrSecurityDisabled:
  306. return "kafka server: Security features are disabled."
  307. case ErrOperationNotAttempted:
  308. return "kafka server: The broker did not attempt to execute this operation."
  309. case ErrKafkaStorageError:
  310. return "kafka server: Disk error when trying to access log file on the disk."
  311. case ErrLogDirNotFound:
  312. return "kafka server: The specified log directory is not found in the broker config."
  313. case ErrSASLAuthenticationFailed:
  314. return "kafka server: SASL Authentication failed."
  315. case ErrUnknownProducerID:
  316. return "kafka server: The broker could not locate the producer metadata associated with the Producer ID."
  317. case ErrReassignmentInProgress:
  318. return "kafka server: A partition reassignment is in progress."
  319. case ErrDelegationTokenAuthDisabled:
  320. return "kafka server: Delegation Token feature is not enabled."
  321. case ErrDelegationTokenNotFound:
  322. return "kafka server: Delegation Token is not found on server."
  323. case ErrDelegationTokenOwnerMismatch:
  324. return "kafka server: Specified Principal is not valid Owner/Renewer."
  325. case ErrDelegationTokenRequestNotAllowed:
  326. return "kafka server: Delegation Token requests are not allowed on PLAINTEXT/1-way SSL channels and on delegation token authenticated channels."
  327. case ErrDelegationTokenAuthorizationFailed:
  328. return "kafka server: Delegation Token authorization failed."
  329. case ErrDelegationTokenExpired:
  330. return "kafka server: Delegation Token is expired."
  331. case ErrInvalidPrincipalType:
  332. return "kafka server: Supplied principalType is not supported."
  333. case ErrNonEmptyGroup:
  334. return "kafka server: The group is not empty."
  335. case ErrGroupIDNotFound:
  336. return "kafka server: The group id does not exist."
  337. case ErrFetchSessionIDNotFound:
  338. return "kafka server: The fetch session ID was not found."
  339. case ErrInvalidFetchSessionEpoch:
  340. return "kafka server: The fetch session epoch is invalid."
  341. case ErrListenerNotFound:
  342. return "kafka server: There is no listener on the leader broker that matches the listener on which metadata request was processed."
  343. case ErrTopicDeletionDisabled:
  344. return "kafka server: Topic deletion is disabled."
  345. case ErrFencedLeaderEpoch:
  346. return "kafka server: The leader epoch in the request is older than the epoch on the broker."
  347. case ErrUnknownLeaderEpoch:
  348. return "kafka server: The leader epoch in the request is newer than the epoch on the broker."
  349. case ErrUnsupportedCompressionType:
  350. return "kafka server: The requesting client does not support the compression type of given partition."
  351. case ErrStaleBrokerEpoch:
  352. return "kafka server: Broker epoch has changed"
  353. case ErrOffsetNotAvailable:
  354. return "kafka server: The leader high watermark has not caught up from a recent leader election so the offsets cannot be guaranteed to be monotonically increasing"
  355. case ErrMemberIdRequired:
  356. return "kafka server: The group member needs to have a valid member id before actually entering a consumer group"
  357. case ErrPreferredLeaderNotAvailable:
  358. return "kafka server: The preferred leader was not available"
  359. case ErrGroupMaxSizeReached:
  360. return "kafka server: Consumer group The consumer group has reached its max size. already has the configured maximum number of members."
  361. case ErrFencedInstancedId:
  362. return "kafka server: The broker rejected this static consumer since another consumer with the same group.instance.id has registered with a different member.id."
  363. case ErrEligibleLeadersNotAvailable:
  364. return "kafka server: Eligible topic partition leaders are not available."
  365. case ErrElectionNotNeeded:
  366. return "kafka server: Leader election not needed for topic partition."
  367. case ErrNoReassignmentInProgress:
  368. return "kafka server: No partition reassignment is in progress."
  369. case ErrGroupSubscribedToTopic:
  370. return "kafka server: Deleting offsets of a topic is forbidden while the consumer group is actively subscribed to it."
  371. case ErrInvalidRecord:
  372. return "kafka server: This record has failed the validation on broker and hence will be rejected."
  373. case ErrUnstableOffsetCommit:
  374. return "kafka server: There are unstable offsets that need to be cleared."
  375. }
  376. return fmt.Sprintf("Unknown error, how did this happen? Error code = %d", err)
  377. }