Skip to content

Commit

Permalink
add custom error type for unknown topic or partition (segmentio#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
alpergencdev committed Jul 9, 2024
1 parent 7b9c99d commit 4eaa652
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (d *Dialer) LookupPartition(ctx context.Context, network string, address st
}
}

errch <- UnknownTopicOrPartition
errch <- unknownTopicOrPartition(topic, &partition)
}()

var prt Partition
Expand Down
17 changes: 17 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,23 @@ func (e MessageTooLargeError) Error() string {
return MessageSizeTooLarge.Error()
}

type UnknownTopicOrPartitionError struct {
Topic string
Partition *int
}

func unknownTopicOrPartition(topic string, partition *int) UnknownTopicOrPartitionError {
return UnknownTopicOrPartitionError{Topic: topic, Partition: partition}
}

func (e UnknownTopicOrPartitionError) Error() string {
return UnknownTopicOrPartition.Error()
}

func (e UnknownTopicOrPartitionError) Is(err error) bool {
return errors.Is(err, UnknownTopicOrPartition)
}

func makeError(code int16, message string) error {
if code == 0 {
return nil
Expand Down
2 changes: 1 addition & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ func (w *Writer) partitions(ctx context.Context, topic string) (int, error) {
return len(t.Partitions), nil
}
}
return 0, UnknownTopicOrPartition
return 0, unknownTopicOrPartition(topic, nil)
}

func (w *Writer) client(timeout time.Duration) *Client {
Expand Down

0 comments on commit 4eaa652

Please sign in to comment.