maxVarInt8 = 4611686018427387903
)
+type varintLengthError struct {
+ Num uint64
+}
+
+func (e *varintLengthError) Error() string {
+ return fmt.Sprintf("value doesn't fit into 62 bits: %d", e.Num)
+}
+
// Read reads a number in the QUIC varint format from r.
func Read(r io.ByteReader) (uint64, error) {
firstByte, err := r.ReadByte()
uint8(i >> 24), uint8(i >> 16), uint8(i >> 8), uint8(i),
}...)
}
- panic(fmt.Sprintf("%#x doesn't fit into 62 bits", i))
+ panic(&varintLengthError{Num: i})
}
// AppendWithLen append i in the QUIC varint format with the desired length.
}
// Don't use a fmt.Sprintf here to format the error message.
// The function would then exceed the inlining budget.
- panic(struct {
- message string
- num uint64
- }{"value doesn't fit into 62 bits: ", i})
+ panic(&varintLengthError{Num: i})
}
import (
"bytes"
+ "fmt"
"io"
"math/rand/v2"
"testing"
}
t.Run("panics when given a too large number (> 62 bit)", func(t *testing.T) {
- require.Panics(t, func() { Append(nil, maxVarInt8+1) })
+ require.PanicsWithError(t,
+ fmt.Sprintf("value doesn't fit into 62 bits: %d", maxVarInt8+1),
+ func() { Append(nil, maxVarInt8+1) },
+ )
})
}
}
t.Run("panics on too large number", func(t *testing.T) {
- require.Panics(t, func() { Len(maxVarInt8 + 1) })
+ require.PanicsWithError(t,
+ fmt.Sprintf("value doesn't fit into 62 bits: %d", maxVarInt8+1),
+ func() { Len(maxVarInt8 + 1) },
+ )
})
}