Skip to content

Commit ace2755

Browse files
committed
Remove most panics
Remove most panics. The only ones remaining are in writeBuf when the messages are too long (added in #1161). I think that's probably okay: normal operations should never hit this.
1 parent b2ad1f4 commit ace2755

File tree

13 files changed

+616
-476
lines changed

13 files changed

+616
-476
lines changed

array.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,10 @@ func (a StringArray) Value() (driver.Value, error) {
684684
return "{}", nil
685685
}
686686

687-
// appendArray appends rv to the buffer, returning the extended buffer and
688-
// the delimiter used between elements.
687+
// appendArray appends rv to the buffer, returning the extended buffer and the
688+
// delimiter used between elements.
689689
//
690-
// It panics when n <= 0 or rv's Kind is not reflect.Array nor reflect.Slice.
690+
// Returns an error when n <= 0 or rv is not a reflect.Array or reflect.Slice.
691691
func appendArray(b []byte, rv reflect.Value, n int) ([]byte, string, error) {
692692
var del string
693693
var err error
@@ -770,7 +770,11 @@ func appendArrayQuotedBytes(b, v []byte) []byte {
770770
}
771771

772772
func appendValue(b []byte, v driver.Value) ([]byte, error) {
773-
return append(b, encode(nil, v, 0)...), nil
773+
enc, err := encode(nil, v, 0)
774+
if err != nil {
775+
return nil, err
776+
}
777+
return append(b, enc...), nil
774778
}
775779

776780
// parseArray extracts the dimensions and elements of an array represented in

buf.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pq
33
import (
44
"bytes"
55
"encoding/binary"
6+
"fmt"
67
"math"
78

89
"github.com/lib/pq/internal/proto"
@@ -33,7 +34,7 @@ func (b *readBuf) int16() (n int) {
3334
func (b *readBuf) string() string {
3435
i := bytes.IndexByte(*b, 0)
3536
if i < 0 {
36-
errorf("invalid message format; expected string terminator")
37+
panic(fmt.Errorf("pq: invalid message format; expected string terminator"))
3738
}
3839
s := (*b)[:i]
3940
*b = (*b)[i+1:]
@@ -82,7 +83,7 @@ func (b *writeBuf) bytes(v []byte) {
8283
func (b *writeBuf) wrap() []byte {
8384
p := b.buf[b.pos:]
8485
if len(p) > math.MaxUint32 {
85-
errorf("message too large (%d > math.MaxUint32)", len(p))
86+
panic(fmt.Errorf("pq: message too large (%d > math.MaxUint32)", len(p)))
8687
}
8788
binary.BigEndian.PutUint32(p, uint32(len(p)))
8889
return b.buf
@@ -91,7 +92,7 @@ func (b *writeBuf) wrap() []byte {
9192
func (b *writeBuf) next(c proto.RequestCode) {
9293
p := b.buf[b.pos:]
9394
if len(p) > math.MaxUint32 {
94-
errorf("message too large (%d > math.MaxUint32)", len(p))
95+
panic(fmt.Errorf("pq: message too large (%d > math.MaxUint32)", len(p)))
9596
}
9697
binary.BigEndian.PutUint32(p, uint32(len(p)))
9798
b.pos = len(b.buf) + 1

0 commit comments

Comments
 (0)