errors.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // ErrClosedClient is the error returned when a method is called on a client that has been closed.
  10. var ErrClosedClient = errors.New("kafka: tried to use a client that was closed")
  11. // ErrIncompleteResponse is the error returned when the server returns a syntactically valid response, but it does
  12. // not contain the expected information.
  13. var ErrIncompleteResponse = errors.New("kafka: response did not contain all the expected topic/partition blocks")
  14. // ErrInvalidPartition is the error returned when a partitioner returns an invalid partition index
  15. // (meaning one outside of the range [0...numPartitions-1]).
  16. var ErrInvalidPartition = errors.New("kafka: partitioner returned an invalid partition index")
  17. // ErrAlreadyConnected is the error returned when calling Open() on a Broker that is already connected or connecting.
  18. var ErrAlreadyConnected = errors.New("kafka: broker connection already initiated")
  19. // ErrNotConnected is the error returned when trying to send or call Close() on a Broker that is not connected.
  20. var ErrNotConnected = errors.New("kafka: broker not connected")
  21. // ErrInsufficientData is returned when decoding and the packet is truncated. This can be expected
  22. // when requesting messages, since as an optimization the server is allowed to return a partial message at the end
  23. // of the message set.
  24. var ErrInsufficientData = errors.New("kafka: insufficient data to decode packet, more bytes expected")
  25. // ErrShuttingDown is returned when a producer receives a message during shutdown.
  26. var ErrShuttingDown = errors.New("kafka: message received by producer in process of shutting down")
  27. // ErrMessageTooLarge is returned when the next message to consume is larger than the configured Consumer.Fetch.Max
  28. var ErrMessageTooLarge = errors.New("kafka: message is larger than Consumer.Fetch.Max")
  29. // ErrConsumerOffsetNotAdvanced is returned when a partition consumer didn't advance its offset after parsing
  30. // a RecordBatch.
  31. var ErrConsumerOffsetNotAdvanced = errors.New("kafka: consumer offset was not advanced after a RecordBatch")
  32. // ErrControllerNotAvailable is returned when server didn't give correct controller id. May be kafka server's version
  33. // is lower than 0.10.0.0.
  34. var ErrControllerNotAvailable = errors.New("kafka: controller is not available")
  35. // ErrNoTopicsToUpdateMetadata is returned when Meta.Full is set to false but no specific topics were found to update
  36. // the metadata.
  37. var ErrNoTopicsToUpdateMetadata = errors.New("kafka: no specific topics to update metadata")
  38. // PacketEncodingError is returned from a failure while encoding a Kafka packet. This can happen, for example,
  39. // if you try to encode a string over 2^15 characters in length, since Kafka's encoding rules do not permit that.
  40. type PacketEncodingError struct {
  41. Info string
  42. }
  43. func (err PacketEncodingError) Error() string {
  44. return fmt.Sprintf("kafka: error encoding packet: %s", err.Info)
  45. }
  46. // PacketDecodingError is returned when there was an error (other than truncated data) decoding the Kafka broker's response.
  47. // This can be a bad CRC or length field, or any other invalid value.
  48. type PacketDecodingError struct {
  49. Info string
  50. }
  51. func (err PacketDecodingError) Error() string {
  52. return fmt.Sprintf("kafka: error decoding packet: %s", err.Info)
  53. }
  54. // ConfigurationError is the type of error returned from a constructor (e.g. NewClient, or NewConsumer)
  55. // when the specified configuration is invalid.
  56. type ConfigurationError string
  57. func (err ConfigurationError) Error() string {
  58. return "kafka: invalid configuration (" + string(err) + ")"
  59. }
  60. // KError is the type of error that can be returned directly by the Kafka broker.
  61. // See https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-ErrorCodes
  62. type KError int16
  63. // Numeric error codes returned by the Kafka server.
  64. const (
  65. ErrNoError KError = 0
  66. ErrUnknown KError = -1
  67. ErrOffsetOutOfRange KError = 1
  68. ErrInvalidMessage KError = 2
  69. ErrUnknownTopicOrPartition KError = 3
  70. ErrInvalidMessageSize KError = 4
  71. ErrLeaderNotAvailable KError = 5
  72. ErrNotLeaderForPartition KError = 6
  73. ErrRequestTimedOut KError = 7
  74. ErrBrokerNotAvailable KError = 8
  75. ErrReplicaNotAvailable KError = 9
  76. ErrMessageSizeTooLarge KError = 10
  77. ErrStaleControllerEpochCode KError = 11
  78. ErrOffsetMetadataTooLarge KError = 12
  79. ErrNetworkException KError = 13
  80. ErrOffsetsLoadInProgress KError = 14
  81. ErrConsumerCoordinatorNotAvailable KError = 15
  82. ErrNotCoordinatorForConsumer KError = 16
  83. ErrInvalidTopic KError = 17
  84. ErrMessageSetSizeTooLarge KError = 18
  85. ErrNotEnoughReplicas KError = 19
  86. ErrNotEnoughReplicasAfterAppend KError = 20
  87. ErrInvalidRequiredAcks KError = 21
  88. ErrIllegalGeneration KError = 22
  89. ErrInconsistentGroupProtocol KError = 23
  90. ErrInvalidGroupId KError = 24
  91. ErrUnknownMemberId KError = 25
  92. ErrInvalidSessionTimeout KError = 26
  93. ErrRebalanceInProgress KError = 27
  94. ErrInvalidCommitOffsetSize KError = 28
  95. ErrTopicAuthorizationFailed KError = 29
  96. ErrGroupAuthorizationFailed KError = 30
  97. ErrClusterAuthorizationFailed KError = 31
  98. ErrInvalidTimestamp KError = 32
  99. ErrUnsupportedSASLMechanism KError = 33
  100. ErrIllegalSASLState KError = 34
  101. ErrUnsupportedVersion KError = 35
  102. ErrTopicAlreadyExists KError = 36
  103. ErrInvalidPartitions KError = 37
  104. ErrInvalidReplicationFactor KError = 38
  105. ErrInvalidReplicaAssignment KError = 39
  106. ErrInvalidConfig KError = 40
  107. ErrNotController KError = 41
  108. ErrInvalidRequest KError = 42
  109. ErrUnsupportedForMessageFormat KError = 43
  110. ErrPolicyViolation KError = 44
  111. ErrOutOfOrderSequenceNumber KError = 45
  112. ErrDuplicateSequenceNumber KError = 46
  113. ErrInvalidProducerEpoch KError = 47
  114. ErrInvalidTxnState KError = 48
  115. ErrInvalidProducerIDMapping KError = 49
  116. ErrInvalidTransactionTimeout KError = 50
  117. ErrConcurrentTransactions KError = 51
  118. ErrTransactionCoordinatorFenced KError = 52
  119. ErrTransactionalIDAuthorizationFailed KError = 53
  120. ErrSecurityDisabled KError = 54
  121. ErrOperationNotAttempted KError = 55
  122. ErrKafkaStorageError KError = 56
  123. ErrLogDirNotFound KError = 57
  124. ErrSASLAuthenticationFailed KError = 58
  125. ErrUnknownProducerID KError = 59
  126. ErrReassignmentInProgress KError = 60
  127. )
  128. func (err KError) Error() string {
  129. // Error messages stolen/adapted from
  130. // https://kafka.apache.org/protocol#protocol_error_codes
  131. switch err {
  132. case ErrNoError:
  133. return "kafka server: Not an error, why are you printing me?"
  134. case ErrUnknown:
  135. return "kafka server: Unexpected (unknown?) server error."
  136. case ErrOffsetOutOfRange:
  137. return "kafka server: The requested offset is outside the range of offsets maintained by the server for the given topic/partition."
  138. case ErrInvalidMessage:
  139. return "kafka server: Message contents does not match its CRC."
  140. case ErrUnknownTopicOrPartition:
  141. return "kafka server: Request was for a topic or partition that does not exist on this broker."
  142. case ErrInvalidMessageSize:
  143. return "kafka server: The message has a negative size."
  144. case ErrLeaderNotAvailable:
  145. 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."
  146. case ErrNotLeaderForPartition:
  147. 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."
  148. case ErrRequestTimedOut:
  149. return "kafka server: Request exceeded the user-specified time limit in the request."
  150. case ErrBrokerNotAvailable:
  151. return "kafka server: Broker not available. Not a client facing error, we should never receive this!!!"
  152. case ErrReplicaNotAvailable:
  153. return "kafka server: Replica information not available, one or more brokers are down."
  154. case ErrMessageSizeTooLarge:
  155. return "kafka server: Message was too large, server rejected it to avoid allocation error."
  156. case ErrStaleControllerEpochCode:
  157. return "kafka server: StaleControllerEpochCode (internal error code for broker-to-broker communication)."
  158. case ErrOffsetMetadataTooLarge:
  159. return "kafka server: Specified a string larger than the configured maximum for offset metadata."
  160. case ErrNetworkException:
  161. return "kafka server: The server disconnected before a response was received."
  162. case ErrOffsetsLoadInProgress:
  163. return "kafka server: The broker is still loading offsets after a leader change for that offset's topic partition."
  164. case ErrConsumerCoordinatorNotAvailable:
  165. return "kafka server: Offset's topic has not yet been created."
  166. case ErrNotCoordinatorForConsumer:
  167. return "kafka server: Request was for a consumer group that is not coordinated by this broker."
  168. case ErrInvalidTopic:
  169. return "kafka server: The request attempted to perform an operation on an invalid topic."
  170. case ErrMessageSetSizeTooLarge:
  171. return "kafka server: The request included message batch larger than the configured segment size on the server."
  172. case ErrNotEnoughReplicas:
  173. return "kafka server: Messages are rejected since there are fewer in-sync replicas than required."
  174. case ErrNotEnoughReplicasAfterAppend:
  175. return "kafka server: Messages are written to the log, but to fewer in-sync replicas than required."
  176. case ErrInvalidRequiredAcks:
  177. return "kafka server: The number of required acks is invalid (should be either -1, 0, or 1)."
  178. case ErrIllegalGeneration:
  179. return "kafka server: The provided generation id is not the current generation."
  180. case ErrInconsistentGroupProtocol:
  181. return "kafka server: The provider group protocol type is incompatible with the other members."
  182. case ErrInvalidGroupId:
  183. return "kafka server: The provided group id was empty."
  184. case ErrUnknownMemberId:
  185. return "kafka server: The provided member is not known in the current generation."
  186. case ErrInvalidSessionTimeout:
  187. return "kafka server: The provided session timeout is outside the allowed range."
  188. case ErrRebalanceInProgress:
  189. return "kafka server: A rebalance for the group is in progress. Please re-join the group."
  190. case ErrInvalidCommitOffsetSize:
  191. return "kafka server: The provided commit metadata was too large."
  192. case ErrTopicAuthorizationFailed:
  193. return "kafka server: The client is not authorized to access this topic."
  194. case ErrGroupAuthorizationFailed:
  195. return "kafka server: The client is not authorized to access this group."
  196. case ErrClusterAuthorizationFailed:
  197. return "kafka server: The client is not authorized to send this request type."
  198. case ErrInvalidTimestamp:
  199. return "kafka server: The timestamp of the message is out of acceptable range."
  200. case ErrUnsupportedSASLMechanism:
  201. return "kafka server: The broker does not support the requested SASL mechanism."
  202. case ErrIllegalSASLState:
  203. return "kafka server: Request is not valid given the current SASL state."
  204. case ErrUnsupportedVersion:
  205. return "kafka server: The version of API is not supported."
  206. case ErrTopicAlreadyExists:
  207. return "kafka server: Topic with this name already exists."
  208. case ErrInvalidPartitions:
  209. return "kafka server: Number of partitions is invalid."
  210. case ErrInvalidReplicationFactor:
  211. return "kafka server: Replication-factor is invalid."
  212. case ErrInvalidReplicaAssignment:
  213. return "kafka server: Replica assignment is invalid."
  214. case ErrInvalidConfig:
  215. return "kafka server: Configuration is invalid."
  216. case ErrNotController:
  217. return "kafka server: This is not the correct controller for this cluster."
  218. case ErrInvalidRequest:
  219. 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."
  220. case ErrUnsupportedForMessageFormat:
  221. return "kafka server: The requested operation is not supported by the message format version."
  222. case ErrPolicyViolation:
  223. return "kafka server: Request parameters do not satisfy the configured policy."
  224. case ErrOutOfOrderSequenceNumber:
  225. return "kafka server: The broker received an out of order sequence number."
  226. case ErrDuplicateSequenceNumber:
  227. return "kafka server: The broker received a duplicate sequence number."
  228. case ErrInvalidProducerEpoch:
  229. return "kafka server: Producer attempted an operation with an old epoch."
  230. case ErrInvalidTxnState:
  231. return "kafka server: The producer attempted a transactional operation in an invalid state."
  232. case ErrInvalidProducerIDMapping:
  233. return "kafka server: The producer attempted to use a producer id which is not currently assigned to its transactional id."
  234. case ErrInvalidTransactionTimeout:
  235. return "kafka server: The transaction timeout is larger than the maximum value allowed by the broker (as configured by max.transaction.timeout.ms)."
  236. case ErrConcurrentTransactions:
  237. return "kafka server: The producer attempted to update a transaction while another concurrent operation on the same transaction was ongoing."
  238. case ErrTransactionCoordinatorFenced:
  239. return "kafka server: The transaction coordinator sending a WriteTxnMarker is no longer the current coordinator for a given producer."
  240. case ErrTransactionalIDAuthorizationFailed:
  241. return "kafka server: Transactional ID authorization failed."
  242. case ErrSecurityDisabled:
  243. return "kafka server: Security features are disabled."
  244. case ErrOperationNotAttempted:
  245. return "kafka server: The broker did not attempt to execute this operation."
  246. case ErrKafkaStorageError:
  247. return "kafka server: Disk error when trying to access log file on the disk."
  248. case ErrLogDirNotFound:
  249. return "kafka server: The specified log directory is not found in the broker config."
  250. case ErrSASLAuthenticationFailed:
  251. return "kafka server: SASL Authentication failed."
  252. case ErrUnknownProducerID:
  253. return "kafka server: The broker could not locate the producer metadata associated with the Producer ID."
  254. case ErrReassignmentInProgress:
  255. return "kafka server: A partition reassignment is in progress."
  256. }
  257. return fmt.Sprintf("Unknown error, how did this happen? Error code = %d", err)
  258. }