errors.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. var DecodingError = errors.New("kafka: Error while decoding packet.")
  31. // MessageTooLarge is returned when the next message to consume is larger than the configured MaxFetchSize
  32. var MessageTooLarge = errors.New("kafka: Message is larger than MaxFetchSize")
  33. // ConfigurationError is the type of error returned from NewClient, NewProducer or NewConsumer when the specified
  34. // configuration is invalid.
  35. type ConfigurationError string
  36. func (err ConfigurationError) Error() string {
  37. return "kafka: Invalid Configuration: " + string(err)
  38. }
  39. type DroppedMessagesError struct {
  40. ndropped int
  41. err error
  42. }
  43. func (err DroppedMessagesError) Error() string {
  44. if err.err != nil {
  45. return fmt.Sprintf("kafka: Dropped %d messages: %s", err.ndropped, err.Error())
  46. } else {
  47. return fmt.Sprintf("kafka: Dropped %d messages", err.ndropped)
  48. }
  49. }
  50. // KError is the type of error that can be returned directly by the Kafka broker.
  51. // See https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-ErrorCodes
  52. type KError int16
  53. // Numeric error codes returned by the Kafka server.
  54. const (
  55. NoError KError = 0
  56. Unknown KError = -1
  57. OffsetOutOfRange KError = 1
  58. InvalidMessage KError = 2
  59. UnknownTopicOrPartition KError = 3
  60. InvalidMessageSize KError = 4
  61. LeaderNotAvailable KError = 5
  62. NotLeaderForPartition KError = 6
  63. RequestTimedOut KError = 7
  64. BrokerNotAvailable KError = 8
  65. ReplicaNotAvailable KError = 9
  66. MessageSizeTooLarge KError = 10
  67. StaleControllerEpochCode KError = 11
  68. OffsetMetadataTooLarge KError = 12
  69. )
  70. func (err KError) Error() string {
  71. // Error messages stolen/adapted from
  72. // https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
  73. switch err {
  74. case NoError:
  75. return "kafka server: Not an error, why are you printing me?"
  76. case Unknown:
  77. return "kafka server: Unexpected (unknown?) server error."
  78. case OffsetOutOfRange:
  79. return "kafka server: The requested offset is outside the range of offsets maintained by the server for the given topic/partition."
  80. case InvalidMessage:
  81. return "kafka server: Message contents does not match its CRC."
  82. case UnknownTopicOrPartition:
  83. return "kafka server: Request was for a topic or partition that does not exist on this broker."
  84. case InvalidMessageSize:
  85. return "kafka server: The message has a negative size."
  86. case LeaderNotAvailable:
  87. 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."
  88. case NotLeaderForPartition:
  89. 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."
  90. case RequestTimedOut:
  91. return "kafka server: Request exceeded the user-specified time limit in the request."
  92. case BrokerNotAvailable:
  93. return "kafka server: Broker not available. Not a client facing error, we should never receive this!!!"
  94. case ReplicaNotAvailable:
  95. return "kafka server: Replica not available. What is the difference between this and LeaderNotAvailable?"
  96. case MessageSizeTooLarge:
  97. return "kafka server: Message was too large, server rejected it to avoid allocation error."
  98. case StaleControllerEpochCode:
  99. return "kafka server: Stale controller epoch code. ???"
  100. case OffsetMetadataTooLarge:
  101. return "kafka server: Specified a string larger than the configured maximum for offset metadata."
  102. }
  103. return fmt.Sprintf("Unknown error, how did this happen? Error code = %d", err)
  104. }