errors.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. var ErrInvalidArrayLength = PacketDecodingError{"invalid array length"}
  30. var ErrInvalidByteSliceLength = PacketDecodingError{"invalid byteslice length"}
  31. var ErrInvalidStringLength = PacketDecodingError{"invalid string length"}
  32. var ErrInvalidSubsetSize = PacketDecodingError{"invalid subset size"}
  33. // PacketEncodingError is returned from a failure while encoding a Kafka packet. This can happen, for example,
  34. // if you try to encode a string over 2^15 characters in length, since Kafka's encoding rules do not permit that.
  35. type PacketEncodingError struct {
  36. Info string
  37. }
  38. func (err PacketEncodingError) Error() string {
  39. return fmt.Sprintf("kafka: error encoding packet: %s", err.Info)
  40. }
  41. // PacketDecodingError is returned when there was an error (other than truncated data) decoding the Kafka broker's response.
  42. // This can be a bad CRC or length field, or any other invalid value.
  43. type PacketDecodingError struct {
  44. Info string
  45. }
  46. func (err PacketDecodingError) Error() string {
  47. return fmt.Sprintf("kafka: error decoding packet: %s", err.Info)
  48. }
  49. // ConfigurationError is the type of error returned from a constructor (e.g. NewClient, or NewConsumer)
  50. // when the specified configuration is invalid.
  51. type ConfigurationError string
  52. func (err ConfigurationError) Error() string {
  53. return "kafka: invalid configuration (" + string(err) + ")"
  54. }
  55. // KError is the type of error that can be returned directly by the Kafka broker.
  56. // See https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-ErrorCodes
  57. type KError int16
  58. // Numeric error codes returned by the Kafka server.
  59. const (
  60. ErrNoError KError = 0
  61. ErrUnknown KError = -1
  62. ErrOffsetOutOfRange KError = 1
  63. ErrInvalidMessage KError = 2
  64. ErrUnknownTopicOrPartition KError = 3
  65. ErrInvalidMessageSize KError = 4
  66. ErrLeaderNotAvailable KError = 5
  67. ErrNotLeaderForPartition KError = 6
  68. ErrRequestTimedOut KError = 7
  69. ErrBrokerNotAvailable KError = 8
  70. ErrReplicaNotAvailable KError = 9
  71. ErrMessageSizeTooLarge KError = 10
  72. ErrStaleControllerEpochCode KError = 11
  73. ErrOffsetMetadataTooLarge KError = 12
  74. ErrOffsetsLoadInProgress KError = 14
  75. ErrConsumerCoordinatorNotAvailable KError = 15
  76. ErrNotCoordinatorForConsumer KError = 16
  77. ErrInvalidTopic KError = 17
  78. ErrMessageSetSizeTooLarge KError = 18
  79. ErrNotEnoughReplicas KError = 19
  80. ErrNotEnoughReplicasAfterAppend KError = 20
  81. ErrInvalidRequiredAcks KError = 21
  82. ErrIllegalGeneration KError = 22
  83. ErrInconsistentGroupProtocol KError = 23
  84. ErrInvalidGroupId KError = 24
  85. ErrUnknownMemberId KError = 25
  86. ErrInvalidSessionTimeout KError = 26
  87. ErrRebalanceInProgress KError = 27
  88. ErrInvalidCommitOffsetSize KError = 28
  89. ErrTopicAuthorizationFailed KError = 29
  90. ErrGroupAuthorizationFailed KError = 30
  91. ErrClusterAuthorizationFailed KError = 31
  92. )
  93. func (err KError) Error() string {
  94. // Error messages stolen/adapted from
  95. // https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
  96. switch err {
  97. case ErrNoError:
  98. return "kafka server: Not an error, why are you printing me?"
  99. case ErrUnknown:
  100. return "kafka server: Unexpected (unknown?) server error."
  101. case ErrOffsetOutOfRange:
  102. return "kafka server: The requested offset is outside the range of offsets maintained by the server for the given topic/partition."
  103. case ErrInvalidMessage:
  104. return "kafka server: Message contents does not match its CRC."
  105. case ErrUnknownTopicOrPartition:
  106. return "kafka server: Request was for a topic or partition that does not exist on this broker."
  107. case ErrInvalidMessageSize:
  108. return "kafka server: The message has a negative size."
  109. case ErrLeaderNotAvailable:
  110. 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."
  111. case ErrNotLeaderForPartition:
  112. 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."
  113. case ErrRequestTimedOut:
  114. return "kafka server: Request exceeded the user-specified time limit in the request."
  115. case ErrBrokerNotAvailable:
  116. return "kafka server: Broker not available. Not a client facing error, we should never receive this!!!"
  117. case ErrReplicaNotAvailable:
  118. return "kafka server: Replica infomation not available, one or more brokers are down."
  119. case ErrMessageSizeTooLarge:
  120. return "kafka server: Message was too large, server rejected it to avoid allocation error."
  121. case ErrStaleControllerEpochCode:
  122. return "kafka server: StaleControllerEpochCode (internal error code for broker-to-broker communication)."
  123. case ErrOffsetMetadataTooLarge:
  124. return "kafka server: Specified a string larger than the configured maximum for offset metadata."
  125. case ErrOffsetsLoadInProgress:
  126. return "kafka server: The broker is still loading offsets after a leader change for that offset's topic partition."
  127. case ErrConsumerCoordinatorNotAvailable:
  128. return "kafka server: Offset's topic has not yet been created."
  129. case ErrNotCoordinatorForConsumer:
  130. return "kafka server: Request was for a consumer group that is not coordinated by this broker."
  131. case ErrInvalidTopic:
  132. return "kafka server: The request attempted to perform an operation on an invalid topic."
  133. case ErrMessageSetSizeTooLarge:
  134. return "kafka server: The request included message batch larger than the configured segment size on the server."
  135. case ErrNotEnoughReplicas:
  136. return "kafka server: Messages are rejected since there are fewer in-sync replicas than required."
  137. case ErrNotEnoughReplicasAfterAppend:
  138. return "kafka server: Messages are written to the log, but to fewer in-sync replicas than required."
  139. case ErrInvalidRequiredAcks:
  140. return "kafka server: The number of required acks is invalid (should be either -1, 0, or 1)."
  141. case ErrIllegalGeneration:
  142. return "kafka server: The provided generation id is not the current generation."
  143. case ErrInconsistentGroupProtocol:
  144. return "kafka server: The provider group protocol type is incompatible with the other members."
  145. case ErrInvalidGroupId:
  146. return "kafka server: The provided group id was empty."
  147. case ErrUnknownMemberId:
  148. return "kafka server: The provided member is not known in the current generation."
  149. case ErrInvalidSessionTimeout:
  150. return "kafka server: The provided session timeout is outside the allowed range."
  151. case ErrRebalanceInProgress:
  152. return "kafka server: A rebalance for the group is in progress. Please re-join the group."
  153. case ErrInvalidCommitOffsetSize:
  154. return "kafka server: The provided commit metadata was too large."
  155. case ErrTopicAuthorizationFailed:
  156. return "kafka server: The client is not authorized to access this topic."
  157. case ErrGroupAuthorizationFailed:
  158. return "kafka server: The client is not authorized to access this group."
  159. case ErrClusterAuthorizationFailed:
  160. return "kafka server: The client is not authorized to send this request type."
  161. }
  162. return fmt.Sprintf("Unknown error, how did this happen? Error code = %d", err)
  163. }