Skip to content

Use communicationError for network errors #219

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
14 changes: 10 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func (s transactionStatus) String() string {
panic("not reached")
}

// Any failure in communication should be raised as a communicationError to
// allow errRecover to know what happened.
type communicationError struct {
error
}

type conn struct {
c net.Conn
buf *bufio.Reader
Expand Down Expand Up @@ -153,7 +159,7 @@ func Open(name string) (_ driver.Conn, err error) {

c, err := net.Dial(network(o))
if err != nil {
return nil, err
return nil, communicationError{err}
}

cn := &conn{c: c}
Expand Down Expand Up @@ -556,7 +562,7 @@ func (cn *conn) send(m *writeBuf) {

_, err := cn.c.Write(*m)
if err != nil {
panic(err)
panic(communicationError{err})
}
}

Expand All @@ -574,7 +580,7 @@ func (cn *conn) recvMessage() (byte, *readBuf, error) {
x := cn.scratch[:5]
_, err := io.ReadFull(cn.buf, x)
if err != nil {
return 0, nil, err
return 0, nil, communicationError{err}
}
t := x[0]

Expand All @@ -588,7 +594,7 @@ func (cn *conn) recvMessage() (byte, *readBuf, error) {
}
_, err = io.ReadFull(cn.buf, y)
if err != nil {
return 0, nil, err
return 0, nil, communicationError{err}
}

return t, (*readBuf)(&y), nil
Expand Down
2 changes: 1 addition & 1 deletion conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func TestBadConn(t *testing.T) {

func() {
defer errRecover(&err)
panic(io.EOF)
panic(communicationError{io.EOF})
}()

if err != driver.ErrBadConn {
Expand Down
10 changes: 2 additions & 8 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package pq
import (
"database/sql/driver"
"fmt"
"io"
"net"
"runtime"
)

Expand Down Expand Up @@ -452,14 +450,10 @@ func errRecover(err *error) {
} else {
*err = v
}
case *net.OpError:
case communicationError:
*err = driver.ErrBadConn
case error:
if v == io.EOF || v.(error).Error() == "remote error: handshake failure" {
*err = driver.ErrBadConn
} else {
*err = v
}
*err = v

default:
panic(fmt.Sprintf("unknown error: %#v", e))
Expand Down