Skip to content

Commit 97d20cc

Browse files
authoredAug 26, 2024
Merge pull request #2115 from ninedraft/sql-err-no-rows
Use sql.ErrNoRows as value for pgx.ErrNoRows
2 parents e9bd382 + 035bbbe commit 97d20cc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
 

‎conn.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pgx
33
import (
44
"context"
55
"crypto/sha256"
6+
"database/sql"
67
"encoding/hex"
78
"errors"
89
"fmt"
@@ -102,11 +103,27 @@ func (ident Identifier) Sanitize() string {
102103

103104
var (
104105
// ErrNoRows occurs when rows are expected but none are returned.
105-
ErrNoRows = errors.New("no rows in result set")
106+
ErrNoRows = newProxyErr(sql.ErrNoRows, "no rows in result set")
106107
// ErrTooManyRows occurs when more rows than expected are returned.
107108
ErrTooManyRows = errors.New("too many rows in result set")
108109
)
109110

111+
func newProxyErr(background error, msg string) error {
112+
return &proxyError{
113+
msg: msg,
114+
background: background,
115+
}
116+
}
117+
118+
type proxyError struct {
119+
msg string
120+
background error
121+
}
122+
123+
func (err *proxyError) Error() string { return err.msg }
124+
125+
func (err *proxyError) Unwrap() error { return err.background }
126+
110127
var (
111128
errDisabledStatementCache = fmt.Errorf("cannot use QueryExecModeCacheStatement with disabled statement cache")
112129
errDisabledDescriptionCache = fmt.Errorf("cannot use QueryExecModeCacheDescribe with disabled description cache")

‎conn_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pgx_test
33
import (
44
"bytes"
55
"context"
6+
"database/sql"
67
"os"
78
"strings"
89
"sync"
@@ -1408,3 +1409,13 @@ func TestConnDeallocateInvalidatedCachedStatementsInTransactionWithBatch(t *test
14081409

14091410
ensureConnValid(t, conn)
14101411
}
1412+
1413+
func TestErrNoRows(t *testing.T) {
1414+
t.Parallel()
1415+
1416+
// ensure we preserve old error message
1417+
require.Equal(t, "no rows in result set", pgx.ErrNoRows.Error())
1418+
1419+
require.ErrorIs(t, pgx.ErrNoRows, sql.ErrNoRows, "pgx.ErrNowRows must match sql.ErrNoRows")
1420+
require.ErrorIs(t, pgx.ErrNoRows, pgx.ErrNoRows, "sql.ErrNowRows must match pgx.ErrNoRows")
1421+
}

0 commit comments

Comments
 (0)