errors.go 14 KB

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