errors.go 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package kafka
  2. import "errors"
  3. // AlreadyConnected is the error returned when calling Connect() on a Broker that is already connected.
  4. var AlreadyConnected = errors.New("kafka: broker: already connected")
  5. // NotConnected is the error returned when trying to send or call Close() on a Broker that is not connected.
  6. var NotConnected = errors.New("kafka: broker: not connected")
  7. // EncodingError is returned from a failure while encoding a Kafka packet. This can happen, for example,
  8. // if you try to encode a string over 2^15 characters in length, since Kafka's encoding rules do not permit that.
  9. var EncodingError = errors.New("kafka: Error while encoding packet.")
  10. // InsufficientData is returned when decoding and the packet is truncated. This can be expected
  11. // when requesting messages, since as an optimization the server is allowed to return a partial message at the end
  12. // of the message set.
  13. var InsufficientData = errors.New("kafka: Insufficient data to decode packet, more bytes expected.")
  14. // DecodingError is returned when there was an error (other than truncated data) decoding the Kafka broker's response.
  15. // This can be a bad CRC or length field, or any other invalid value.
  16. var DecodingError = errors.New("kafka: Error while decoding packet.")
  17. // KError is the type of error that can be returned directly by the Kafka broker.
  18. // See https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-ErrorCodes
  19. type KError int16
  20. const (
  21. NO_ERROR KError = 0
  22. UNKNOWN KError = -1
  23. OFFSET_OUT_OF_RANGE KError = 1
  24. INVALID_MESSAGE KError = 2
  25. UNKNOWN_TOPIC_OR_PARTITION KError = 3
  26. INVALID_MESSAGE_SIZE KError = 4
  27. LEADER_NOT_AVAILABLE KError = 5
  28. NOT_LEADER_FOR_PARTITION KError = 6
  29. REQUEST_TIMED_OUT KError = 7
  30. BROKER_NOT_AVAILABLE KError = 8
  31. REPLICA_NOT_AVAILABLE KError = 9
  32. MESSAGE_SIZE_TOO_LARGE KError = 10
  33. STALE_CONTROLLER_EPOCH_CODE KError = 11
  34. OFFSET_METADATA_TOO_LARGE KError = 12
  35. )
  36. func (err KError) Error() string {
  37. // Error messages stolen/adapted from
  38. // https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
  39. switch err {
  40. case NO_ERROR:
  41. return "kafka server: Not an error, why are you printing me?"
  42. case UNKNOWN:
  43. return "kafka server: Unexpected (unknown?) server error."
  44. case OFFSET_OUT_OF_RANGE:
  45. return "kafka server: The requested offset is outside the range of offsets maintained by the server for the given topic/partition."
  46. case INVALID_MESSAGE:
  47. return "kafka server: Message contents does not match its CRC."
  48. case UNKNOWN_TOPIC_OR_PARTITION:
  49. return "kafka server: Request was for a topic or partition that does not exist on this broker."
  50. case INVALID_MESSAGE_SIZE:
  51. return "kafka server: The message has a negative size."
  52. case LEADER_NOT_AVAILABLE:
  53. 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."
  54. case NOT_LEADER_FOR_PARTITION:
  55. 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."
  56. case REQUEST_TIMED_OUT:
  57. return "kafka server: Request exceeded the user-specified time limit in the request."
  58. case BROKER_NOT_AVAILABLE:
  59. return "kafka server: Broker not available. Not a client facing error, we should never receive this!!!"
  60. case REPLICA_NOT_AVAILABLE:
  61. return "kafka server: Replica not available. What is the difference between this and LeaderNotAvailable?"
  62. case MESSAGE_SIZE_TOO_LARGE:
  63. return "kafka server: Message was too large, server rejected it to avoid allocation error."
  64. case STALE_CONTROLLER_EPOCH_CODE:
  65. return "kafka server: Stale controller epoch code. ???"
  66. case OFFSET_METADATA_TOO_LARGE:
  67. return "kafka server: Specified a string larger than the configured maximum for offset metadata."
  68. default:
  69. return "Unknown error, how did this happen?"
  70. }
  71. }