github.com/QuangHoangHao/kafka-go@v0.4.36/protocol/error.go (about)

     1  package protocol
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // Error represents client-side protocol errors.
     8  type Error string
     9  
    10  func (e Error) Error() string { return string(e) }
    11  
    12  func Errorf(msg string, args ...interface{}) Error {
    13  	return Error(fmt.Sprintf(msg, args...))
    14  }
    15  
    16  const (
    17  	// ErrNoTopic is returned when a request needs to be sent to a specific.
    18  	ErrNoTopic Error = "topic not found"
    19  
    20  	// ErrNoPartition is returned when a request needs to be sent to a specific
    21  	// partition, but the client did not find it in the cluster metadata.
    22  	ErrNoPartition Error = "topic partition not found"
    23  
    24  	// ErrNoLeader is returned when a request needs to be sent to a partition
    25  	// leader, but the client could not determine what the leader was at this
    26  	// time.
    27  	ErrNoLeader Error = "topic partition has no leader"
    28  
    29  	// ErrNoRecord is returned when attempting to write a message containing an
    30  	// empty record set (which kafka forbids).
    31  	//
    32  	// We handle this case client-side because kafka will close the connection
    33  	// that it received an empty produce request on, causing all concurrent
    34  	// requests to be aborted.
    35  	ErrNoRecord Error = "record set contains no records"
    36  
    37  	// ErrNoReset is returned by ResetRecordReader when the record reader does
    38  	// not support being reset.
    39  	ErrNoReset Error = "record sequence does not support reset"
    40  )
    41  
    42  type TopicError struct {
    43  	Topic string
    44  	Err   error
    45  }
    46  
    47  func NewTopicError(topic string, err error) *TopicError {
    48  	return &TopicError{Topic: topic, Err: err}
    49  }
    50  
    51  func NewErrNoTopic(topic string) *TopicError {
    52  	return NewTopicError(topic, ErrNoTopic)
    53  }
    54  
    55  func (e *TopicError) Error() string {
    56  	return fmt.Sprintf("%v (topic=%q)", e.Err, e.Topic)
    57  }
    58  
    59  func (e *TopicError) Unwrap() error {
    60  	return e.Err
    61  }
    62  
    63  type TopicPartitionError struct {
    64  	Topic     string
    65  	Partition int32
    66  	Err       error
    67  }
    68  
    69  func NewTopicPartitionError(topic string, partition int32, err error) *TopicPartitionError {
    70  	return &TopicPartitionError{
    71  		Topic:     topic,
    72  		Partition: partition,
    73  		Err:       err,
    74  	}
    75  }
    76  
    77  func NewErrNoPartition(topic string, partition int32) *TopicPartitionError {
    78  	return NewTopicPartitionError(topic, partition, ErrNoPartition)
    79  }
    80  
    81  func NewErrNoLeader(topic string, partition int32) *TopicPartitionError {
    82  	return NewTopicPartitionError(topic, partition, ErrNoLeader)
    83  }
    84  
    85  func (e *TopicPartitionError) Error() string {
    86  	return fmt.Sprintf("%v (topic=%q partition=%d)", e.Err, e.Topic, e.Partition)
    87  }
    88  
    89  func (e *TopicPartitionError) Unwrap() error {
    90  	return e.Err
    91  }