Skip to content

Commit a8e5eab

Browse files
Add ErrorType method to MessageTooLargeError (#1311)
* Add ErrorType method to MessageTooLargeError struct to support functionality with the errors.Is function. See #1293 * implement the unwrap method to satisfy errors.Is * support error interface for unwrap
1 parent 4713019 commit a8e5eab

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

error.go

+5
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ func coalesceErrors(errs ...error) error {
636636
return nil
637637
}
638638

639+
// MessageTooLargeError is returned when a message is too large to fit within the allowed size.
639640
type MessageTooLargeError struct {
640641
Message Message
641642
Remaining []Message
@@ -655,6 +656,10 @@ func (e MessageTooLargeError) Error() string {
655656
return MessageSizeTooLarge.Error()
656657
}
657658

659+
func (e MessageTooLargeError) Unwrap() error {
660+
return MessageSizeTooLarge
661+
}
662+
658663
func makeError(code int16, message string) error {
659664
if code == 0 {
660665
return nil

error_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package kafka
33
import (
44
"fmt"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestError(t *testing.T) {
@@ -110,4 +112,16 @@ func TestError(t *testing.T) {
110112
t.Error("non-empty description:", s)
111113
}
112114
})
115+
116+
t.Run("MessageTooLargeError error.Is satisfaction", func(t *testing.T) {
117+
err := MessageSizeTooLarge
118+
msg := []Message{
119+
{Key: []byte("key"), Value: []byte("value")},
120+
{Key: []byte("key"), Value: make([]byte, 8)},
121+
}
122+
msgTooLarge := messageTooLarge(msg, 1)
123+
assert.NotErrorIs(t, err, msgTooLarge)
124+
assert.Contains(t, msgTooLarge.Error(), MessageSizeTooLarge.Error())
125+
assert.ErrorIs(t, msgTooLarge, MessageSizeTooLarge)
126+
})
113127
}

0 commit comments

Comments
 (0)