Skip to content

surface original network errors and create error stacks #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions packet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *Conn) ReadPacketTo(w io.Writer) error {
header := []byte{0, 0, 0, 0}

if _, err := io.ReadFull(c.reader, header); err != nil {
return ErrBadConn
return errors.Wrapf(ErrBadConn, "io.ReadFull(header) failed. err %v", err)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preserving dummy ErrBadConn here, because code in canal/canal.go, failover/server.go and driver/driver.go is checking for it by applying errors.Cause

}

length := int(uint32(header[0]) | uint32(header[1])<<8 | uint32(header[2])<<16)
Expand All @@ -112,16 +112,16 @@ func (c *Conn) ReadPacketTo(w io.Writer) error {
}

if n, err := io.CopyN(w, c.reader, int64(length)); err != nil {
return ErrBadConn
return errors.Wrapf(ErrBadConn, "io.CopyN failed. err %v, copied %v, expected %v", err, n, length)
} else if n != int64(length) {
return ErrBadConn
return errors.Wrapf(ErrBadConn, "io.CopyN failed(n != int64(length)). %v bytes copied, while %v expected", n, length)
} else {
if length < MaxPayloadLen {
return nil
}

if err := c.ReadPacketTo(w); err != nil {
return err
return errors.Wrap(err, "ReadPacketTo failed")
}
}

Expand All @@ -141,9 +141,9 @@ func (c *Conn) WritePacket(data []byte) error {
data[3] = c.Sequence

if n, err := c.Write(data[:4+MaxPayloadLen]); err != nil {
return ErrBadConn
return errors.Wrapf(ErrBadConn, "Write(payload portion) failed. err %v", err)
} else if n != (4 + MaxPayloadLen) {
return ErrBadConn
return errors.Wrapf(ErrBadConn, "Write(payload portion) failed. only %v bytes written, while %v expected", n, 4+MaxPayloadLen)
} else {
c.Sequence++
length -= MaxPayloadLen
Expand All @@ -157,9 +157,9 @@ func (c *Conn) WritePacket(data []byte) error {
data[3] = c.Sequence

if n, err := c.Write(data); err != nil {
return ErrBadConn
return errors.Wrapf(ErrBadConn, "Write failed. err %v", err)
} else if n != len(data) {
return ErrBadConn
return errors.Wrapf(ErrBadConn, "Write failed. only %v bytes written, while %v expected", n, len(data))
} else {
c.Sequence++
return nil
Expand All @@ -177,7 +177,7 @@ func (c *Conn) WriteClearAuthPacket(password string) error {
copy(data[4:], password)
data[4+pktLen-1] = 0x00

return c.WritePacket(data)
return errors.Wrap(c.WritePacket(data), "WritePacket failed")
}

// WritePublicKeyAuthPacket: Caching sha2 authentication. Public key request and send encrypted password
Expand All @@ -186,17 +186,19 @@ func (c *Conn) WritePublicKeyAuthPacket(password string, cipher []byte) error {
// request public key
data := make([]byte, 4+1)
data[4] = 2 // cachingSha2PasswordRequestPublicKey
c.WritePacket(data)
if err := c.WritePacket(data); err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a liberty to add error checking here

return errors.Wrap(err, "WritePacket(single byte) failed")
}

data, err := c.ReadPacket()
if err != nil {
return err
return errors.Wrap(err, "ReadPacket failed")
}

block, _ := pem.Decode(data[1:])
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
return errors.Wrap(err, "x509.ParsePKIXPublicKey failed")
}

plain := make([]byte, len(password)+1)
Expand All @@ -209,15 +211,15 @@ func (c *Conn) WritePublicKeyAuthPacket(password string, cipher []byte) error {
enc, _ := rsa.EncryptOAEP(sha1v, rand.Reader, pub.(*rsa.PublicKey), plain, nil)
data = make([]byte, 4+len(enc))
copy(data[4:], enc)
return c.WritePacket(data)
return errors.Wrap(c.WritePacket(data), "WritePacket failed")
}

func (c *Conn) WriteEncryptedPassword(password string, seed []byte, pub *rsa.PublicKey) error {
enc, err := EncryptPassword(password, seed, pub)
if err != nil {
return err
return errors.Wrap(err, "EncryptPassword failed")
}
return c.WriteAuthSwitchPacket(enc, false)
return errors.Wrap(c.WriteAuthSwitchPacket(enc, false), "WriteAuthSwitchPacket failed")
}

// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
Expand All @@ -234,7 +236,7 @@ func (c *Conn) WriteAuthSwitchPacket(authData []byte, addNUL bool) error {
data[pktLen-1] = 0x00
}

return c.WritePacket(data)
return errors.Wrap(c.WritePacket(data), "WritePacket failed")
}

func (c *Conn) ResetSequence() {
Expand All @@ -244,7 +246,7 @@ func (c *Conn) ResetSequence() {
func (c *Conn) Close() error {
c.Sequence = 0
if c.Conn != nil {
return c.Conn.Close()
return errors.Wrap(c.Conn.Close(), "Conn.Close failed")
}
return nil
}