errors.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package sarama
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. // OutOfBrokers 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 OutOfBrokers = errors.New("kafka: Client has run out of available brokers to talk to. Is your cluster reachable?")
  9. // ClosedClient is the error returned when a method is called on a client that has been closed.
  10. var ClosedClient = errors.New("kafka: Tried to use a client that was closed.")
  11. // NoSuchTopic is the error returned when the supplied topic is rejected by the Kafka servers.
  12. var NoSuchTopic = errors.New("kafka: Topic not recognized by brokers.")
  13. // IncompleteResponse is the error returned when the server returns a syntactically valid response, but it does
  14. // not contain the expected information.
  15. var IncompleteResponse = errors.New("kafka: Response did not contain all the expected topic/partition blocks.")
  16. // InvalidPartition is the error returned when a partitioner returns an invalid partition index
  17. // (meaning one outside of the range [0...numPartitions-1]).
  18. var InvalidPartition = errors.New("kafka: Partitioner returned an invalid partition index.")
  19. // AlreadyConnected is the error returned when calling Open() on a Broker that is already connected.
  20. var AlreadyConnected = errors.New("kafka: broker: already connected")
  21. // NotConnected is the error returned when trying to send or call Close() on a Broker that is not connected.
  22. var NotConnected = errors.New("kafka: broker: not connected")
  23. // EncodingError is returned from a failure while encoding a Kafka packet. This can happen, for example,
  24. // if you try to encode a string over 2^15 characters in length, since Kafka's encoding rules do not permit that.
  25. var EncodingError = errors.New("kafka: Error while encoding packet.")
  26. // InsufficientData is returned when decoding and the packet is truncated. This can be expected
  27. // when requesting messages, since as an optimization the server is allowed to return a partial message at the end
  28. // of the message set.
  29. var InsufficientData = errors.New("kafka: Insufficient data to decode packet, more bytes expected.")
  30. // ShuttingDown is returned when a producer receives a message during shutdown.
  31. var ShuttingDown = errors.New("kafka: Message received by producer in process of shutting down.")
  32. // DecodingError is returned when there was an error (other than truncated data) decoding the Kafka broker's response.
  33. // This can be a bad CRC or length field, or any other invalid value.
  34. type DecodingError struct {
  35. Info string
  36. }
  37. func (err DecodingError) Error() string {
  38. return fmt.Sprintf("kafka: Error while decoding packet: %s", err.Info)
  39. }
  40. // MessageTooLarge is returned when the next message to consume is larger than the configured MaxFetchSize
  41. var MessageTooLarge = errors.New("kafka: Message is larger than MaxFetchSize")
  42. // ConfigurationError is the type of error returned from NewClient, NewProducer or NewConsumer when the specified
  43. // configuration is invalid.
  44. type ConfigurationError string
  45. func (err ConfigurationError) Error() string {
  46. return "kafka: Invalid Configuration: " + string(err)
  47. }
  48. // DroppedMessagesError is returned from a producer when messages weren't able to be successfully delivered to a broker.
  49. type DroppedMessagesError struct {
  50. DroppedMessages int
  51. Err error
  52. }
  53. func (err DroppedMessagesError) Error() string {
  54. if err.Err != nil {
  55. return fmt.Sprintf("kafka: Dropped %d messages: %s", err.DroppedMessages, err.Err.Error())
  56. } else {
  57. return fmt.Sprintf("kafka: Dropped %d messages", err.DroppedMessages)
  58. }
  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. NoError KError = 0
  66. Unknown KError = -1
  67. OffsetOutOfRange KError = 1
  68. InvalidMessage KError = 2
  69. UnknownTopicOrPartition KError = 3
  70. InvalidMessageSize KError = 4
  71. LeaderNotAvailable KError = 5
  72. NotLeaderForPartition KError = 6
  73. RequestTimedOut KError = 7
  74. BrokerNotAvailable KError = 8
  75. ReplicaNotAvailable KError = 9
  76. MessageSizeTooLarge KError = 10
  77. StaleControllerEpochCode KError = 11
  78. OffsetMetadataTooLarge KError = 12
  79. OffsetsLoadInProgress KError = 14
  80. ConsumerCoordinatorNotAvailable KError = 15
  81. NotCoordinatorForConsumer KError = 16
  82. )
  83. func (err KError) Error() string {
  84. // Error messages stolen/adapted from
  85. // https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
  86. switch err {
  87. case NoError:
  88. return "kafka server: Not an error, why are you printing me?"
  89. case Unknown:
  90. return "kafka server: Unexpected (unknown?) server error."
  91. case OffsetOutOfRange:
  92. return "kafka server: The requested offset is outside the range of offsets maintained by the server for the given topic/partition."
  93. case InvalidMessage:
  94. return "kafka server: Message contents does not match its CRC."
  95. case UnknownTopicOrPartition:
  96. return "kafka server: Request was for a topic or partition that does not exist on this broker."
  97. case InvalidMessageSize:
  98. return "kafka server: The message has a negative size."
  99. case LeaderNotAvailable:
  100. 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."
  101. case NotLeaderForPartition:
  102. 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."
  103. case RequestTimedOut:
  104. return "kafka server: Request exceeded the user-specified time limit in the request."
  105. case BrokerNotAvailable:
  106. return "kafka server: Broker not available. Not a client facing error, we should never receive this!!!"
  107. case ReplicaNotAvailable:
  108. return "kafka server: Replica infomation not available, one or more brokers are down."
  109. case MessageSizeTooLarge:
  110. return "kafka server: Message was too large, server rejected it to avoid allocation error."
  111. case StaleControllerEpochCode:
  112. return "kafka server: StaleControllerEpochCode (internal error code for broker-to-broker communication)."
  113. case OffsetMetadataTooLarge:
  114. return "kafka server: Specified a string larger than the configured maximum for offset metadata."
  115. case OffsetsLoadInProgress:
  116. return "kafka server: The broker is still loading offsets after a leader change for that offset's topic partition."
  117. case ConsumerCoordinatorNotAvailable:
  118. return "kafka server: Offset's topic has not yet been created."
  119. case NotCoordinatorForConsumer:
  120. return "kafka server: Request was for a consumer group that is not coordinated by this broker."
  121. }
  122. return fmt.Sprintf("Unknown error, how did this happen? Error code = %d", err)
  123. }