Skip to content

Commit

Permalink
error messages were refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
tengu-alt authored and tengu-alt committed Oct 14, 2024
1 parent 953e0df commit 516f6e3
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 97 deletions.
6 changes: 3 additions & 3 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (cfg *ClusterConfig) filterHost(host *HostInfo) bool {
}

var (
ErrNoHosts = errors.New("no hosts provided")
ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
ErrHostQueryFailed = errors.New("unable to populate Hosts")
ErrAuthenticatorAndAuthProvider = errors.New("gocql: Can't use both Authenticator and AuthProvider in cluster config.")
ErrNoHosts = errors.New("gocql: no hosts provided")
ErrNoConnectionsStarted = errors.New("gocql: no connections were made when creating the session")
)
14 changes: 7 additions & 7 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type PasswordAuthenticator struct {

func (p PasswordAuthenticator) Challenge(req []byte) ([]byte, Authenticator, error) {
if !approve(string(req), p.AllowedAuthenticators) {
return nil, nil, fmt.Errorf("unexpected authenticator %q", req)
return nil, nil, fmt.Errorf("gocql: unexpected authenticator %q", req)
}
resp := make([]byte, 2+len(p.Username)+len(p.Password))
resp[0] = 0
Expand Down Expand Up @@ -484,7 +484,7 @@ func (s *startupCoordinator) startup(ctx context.Context, supported map[string][

func (s *startupCoordinator) authenticateHandshake(ctx context.Context, authFrame *authenticateFrame) error {
if s.conn.auth == nil {
return fmt.Errorf("authentication required (using %q)", authFrame.class)
return fmt.Errorf("gocql: authentication required (using %q)", authFrame.class)
}

resp, challenger, err := s.conn.auth.Challenge([]byte(authFrame.class))
Expand Down Expand Up @@ -517,7 +517,7 @@ func (s *startupCoordinator) authenticateHandshake(ctx context.Context, authFram
data: resp,
}
default:
return fmt.Errorf("unknown frame response during authentication: %v", v)
return fmt.Errorf("gocql: unknown frame response during authentication: %v", v)
}
}
}
Expand Down Expand Up @@ -1019,7 +1019,7 @@ func (c *Conn) addCall(call *callReq) error {
}
existingCall := c.calls[call.streamID]
if existingCall != nil {
return fmt.Errorf("attempting to use stream already in use: %d -> %d", call.streamID,
return fmt.Errorf("gocql: attempting to use stream already in use: %d -> %d", call.streamID,
existingCall.streamID)
}
c.calls[call.streamID] = call
Expand Down Expand Up @@ -1291,7 +1291,7 @@ func (c *Conn) prepareStatement(ctx context.Context, stmt string, tracer Tracer)
response: x.respMeta,
}
case error:
flight.err = x
flight.err = fmt.Errorf("cassandra: %w", x)
default:
flight.err = NewErrProtocol("Unknown type in response to prepare frame: %s", x)
}
Expand Down Expand Up @@ -1528,7 +1528,7 @@ func (c *Conn) UseKeyspace(keyspace string) error {
switch x := resp.(type) {
case *resultKeyspaceFrame:
case error:
return x
return fmt.Errorf("cassandra: %w", x)
default:
return NewErrProtocol("unknown frame in response to USE: %v", x)
}
Expand Down Expand Up @@ -1636,7 +1636,7 @@ func (c *Conn) executeBatch(ctx context.Context, batch *Batch) *Iter {

return iter
case error:
return &Iter{err: x, framer: framer}
return &Iter{err: fmt.Errorf("cassandra: %w", x), framer: framer}
default:
return &Iter{err: NewErrProtocol("Unknown type in response to batch statement: %s", x), framer: framer}
}
Expand Down
12 changes: 6 additions & 6 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func hostInfo(addr string, defaultPort int) ([]*HostInfo, error) {
if err != nil {
return nil, err
} else if len(ips) == 0 {
return nil, fmt.Errorf("no IP's returned from DNS lookup for %q", addr)
return nil, fmt.Errorf("gocql: no IP's returned from DNS lookup for %q", addr)
}

// Filter to v4 addresses if any present
Expand Down Expand Up @@ -275,7 +275,7 @@ func (c *controlConn) connect(hosts []*HostInfo) error {
conn = nil
}
if conn == nil {
return fmt.Errorf("unable to connect to initial hosts: %v", err)
return fmt.Errorf("gocql: unable to connect to initial hosts: %w", err)
}

// we could fetch the initial ring here and update initial host data. So that
Expand All @@ -302,11 +302,11 @@ func (c *controlConn) setupConn(conn *Conn) error {
host = c.session.ring.addOrUpdate(host)

if c.session.cfg.filterHost(host) {
return fmt.Errorf("host was filtered: %v", host.ConnectAddress())
return fmt.Errorf("gocql: host was filtered: %v", host.ConnectAddress())
}

if err := c.registerEvents(conn); err != nil {
return fmt.Errorf("register events: %v", err)
return fmt.Errorf("gocql: register events: %w", err)
}

ch := &connHost{
Expand Down Expand Up @@ -356,7 +356,7 @@ func (c *controlConn) registerEvents(conn *Conn) error {
if err != nil {
return err
} else if _, ok := frame.(*readyFrame); !ok {
return fmt.Errorf("unexpected frame in response to register: got %T: %v\n", frame, frame)
return fmt.Errorf("gocql: unexpected frame in response to register: got %T: %v\n", frame, frame)
}

return nil
Expand Down Expand Up @@ -413,7 +413,7 @@ func (c *controlConn) attemptReconnect() (*Conn, error) {
// changed their IPs while keeping the same hostname(s).
initialHosts, resolvErr := addrsToHosts(c.session.cfg.Hosts, c.session.cfg.Port, c.session.logger)
if resolvErr != nil {
return nil, fmt.Errorf("resolve contact points' hostnames: %v", resolvErr)
return nil, fmt.Errorf("gocql: resolve contact points' hostnames: %w", resolvErr)
}

return c.attemptReconnectToAnyOfHosts(initialHosts)
Expand Down
4 changes: 2 additions & 2 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func (hd *defaultHostDialer) DialHost(ctx context.Context, host *HostInfo) (*Dia
port := host.Port()

if !validIpAddr(ip) {
return nil, fmt.Errorf("host missing connect ip address: %v", ip)
return nil, fmt.Errorf("gocql: host missing connect ip address: %v", ip)
} else if port == 0 {
return nil, fmt.Errorf("host missing port: %v", port)
return nil, fmt.Errorf("gocql: host missing port: %v", port)
}

connAddr := host.ConnectAddressAndPort()
Expand Down
2 changes: 1 addition & 1 deletion filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func WhiteListHostFilter(hosts ...string) HostFilter {
hostInfos, err := addrsToHosts(hosts, 9042, nopLogger{})
if err != nil {
// dont want to panic here, but rather not break the API
panic(fmt.Errorf("unable to lookup host info from address: %v", err))
panic(fmt.Errorf("gocql: unable to lookup host info from address: %w", err))
}

m := make(map[string]bool, len(hostInfos))
Expand Down
54 changes: 27 additions & 27 deletions frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (c *Consistency) UnmarshalText(text []byte) error {
case "LOCAL_ONE":
*c = LocalOne
default:
return fmt.Errorf("invalid consistency %q", string(text))
return fmt.Errorf("gocql: invalid consistency %q", string(text))
}

return nil
Expand Down Expand Up @@ -315,7 +315,7 @@ func (s *SerialConsistency) UnmarshalText(text []byte) error {
case "LOCAL_SERIAL":
*s = LocalSerial
default:
return fmt.Errorf("invalid consistency %q", string(text))
return fmt.Errorf("gocql: invalid consistency %q", string(text))
}

return nil
Expand All @@ -326,7 +326,7 @@ const (
)

var (
ErrFrameTooBig = errors.New("frame length is bigger than the maximum allowed")
ErrFrameTooBig = errors.New("gocql: frame length is bigger than the maximum allowed")
)

const maxFrameHeaderSize = 9
Expand Down Expand Up @@ -469,15 +469,15 @@ func readHeader(r io.Reader, p []byte) (head frameHeader, err error) {

if version > protoVersion2 {
if len(p) != 9 {
return frameHeader{}, fmt.Errorf("not enough bytes to read header require 9 got: %d", len(p))
return frameHeader{}, fmt.Errorf("gocql: not enough bytes to read header require 9 got: %d", len(p))
}

head.stream = int(int16(p[2])<<8 | int16(p[3]))
head.op = frameOp(p[4])
head.length = int(readInt(p[5:]))
} else {
if len(p) != 8 {
return frameHeader{}, fmt.Errorf("not enough bytes to read header require 8 got: %d", len(p))
return frameHeader{}, fmt.Errorf("gocql: not enough bytes to read header require 8 got: %d", len(p))
}

head.stream = int(int8(p[2]))
Expand All @@ -501,12 +501,12 @@ func (f *framer) payload() {
// reads a frame form the wire into the framers buffer
func (f *framer) readFrame(r io.Reader, head *frameHeader) error {
if head.length < 0 {
return fmt.Errorf("frame body length can not be less than 0: %d", head.length)
return fmt.Errorf("gocql: frame body length can not be less than 0: %d", head.length)
} else if head.length > maxFrameSize {
// need to free up the connection to be used again
_, err := io.CopyN(ioutil.Discard, r, int64(head.length))
if err != nil {
return fmt.Errorf("error whilst trying to discard frame with invalid length: %v", err)
return fmt.Errorf("gocql: error whilst trying to discard frame with invalid length: %w", err)
}
return ErrFrameTooBig
}
Expand All @@ -521,7 +521,7 @@ func (f *framer) readFrame(r io.Reader, head *frameHeader) error {
// assume the underlying reader takes care of timeouts and retries
n, err := io.ReadFull(r, f.buf)
if err != nil {
return fmt.Errorf("unable to read frame body: read %d/%d bytes: %v", n, head.length, err)
return fmt.Errorf("gocql: unable to read frame body: read %d/%d bytes: %w", n, head.length, err)
}

if head.flags&flagCompress == flagCompress {
Expand All @@ -531,7 +531,7 @@ func (f *framer) readFrame(r io.Reader, head *frameHeader) error {

f.buf, err = f.compres.Decode(f.buf)
if err != nil {
return err
return fmt.Errorf("gocql: %w", err)
}
}

Expand Down Expand Up @@ -707,7 +707,7 @@ func (f *framer) parseErrorFrame() frame {
// TODO(zariel): we should have some distinct types for these errors
return errD
default:
panic(fmt.Errorf("unknown error code: 0x%x", errD.code))
panic(fmt.Errorf("gocql: unknown error code: 0x%x", errD.code))
}
}

Expand Down Expand Up @@ -776,7 +776,7 @@ func (f *framer) finish() error {
// TODO: only compress frames which are big enough
compressed, err := f.compres.Encode(f.buf[f.headSize:])
if err != nil {
return err
return fmt.Errorf("gocql: %w", err)
}

f.buf = append(f.buf[:f.headSize], compressed...)
Expand Down Expand Up @@ -856,7 +856,7 @@ func (w *writePrepareFrame) buildFrame(f *framer, streamID int) error {
if f.proto > protoVersion4 {
flags |= flagWithPreparedKeyspace
} else {
panic(fmt.Errorf("the keyspace can only be set with protocol 5 or higher"))
panic(fmt.Errorf("gocql: the keyspace can only be set with protocol 5 or higher"))
}
}
if f.proto > protoVersion4 {
Expand Down Expand Up @@ -955,7 +955,7 @@ func (f *framer) parsePreparedMetadata() preparedMetadata {
meta.flags = f.readInt()
meta.colCount = f.readInt()
if meta.colCount < 0 {
panic(fmt.Errorf("received negative column count: %d", meta.colCount))
panic(fmt.Errorf("gocql: received negative column count: %d", meta.colCount))
}
meta.actualColCount = meta.colCount

Expand Down Expand Up @@ -1052,7 +1052,7 @@ func (f *framer) parseResultMetadata() resultMetadata {
meta.flags = f.readInt()
meta.colCount = f.readInt()
if meta.colCount < 0 {
panic(fmt.Errorf("received negative column count: %d", meta.colCount))
panic(fmt.Errorf("gocql: received negative column count: %d", meta.colCount))
}
meta.actualColCount = meta.colCount

Expand Down Expand Up @@ -1139,7 +1139,7 @@ func (f *framer) parseResultRows() frame {

result.numRows = f.readInt()
if result.numRows < 0 {
panic(fmt.Errorf("invalid row_count in result frame: %d", result.numRows))
panic(fmt.Errorf("gocql: invalid row_count in result frame: %d", result.numRows))
}

return result
Expand Down Expand Up @@ -1507,7 +1507,7 @@ func (f *framer) writeQueryParams(opts *queryParams) {
if f.proto > protoVersion4 {
flags |= flagWithKeyspace
} else {
panic(fmt.Errorf("the keyspace can only be set with protocol 5 or higher"))
panic(fmt.Errorf("gocql: the keyspace can only be set with protocol 5 or higher"))
}
}

Expand Down Expand Up @@ -1770,7 +1770,7 @@ func (f *framer) writeRegisterFrame(streamID int, w *writeRegisterFrame) error {

func (f *framer) readByte() byte {
if len(f.buf) < 1 {
panic(fmt.Errorf("not enough bytes in buffer to read byte require 1 got: %d", len(f.buf)))
panic(fmt.Errorf("gocql: not enough bytes in buffer to read byte require 1 got: %d", len(f.buf)))
}

b := f.buf[0]
Expand All @@ -1780,7 +1780,7 @@ func (f *framer) readByte() byte {

func (f *framer) readInt() (n int) {
if len(f.buf) < 4 {
panic(fmt.Errorf("not enough bytes in buffer to read int require 4 got: %d", len(f.buf)))
panic(fmt.Errorf("gocql: not enough bytes in buffer to read int require 4 got: %d", len(f.buf)))
}

n = int(int32(f.buf[0])<<24 | int32(f.buf[1])<<16 | int32(f.buf[2])<<8 | int32(f.buf[3]))
Expand All @@ -1790,7 +1790,7 @@ func (f *framer) readInt() (n int) {

func (f *framer) readShort() (n uint16) {
if len(f.buf) < 2 {
panic(fmt.Errorf("not enough bytes in buffer to read short require 2 got: %d", len(f.buf)))
panic(fmt.Errorf("gocql: not enough bytes in buffer to read short require 2 got: %d", len(f.buf)))
}
n = uint16(f.buf[0])<<8 | uint16(f.buf[1])
f.buf = f.buf[2:]
Expand All @@ -1801,7 +1801,7 @@ func (f *framer) readString() (s string) {
size := f.readShort()

if len(f.buf) < int(size) {
panic(fmt.Errorf("not enough bytes in buffer to read string require %d got: %d", size, len(f.buf)))
panic(fmt.Errorf("gocql: not enough bytes in buffer to read string require %d got: %d", size, len(f.buf)))
}

s = string(f.buf[:size])
Expand All @@ -1813,7 +1813,7 @@ func (f *framer) readLongString() (s string) {
size := f.readInt()

if len(f.buf) < size {
panic(fmt.Errorf("not enough bytes in buffer to read long string require %d got: %d", size, len(f.buf)))
panic(fmt.Errorf("gocql: not enough bytes in buffer to read long string require %d got: %d", size, len(f.buf)))
}

s = string(f.buf[:size])
Expand All @@ -1823,7 +1823,7 @@ func (f *framer) readLongString() (s string) {

func (f *framer) readUUID() *UUID {
if len(f.buf) < 16 {
panic(fmt.Errorf("not enough bytes in buffer to read uuid require %d got: %d", 16, len(f.buf)))
panic(fmt.Errorf("gocql: not enough bytes in buffer to read uuid require %d got: %d", 16, len(f.buf)))
}

// TODO: how to handle this error, if it is a uuid, then sureley, problems?
Expand All @@ -1850,7 +1850,7 @@ func (f *framer) readBytesInternal() ([]byte, error) {
}

if len(f.buf) < size {
return nil, fmt.Errorf("not enough bytes in buffer to read bytes require %d got: %d", size, len(f.buf))
return nil, fmt.Errorf("gocql: not enough bytes in buffer to read bytes require %d got: %d", size, len(f.buf))
}

l := f.buf[:size]
Expand All @@ -1871,7 +1871,7 @@ func (f *framer) readBytes() []byte {
func (f *framer) readShortBytes() []byte {
size := f.readShort()
if len(f.buf) < int(size) {
panic(fmt.Errorf("not enough bytes in buffer to read short bytes: require %d got %d", size, len(f.buf)))
panic(fmt.Errorf("gocql: not enough bytes in buffer to read short bytes: require %d got %d", size, len(f.buf)))
}

l := f.buf[:size]
Expand All @@ -1882,18 +1882,18 @@ func (f *framer) readShortBytes() []byte {

func (f *framer) readInetAdressOnly() net.IP {
if len(f.buf) < 1 {
panic(fmt.Errorf("not enough bytes in buffer to read inet size require %d got: %d", 1, len(f.buf)))
panic(fmt.Errorf("gocql: not enough bytes in buffer to read inet size require %d got: %d", 1, len(f.buf)))
}

size := f.buf[0]
f.buf = f.buf[1:]

if !(size == 4 || size == 16) {
panic(fmt.Errorf("invalid IP size: %d", size))
panic(fmt.Errorf("gocql: invalid IP size: %d", size))
}

if len(f.buf) < 1 {
panic(fmt.Errorf("not enough bytes in buffer to read inet require %d got: %d", size, len(f.buf)))
panic(fmt.Errorf("gocql: not enough bytes in buffer to read inet require %d got: %d", size, len(f.buf)))
}

ip := make([]byte, size)
Expand Down
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func goType(t TypeInfo) (reflect.Type, error) {
case TypeDuration:
return reflect.TypeOf(*new(Duration)), nil
default:
return nil, fmt.Errorf("cannot create Go type for unknown CQL type %s", t)
return nil, fmt.Errorf("gocql: cannot create Go type for unknown CQL type %s", t)
}
}

Expand Down
Loading

0 comments on commit 516f6e3

Please sign in to comment.