Skip to content

Commit

Permalink
Use sql.ErrNoRows as value for pgx.ErrNoRows
Browse files Browse the repository at this point in the history
  • Loading branch information
ninedraft committed Aug 26, 2024
1 parent 4f7e19d commit 035bbbe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
19 changes: 18 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgx
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -102,11 +103,27 @@ func (ident Identifier) Sanitize() string {

var (
// ErrNoRows occurs when rows are expected but none are returned.
ErrNoRows = errors.New("no rows in result set")
ErrNoRows = newProxyErr(sql.ErrNoRows, "no rows in result set")
// ErrTooManyRows occurs when more rows than expected are returned.
ErrTooManyRows = errors.New("too many rows in result set")
)

func newProxyErr(background error, msg string) error {
return &proxyError{
msg: msg,
background: background,
}
}

type proxyError struct {
msg string
background error
}

func (err *proxyError) Error() string { return err.msg }

func (err *proxyError) Unwrap() error { return err.background }

var (
errDisabledStatementCache = fmt.Errorf("cannot use QueryExecModeCacheStatement with disabled statement cache")
errDisabledDescriptionCache = fmt.Errorf("cannot use QueryExecModeCacheDescribe with disabled description cache")
Expand Down
11 changes: 11 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgx_test
import (
"bytes"
"context"
"database/sql"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -1408,3 +1409,13 @@ func TestConnDeallocateInvalidatedCachedStatementsInTransactionWithBatch(t *test

ensureConnValid(t, conn)
}

func TestErrNoRows(t *testing.T) {
t.Parallel()

// ensure we preserve old error message
require.Equal(t, "no rows in result set", pgx.ErrNoRows.Error())

require.ErrorIs(t, pgx.ErrNoRows, sql.ErrNoRows, "pgx.ErrNowRows must match sql.ErrNoRows")
require.ErrorIs(t, pgx.ErrNoRows, pgx.ErrNoRows, "sql.ErrNowRows must match pgx.ErrNoRows")
}

0 comments on commit 035bbbe

Please sign in to comment.