errors.go 5.8 KB

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