errors.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // 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. // ShuttingDown is returned when a producer receives a message during shutdown.
  29. var ShuttingDown = errors.New("kafka: Message received by producer in process of shutting down.")
  30. // DecodingError is returned when there was an error (other than truncated data) decoding the Kafka broker's response.
  31. // This can be a bad CRC or length field, or any other invalid value.
  32. type DecodingError struct {
  33. Info string
  34. }
  35. func (err DecodingError) Error() string {
  36. return fmt.Sprintf("kafka: Error while decoding packet: %s", err.Info)
  37. }
  38. // MessageTooLarge is returned when the next message to consume is larger than the configured MaxFetchSize
  39. var MessageTooLarge = errors.New("kafka: Message is larger than MaxFetchSize")
  40. // ConfigurationError is the type of error returned from NewClient, NewProducer or NewConsumer when the specified
  41. // configuration is invalid.
  42. type ConfigurationError string
  43. func (err ConfigurationError) Error() string {
  44. return "kafka: Invalid Configuration: " + string(err)
  45. }
  46. // KError is the type of error that can be returned directly by the Kafka broker.
  47. // See https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-ErrorCodes
  48. type KError int16
  49. // Numeric error codes returned by the Kafka server.
  50. const (
  51. NoError KError = 0
  52. Unknown KError = -1
  53. OffsetOutOfRange KError = 1
  54. InvalidMessage KError = 2
  55. UnknownTopicOrPartition KError = 3
  56. InvalidMessageSize KError = 4
  57. LeaderNotAvailable KError = 5
  58. NotLeaderForPartition KError = 6
  59. RequestTimedOut KError = 7
  60. BrokerNotAvailable KError = 8
  61. ReplicaNotAvailable KError = 9
  62. MessageSizeTooLarge KError = 10
  63. StaleControllerEpochCode KError = 11
  64. OffsetMetadataTooLarge KError = 12
  65. OffsetsLoadInProgress KError = 14
  66. ConsumerCoordinatorNotAvailable KError = 15
  67. NotCoordinatorForConsumer KError = 16
  68. )
  69. func (err KError) Error() string {
  70. // Error messages stolen/adapted from
  71. // https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
  72. switch err {
  73. case NoError:
  74. return "kafka server: Not an error, why are you printing me?"
  75. case Unknown:
  76. return "kafka server: Unexpected (unknown?) server error."
  77. case OffsetOutOfRange:
  78. return "kafka server: The requested offset is outside the range of offsets maintained by the server for the given topic/partition."
  79. case InvalidMessage:
  80. return "kafka server: Message contents does not match its CRC."
  81. case UnknownTopicOrPartition:
  82. return "kafka server: Request was for a topic or partition that does not exist on this broker."
  83. case InvalidMessageSize:
  84. return "kafka server: The message has a negative size."
  85. case LeaderNotAvailable:
  86. 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."
  87. case NotLeaderForPartition:
  88. 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."
  89. case RequestTimedOut:
  90. return "kafka server: Request exceeded the user-specified time limit in the request."
  91. case BrokerNotAvailable:
  92. return "kafka server: Broker not available. Not a client facing error, we should never receive this!!!"
  93. case ReplicaNotAvailable:
  94. return "kafka server: Replica infomation not available, one or more brokers are down."
  95. case MessageSizeTooLarge:
  96. return "kafka server: Message was too large, server rejected it to avoid allocation error."
  97. case StaleControllerEpochCode:
  98. return "kafka server: StaleControllerEpochCode (internal error code for broker-to-broker communication)."
  99. case OffsetMetadataTooLarge:
  100. return "kafka server: Specified a string larger than the configured maximum for offset metadata."
  101. case OffsetsLoadInProgress:
  102. return "kafka server: The broker is still loading offsets after a leader change for that offset's topic partition."
  103. case ConsumerCoordinatorNotAvailable:
  104. return "kafka server: Offset's topic has not yet been created."
  105. case NotCoordinatorForConsumer:
  106. return "kafka server: Request was for a consumer group that is not coordinated by this broker."
  107. }
  108. return fmt.Sprintf("Unknown error, how did this happen? Error code = %d", err)
  109. }