errors.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package sarama
  2. import "errors"
  3. // OutOfBrokers is the error returned when the client has run out of brokers to talk to because all of them errored
  4. // or otherwise failed to respond.
  5. var OutOfBrokers = errors.New("kafka: Client has run out of available brokers to talk to. Is your cluster reachable?")
  6. // NoSuchTopic is the error returned when the supplied topic is rejected by the Kafka servers.
  7. var NoSuchTopic = errors.New("kafka: Topic not recognized by brokers.")
  8. // IncompleteResponse is the error returned when the server returns a syntactically valid response, but it does
  9. // not contain the expected information.
  10. var IncompleteResponse = errors.New("kafka: Response did not contain all the expected topic/partition blocks.")
  11. // InvalidPartition is the error returned when a partitioner returns an invalid partition index
  12. // (meaning one outside of the range [0...numPartitions-1]).
  13. var InvalidPartition = errors.New("kafka: Partitioner returned an invalid partition index.")
  14. // AlreadyConnected is the error returned when calling Connect() on a Broker that is already connected.
  15. var AlreadyConnected = errors.New("kafka: broker: already connected")
  16. // NotConnected is the error returned when trying to send or call Close() on a Broker that is not connected.
  17. var NotConnected = errors.New("kafka: broker: not connected")
  18. // EncodingError is returned from a failure while encoding a Kafka packet. This can happen, for example,
  19. // if you try to encode a string over 2^15 characters in length, since Kafka's encoding rules do not permit that.
  20. var EncodingError = errors.New("kafka: Error while encoding packet.")
  21. // InsufficientData 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 InsufficientData = errors.New("kafka: Insufficient data to decode packet, more bytes expected.")
  25. // DecodingError is returned when there was an error (other than truncated data) decoding the Kafka broker's response.
  26. // This can be a bad CRC or length field, or any other invalid value.
  27. var DecodingError = errors.New("kafka: Error while decoding packet.")
  28. // MessageTooLarge is returned when the next message to consume is larger than the configured MaxFetchSize
  29. var MessageTooLarge = errors.New("kafka: Message is larger than MaxFetchSize")
  30. // ConfigurationError is the type of error returned from NewClient, NewProducer or NewConsumer when the specified
  31. // configuration is invalid.
  32. type ConfigurationError string
  33. func (err ConfigurationError) Error() string {
  34. return "kafka: Invalid Configuration: " + string(err)
  35. }
  36. // KError is the type of error that can be returned directly by the Kafka broker.
  37. // See https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-ErrorCodes
  38. type KError int16
  39. const (
  40. NO_ERROR KError = 0
  41. UNKNOWN KError = -1
  42. OFFSET_OUT_OF_RANGE KError = 1
  43. INVALID_MESSAGE KError = 2
  44. UNKNOWN_TOPIC_OR_PARTITION KError = 3
  45. INVALID_MESSAGE_SIZE KError = 4
  46. LEADER_NOT_AVAILABLE KError = 5
  47. NOT_LEADER_FOR_PARTITION KError = 6
  48. REQUEST_TIMED_OUT KError = 7
  49. BROKER_NOT_AVAILABLE KError = 8
  50. REPLICA_NOT_AVAILABLE KError = 9
  51. MESSAGE_SIZE_TOO_LARGE KError = 10
  52. STALE_CONTROLLER_EPOCH_CODE KError = 11
  53. OFFSET_METADATA_TOO_LARGE KError = 12
  54. )
  55. func (err KError) Error() string {
  56. // Error messages stolen/adapted from
  57. // https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
  58. switch err {
  59. case NO_ERROR:
  60. return "kafka server: Not an error, why are you printing me?"
  61. case UNKNOWN:
  62. return "kafka server: Unexpected (unknown?) server error."
  63. case OFFSET_OUT_OF_RANGE:
  64. return "kafka server: The requested offset is outside the range of offsets maintained by the server for the given topic/partition."
  65. case INVALID_MESSAGE:
  66. return "kafka server: Message contents does not match its CRC."
  67. case UNKNOWN_TOPIC_OR_PARTITION:
  68. return "kafka server: Request was for a topic or partition that does not exist on this broker."
  69. case INVALID_MESSAGE_SIZE:
  70. return "kafka server: The message has a negative size."
  71. case LEADER_NOT_AVAILABLE:
  72. 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."
  73. case NOT_LEADER_FOR_PARTITION:
  74. 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."
  75. case REQUEST_TIMED_OUT:
  76. return "kafka server: Request exceeded the user-specified time limit in the request."
  77. case BROKER_NOT_AVAILABLE:
  78. return "kafka server: Broker not available. Not a client facing error, we should never receive this!!!"
  79. case REPLICA_NOT_AVAILABLE:
  80. return "kafka server: Replica not available. What is the difference between this and LeaderNotAvailable?"
  81. case MESSAGE_SIZE_TOO_LARGE:
  82. return "kafka server: Message was too large, server rejected it to avoid allocation error."
  83. case STALE_CONTROLLER_EPOCH_CODE:
  84. return "kafka server: Stale controller epoch code. ???"
  85. case OFFSET_METADATA_TOO_LARGE:
  86. return "kafka server: Specified a string larger than the configured maximum for offset metadata."
  87. }
  88. return "Unknown error, how did this happen?"
  89. }