Skip to content

Commit

Permalink
ListenerConn: Report better errors from sendSimpleQuery
Browse files Browse the repository at this point in the history
ErrBadConns here are completely useless, since there's no database/sql
to talk to.
  • Loading branch information
johto committed May 2, 2015
1 parent 40e0272 commit f324032
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
21 changes: 5 additions & 16 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,11 @@ func Open(name string) (_ driver.Conn, err error) {
}

func DialOpen(d Dialer, name string) (_ driver.Conn, err error) {
defer func() {
// Handle any panics during connection initialization. Note that we
// specifically do *not* want to use errRecover(), as that would turn
// any connection errors into ErrBadConns, hiding the real error
// message from the user.
e := recover()
if e == nil {
// Do nothing
return
}
var ok bool
err, ok = e.(error)
if !ok {
err = fmt.Errorf("pq: unexpected error: %#v", e)
}
}()
// Handle any panics during connection initialization. Note that we
// specifically do *not* want to use errRecover(), as that would turn any
// connection errors into ErrBadConns, hiding the real error message from
// the user.
defer errRecoverNoErrBadConn(&err)

o := make(values)

Expand Down
13 changes: 13 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,19 @@ func errorf(s string, args ...interface{}) {
panic(fmt.Errorf("pq: %s", fmt.Sprintf(s, args...)))
}

func errRecoverNoErrBadConn(err *error) {
e := recover()
if e == nil {
// Do nothing
return
}
var ok bool
err, ok = e.(error)
if !ok {
err = fmt.Errorf("pq: unexpected error: %#v", e)
}
}

func (c *conn) errRecover(err *error) {
e := recover()
switch v := e.(type) {
Expand Down
4 changes: 2 additions & 2 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (l *ListenerConn) setState(newState int32) bool {
// away or should be discarded because we couldn't agree on the state with the
// server backend.
func (l *ListenerConn) listenerConnLoop() (err error) {
defer l.cn.errRecover(&err)
defer errRecoverNoErrBadConn(&err)

r := &readBuf{}
for {
Expand Down Expand Up @@ -244,7 +244,7 @@ func (l *ListenerConn) Ping() error {
// The caller must be holding senderLock (see acquireSenderLock and
// releaseSenderLock).
func (l *ListenerConn) sendSimpleQuery(q string) (err error) {
defer l.cn.errRecover(&err)
defer errRecoverNoErrBadConn(&err)

// must set connection state before sending the query
if !l.setState(connStateExpectResponse) {
Expand Down

0 comments on commit f324032

Please sign in to comment.